Include client's default email signature in Gmail using Zend (PHP) - 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.

Related

Sending email with signature using Gmail API

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.

CakePHP and masking email sender

I am currently using a function called send:
public function send(){
if ( !empty($this->request->data) ) {
$email = new CakeEmail('default');
$email->from(array($this->Auth->user('email') => $this->Auth->user('username')))
->to(array('helpdesk#example.com'))
->subject($this->request->data['Ticket']['subject'])
->send(array($this->request->data['Ticket']['issue']));
$this->Session->setFlash('Email Sent Successfully', 'default', array('class' => 'message update span9'));
$this->redirect(array('action' => 'index'));
}
to Send emails to our helpdesk and deposit them into their database. All is working EXCEPT the FROM always shows the username/email address from the configuration options. It is not masking the email with the users email.. I need this to happen so that we know who is having the support issue.
Does anyone have a suggestion here on what to do?
*Addition
This is an intranet application and thus we have an authenticated GENERIC USER using smtp settings. This is not spamming, we just want to know which user the Help Desk ticket came from when inserting to the DB.
Why are you using the Config default anyway?
If you use $email = new CakeEmail();, does the email sent references the Authenticated User email info.
Also, you should always use $email->sender('support#yourcompany.com', 'Your Company Support');. This ensures that if there is an issue the problem get redirected to you and not the user, your app is sending an email on his/her behalf.
I have that setup in my account and it works just fine. To Mark's point, it may not be legal (although, that does not seem to be your issue), but I know it is possible as I have currently a system setup that works with whatever email I want. I do not use any Config and also I do not use any SMTP

Can't send email via SMTP because "550 - Relay Not permitted"

I'm using CakePHP to send automated emails to clients. It's been working great, but it seems some recipients aren't receiving our emails. So I decided to use the SMTP option for sending emails, and route emails through our email provider at Media Temple.
However, when trying to send email from a Media Temple account, I get the error "550- relay not permitted".
That sounds like the Media Temple server is just plain not allowing me to send mail through it.
That's odd because I've confirmed the username and password I'm using is correct and I can send mail via SMTP through it from my macmail client and iPhone mail client. I've also confirmed my cakephp email settings are correct, because I can send emails via SMTP with a gmail account with the exact same configuration in cakephp.
Any idea why I'm getting this error and how to resolve it?
Thanks
Here's the code that handles sending an email. I use this class just like the regular EmailComponent from within many different controllers.
class CanadafindsEmailerComponent extends EmailComponent
{
...
function send($content = null, $template = null, $layout = null) {
if(!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && is_array($this->bcc))
$this->bcc[]=TECHY_MONITOR_EMAIL;
else if (!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && !is_array($this->bcc))
$this->bcc=array(TECHY_MONITOR_EMAIL);
if(DEVSITE){//commented-out code are settings for smtp with gmail, which works fine
$this->delivery = 'smtp';
$this->smtpOptions = array(
'port'=>'465',//'465',
'timeout'=>'30',//'30',
'auth' => true,
'host' => 'ssl://mail.thenumber.biz',//'ssl://smtp.gmail.com',
'username'=>USERNAME,//'USERNAME#gmail.com',
'password'=>SMTP_PASSWORD//,
);
$this->to=$this->correctFormatOn($this->to);
$this->bcc=$this->correctFormatOn($this->bcc);
$this->cc=$this->correctFormatOn($this->cc);
$this->replyTo=$this->correctFormatOn($this->replyTo);
$this->from=$this->correctFormatOn($this->from);
}
return parent::send($content,$template,$layout);
}
function correctFormatOn(&$email){
if(is_array($email)){
$copiedEmail=array();
foreach($email as $singleEmail){
$copiedEmail[]=$this->correctFormatOnSingle($singleEmail);
}
$email=$copiedEmail;
}else{
$email=$this->correctFormatOnSingle($email);
}
return $email;
}
function correctFormatOnSingle(&$email){
$subEmails=explode(",",$email);
$fixedSubEmails=array();
foreach($subEmails as $subEmail){
$fixedSubEmails[]=preg_replace('/<?([^< ]+)#([^>,]+)[>,]?/i', '<$1#$2>', trim($subEmail));
}
$email=implode(",",$fixedSubEmails);
return $email;
}
}
The main problem I was having was that clients weren't receiving emails from our server, (and so I wanted to use an SMTP server to see if that would fix it, instead of the server's default email server).
But I managed to get those clients to receive emails from the server by making some other changes, thus removing the need to use SMTP and the Media Temple email server.
(As an FYI, I found that we were getting bouncebacks from client email servers stating Diagnostic-Code: smtp; 550 Access denied - Invalid HELO name (See RFC2821
4.1.1.1), but they were being sent directly back to the server, and going into the linux user account "www-data". (I read them in /var/mail/www-data, just using tail and vim). I found that postfix, which was handling the sending of emails, was marking the email sender's hostname (ie, "HELO name") as canadafinds3, the name I gave the server in Rackspace, not the domain name: canadafinds.com. So I changed that in /etc/postfix/main.cf, restarted postfix, et voila! No more bouncebacks from those particular clients, and everyone's happy again.)
I ended up writing my own PHP mail() script based on https://web.archive.org/web/20180401094709/http://www.dreamincode.net/forums/topic/36108-send-emails-using-php-smtp-direct/ in order to circumvent this error.

Signing mails sent through SMTP with DKIM

I have setup DKIM in my google apps for mail signing, when sending mails through GMail interface it signs the mail but when I send a mail through my webserver using SMTP (from CodeIgniter) it doesnt signs the mail.
Can anybody help me what should I do?
Yes, it's actually a server configuration thing. But since you posted on SO, there is also a way to sign the mails manually from within PHP. http://php-dkim.sourceforge.net/
Also had been covered before here: Setting up DomainKeys/DKIM in a PHP-based SMTP client
I create new project in googlecode. the name was phpMailDomainSigner
It support DKIM-Signature and DomainKey-Signature in Object Oriented Style.
Here some example:
// Create mailDomainSigner Object
include_once './lib/class.mailDomainSigner.php';
$mds = &new mailDomainSigner($domain_priv,$domain_d,$domain_s);
$new_data = $mds->sign(
$mail_data,
"Message-ID:Subject:From:Content-Type:MIME-Version:Content-Transfer-Encoding:Received:To:Date",
true,true,false);

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