Emailing through Google Apps / Gmail on CakePHP

When launching new websites and web applications, it is difficult to get on trusted email lists of Hotmail, Yahoo and Etc. Aside from having proper SPF records, a great way to avoid this sysadmin problem is to use Gmail / Google Apps to offload your domain’s email to their servers. By doing this, your domain gets an instant trust factor with receiving mail servers that a new server simply cannot do in a short period of time.
To email with Google Apps / Gmail with CakePHP, I’ve had recent success using the SwiftMailer CakePHP component. It is easy to use and leverages the PHP SwiftMailer library.
Configuring SwiftMailer CakePHP Component with Gmail
This is pulled off the component article’s example, but I posted it here as well for your reference.
$this->SwiftMailer->smtpType = ‘tls’;
$this->SwiftMailer->smtpHost = ‘smtp.gmail.com’;
$this->SwiftMailer->smtpPort = 465;
$this->SwiftMailer->smtpUsername = ‘name@domain.com’;
$this->SwiftMailer->smtpPassword = ‘your_password’;
$this->SwiftMailer->sendAs = ‘both’;
$this->SwiftMailer->from = ‘name@domain.com’;
$this->SwiftMailer->fromName = ‘Full Name’;
$this->SwiftMailer->to = ‘to_name@domain.com’;
$this->SwiftMailer->template = ‘your_action’;
$this->SwiftMailer->subject = ‘your_subject’;
I added two custom attributes here, template and subject because I find I like to set that as a variable instead of putting it in the send function (in the next step).
Emailing with SwiftMailer CakePHP Component
The send function has 3 parameters, but we are only concerned with the first two since we’ve already configured our settings in the step above. The first is the template found in /views/elements/email, the second is the subject.
try {
if(!$this->SwiftMailer->send($this->SwiftMailer->template, $this->SwiftMailer->subject)){
$this->log("Error sending email");
}
} catch(Exception $e) {
$this->log("Failed to send email:".$e->getMessage());
}
You will also notice here that we are wrapping the send function in a try/catch block. This allows us to output whatever caused the function to fail through an exception (and fail silently), rather having the component either kill the client to output the message, or simply return false with no explanation like a lot of things in CakePHP-land. Try/catch blocks work great in non-mission critical things like sending an email, contrary to the core library which should simply fail and exit the application.
I had one error on my local computer with TLS not being installed into PHP. So I added the following line to my PHP compile to add TLS support:
--with-openssl
My entire PHP compile looks like this…
sudo make clean && sudo ./configure —prefix=/usr/local/php5 —with-pear —enable-sockets —with-iodbc=/usr —with-curl=/usr —with-mysql=/usr/local/mysql —without-iconv —with-apxs2=/opt/local/apache2/bin/apxs —with-zlib-dir=../zlib-1.2.3/ —with-jpeg-dir=../jpeg-6b —with-openssl —with-gd —with-freetype2=/Developer/SDKs/MacOSX10.5.sdk/usr/X11/include/freetype2/freetype && sudo make && sudo make install
As an alternative to SwiftMailer, I was pointed to joshua’s solution on the mailing list to use the built-in CakePHP email component with Gmail. Apparently he got it to work, but I couldn’t for whatever reason.
Enjoy having mail go through to your users without fail!
11 comments
CakePHP’s Email Component works with gmail/google apps for me. A couple of gotchas, Google seems to discard any bccs and ccs, also you have to enclose from and to addresses in < and >
About how long does it take to become trusted on those mail sites? Are there certain criteria that have to be met?
I had sent some email to my parents the other day, and they say I wasn’t kicked to their spam filters, so I think I’m good for now.
There is no rule of how long it takes to get on receiving mail server’s trusted list. In the case of shared hosting, it can have no problems, or tons of problems depending on the server cluster’s reputation. The problems I am talking about is on a brand new server with a brand new IP address. I never get those to go through to hotmail and yahoo, while gmail is just fine. Though, if I use Google servers to send the mail with the above method, I have no problems at all.
Hey Marc, are you using the SwiftMailer because you couldn’t get the EmailComponent to work with Gmail or does it impact the performance?
What about PHPmailer, any thoughts?
Lucas: I used swiftmailer because I couldn’t get the internal component to work.
How can I send embeded images with this component?
@Heath Nail:
1st: Thank you, Thank you, Thank you! The “<” and “>” bracket hint was immeasurable!
Now let me help you – and others.
2nd: CakePHP’s Email Component requires Email→bcc and Email→cc to be arrays, not strings.
Thanks Marc, wonderful post. What do I need to do to use Google’s SMTP server?
Thanks!
Great post, and moreover, fantastic comments. I was about to switch to something like PHPMailer or this SwiftMailer, even though I need only to send one mail, and even then I don’t really need to use gmail, as it will be on a server with it’s own smtp. But for local testing, I needed to see if all was well and was therefore using the standard Email component.
The comment about wrapping email addresses in <> was a real life saver, as a newbie to the whole cakephp email thing could easily lose half a day or more chasing his/her tail around. Some of the documentation is really bad concerning this, for instance cakephp 1.3 book tells you to set a variable smtp-errors, which simply isn’t going to work with that hyphen in it.
Anyway thanks to all on this post, and good luck! :)
Thanks
Good answer that helped me a lot.
Soumavo
Thanks to Heath Nail… here is a fully working CakePHP Gmail SMTP send email script to go in your controller:
http://pastebin.com/eetvDxye