Sending email with signature using Gmail API - php

I got the Gmail Rest API sending part working but the e-mail doesn't include the signature and in recipient's inbox the 'from' label is sender's user id not the name of the user.
This is in php.
$mime = new Mail_mime();
$mime->setFrom("ABCD"); //This doesn't work....
$mime->setSubject('Testing');
$mime->setTXTBody('This is a demo mail');
$mime->addTo('a#a.com');
$message_body = $mime->getMessage();
$encodeMessage = base64url_encode($message_body);
$message = new Google_Service_Gmail_Message();
$message->setRaw($encodeMessage);
$send = $service->users_messages->send('me', $message);
Is there anyway to include the signature and change the 'from'?

The signature is not added by the API because it is a setting on the web client, not a global setting for the entire account. If you configure your Gmail account on Thunderbird, Outlook or another email client, Gmail will not add the signature either. You should think about Gmail in two separate ways:
The web client interface, accessible at https://mail.google.com, which is just an email client like any other;
Your inbox, the place where messages end up in, which is completely independent of the clients you use to access it.
In other words, this is an email client-dependent setting, and the only thing the clients do is add a bit of text to the text you write yourself, nothing else.

I know this is old but you can retrieve via API the users signature.
https://developers.google.com/admin-sdk/email-settings/#manage_signature_settings
you can then append to your email that you are composing.

Related

Take "setFrom" name as in Gmail configuration using Swift Mailer

I'm using Swiftmailer to send emails in Symfony project.
$msg = (new \Swift_Message("Subject"))
->setFrom('xxx123#gmail.com')
->setTo('yyy#gmail.com')
->setBody($mailContent);
In the received email, the name of Sender looks like xxx123. But the name for this email id in its configuration is XXX YYY.
I can use,
->setFrom('xxx123#gmail.com' => 'XXX YYY')
but, I don't want to set name directly in the code as it may be changed to something else like YYY ZZZ.
How can I achieve this? Thanks in advance!!
Sender name is always set in the header of every message. So header can be set by any email client. Your SwiftMailer is also email client in this case.
Your email server can rewrite header but only in specific cases. Like Gmail rewrite all email addresses that you didn't validate in the account settings to your base email.
If you need to fetch your sender name from Gmail account you can use Gmail-API. Authenticate your application with OAuth and send GET request to url:
https://www.googleapis.com/gmail/v1/users/me/settings/sendAs/YOUR_EMAIL_ADDRESS
You will receive JSON like this:
{
"sendAsEmail": "YOUR_EMAIL_ADDRESS",
"displayName": "SENDER NAME THAT YOU NEED",
"replyToAddress": "",
"signature": "Your standard signature for emails created by Gmail in browser",
"isPrimary": true,
"isDefault": true
}
More about Google API for this request you can find here: https://developers.google.com/gmail/api/v1/reference/users/settings/sendAs/get
You can use this code:
$fromEmail = "xxx123#gmail.com";
$fromName = "Sender Name";
$msg = (new \Swift_Message("Subject"))
->setFrom([$fromEmail => $fromName])
->setTo('yyy#gmail.com')
->setBody($mailContent);
If you put only email, code will cut name from email until #. In your case it's: xxx123#gmail.com

when i am sending email from my php code through a 3rd party mail server, where do i save my emails?

i am using php code to send emails to gmail or yahoo ids. i am using a 3rd party mail server to send my emails. my php code works and i can receive the content in my gmail inbox. but where do i save my sent emails ?? who gives me an inbox for all my received / sent emails. suggest me a Name ?
function helloEmail()
{
$from = new Email(null, "test#example.com");
$subject = "Hello World from the SendGrid PHP Library";
$to = new Email(null, "test#example.com");
$content = new Content("text/plain", "some text here");
$mail = new Mail($from, $subject, $to, $content);
$to = new Email(null, "test2#example.com");
$mail->personalization[0]->addTo($to);
//echo json_encode($mail, JSON_PRETTY_PRINT), "\n";
return $mail;
}
There is no inbox for your sent mails. You can send an e-mail using any e-mail address you choose, even if it belongs to someone else or doesn't exist.
For your received e-mail inbox, you'll have to make one yourself via any third party e-mail service such as Gmail or Hotmail, or via your web hosting platform using your own domain name if they provide e-mail services.
To save your sent e-mails you can opt to save them in a database.

Include client's default email signature in Gmail using Zend (PHP)

I'm using Google oAuth2 to authenticate my client then sending emails on their behalf using Zend (v1.12) SMTP, all using PHP. My question is how do I include the client's default email signature (as set in their Gmail settings) on the outgoing mail created using Zend?
I have full access to the client's Gmail so hoping there is a way to get their HTML signature and then add it to my html email body. Something like this:
...
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$obj=json_decode($_SESSION["access_token"],true);
$client_token=$obj['access_token'];
require_once 'Zend/Mail/Transport/Smtp.php';
require_once 'Zend/Mail.php';
$email = 'ClientJohn#abcco.com';//uses gmail business app
$token = $client_token;
$initClientRequestEncoded = base64_encode("user={$email}\1auth=Bearer {$token}\1\1");
$config = array('ssl' => 'ssl', 'port' => '465', 'auth' => 'oauth2', 'xoauth2_request' => $initClientRequestEncoded);
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
$mail = new Zend_Mail();
// Not real code:
$signature=$_GET['client HTML signature'];
//
$mail->setBodyText('EMAIL_BODY'.$signature);
$mail->setBodyHtml('<b>EMAIL</b>_BODY'.$signature);
$mail->setFrom('ClientJohn#abcco.com', 'Client John');
$mail->addTo('michaelt#test.com', 'Michael F');
$mail->setSubject('EMAIL_SUBJECT');
$mail->send($transport);
} else {
$authUrl = $client->createAuthUrl();
}
...
I've reviewed the Gmail rest API (https://developers.google.com/gmail/api/) which doesn't say anything about signatures.
I've searched the web and reviewed numerous questions on Stack Overflow like: How to send an email in C# with gmail template/signature
but don't think this works as I'm not creating the signature, just want to get what the client already uses. This is promising, just not sure how to implement it into my code (as it's using codeigniter):
How can add email signature in codeigniter?
Most of the web searches are about the digital signature of the email. I'm looking for the actual HTML signature at the bottom of the email I create.
If there isn't a way to get it from Gmail I'm thinking I'll have to have each client send me an email, copy and paste their signature HTML string into a database, then will have to pull that HTML for every email based on the user...not ideal because if they change their signature they'll also have to tell me so I can update the DB.
The signature is a client specific setting that sadly cannot be retrieved, as said by borfast here: Sending email with signature using Gmail API
The signature is not added by the API because it is a setting on the
web client, not a global setting for the entire account. If you
configure your Gmail account on Thunderbird, Outlook or another email
client, Gmail will not add the signature either. You should think
about Gmail in two separate ways:
The web client interface, accessible at mail.google.com, which is just
an email client like any other; Your inbox, the place where messages
end up in, which is completely independent of the clients you use to
access it. In other words, this is an email client-dependent setting,
and the only thing the clients do is add a bit of text to the text you
write yourself, nothing else.
With this in mind, it would probably be a good idea for you to let your users create a separate signature in your application that you use when they send mail.

sending emails with codeigniter received in junk mail

am trying to send emails using codeigniter email library to send emails to users using the following settings
$this->load->library('email');
$this->email->from('email#domain.com','Admin');
$this->email->to($recieverEmail);
$this->email->subject('Morgan MarketBook');
$this->email->message($message);
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.server';
$config['smtp_port'] = 26 ;
$config['smtp_user'] = 'user' ;
$config['smtp_pass'] = 'password' ;
$config['newline'] = "\r\n";
?>
my problem that the received emails are in the junk mail and not in the inbox...what is causing this problem ?
Mail getting into junk instead of inbox isn't code related or codeigniter related for that matter. You have to follow some guidelines in order for the email not to be considered spam:
Some of these guidelines are:
when sending html email, include also the text version of the mail
when sending html email, keep the html and images to a minimum (don't include javascript)
setup the mail server with spf and domain keys
the "from" field should contain a valid email address (with the same domain as the mail server)
if you send mass emails, try to limit the rate of sending
there are lots of guidelines for sending valid emails with php, just google "best practice for sending emails php"
Also, don't include your login credentials to your mail server.
Cheers
I beleive the problem is in your server, not in your CodeIgniter code. Try sending e-mail from the same e-mail address using a mail client. If you still receive the e-mail in the Junk mail you should contact your hosting provider and tell them about this problem, but my experience shows that they cannot do nothing about it.

Processing an Email Bounce back in CakePHP and Postfix

I'm trying to handle bounced message and send to a responsible System Administrator.
I use CakePHP Email Component to send the message. On server side, I use postfix to transport the message.
function sendAsEmail($data) {
$Email->sendAs = 'html';
$Email->from = $user['Sender']['username'] . '#example.com';
$Email->return = Configure::read('App.systemAdminEmail');
$Email->bcc = array($data['Message']['recipient_text']);
$content = 'Some content';
$Email->send($content);
}
As you can see above, I set the $Email->return to sysadmin's email which it will send all the bounced message.
On postfix configuration, I tried creating a bounce.cf template and set bounce_template_file. http://www.howtoforge.com/configure-custom-postfix-bounce-messages
How do I get the bounced message and send it to System Administrator?
I think what you'll need to do is to use an SMTP (or I suppose POP3) connector for PHP. Then you'll basically have to create your own PHP email client that will login to the server, ask for the messages that have been bounced, and parse them appropriately.
I would think there would be a CakePHP component for this, but I can't find one.
I would recommend that you use an Envelope Header in your email. Otherwise you'll be stuck trying to parse the recipient server bounce, and those are very very inconsistent. If you use the VERP (variable envelope return protocol?) header, you can encode a unique hash into the email address which should be really easy to parse out in your PHPEmailClient.
More info on VERP: http://en.wikipedia.org/wiki/Variable_envelope_return_path
Cake-specific VERP stuff: http://www.mainelydesign.com/blog/view/setting-envelope-from-header-cakephp-email-component
I also highly recommend that you look into using SwiftMailer. It has a lot of plugins; you might find a base PHP SMTP client that you can easily modify to do what you need. http://swiftmailer.org/

Categories