PEAR::Mail and GMail

If you ever need to send email from PHP and don’t have access to a SMTP server, but do have a GMail account use this code as a template.

You will need:

  • PHP Pear::Mail class installed
  • A GMail account
  • Port 465 open to smtp.gmail.com in your firewall
<?php
include('Mail.php');
 
$from =     "user@host.com";
$to =       "user@host.com";
$subject =  "Your subject here";
$body =     "\n\nEmail contents here";
$host =     "ssl://smtp.gmail.com";
$port =     "465";
$username = "your_gmail_email@gmail.com";
$password = "yourGmailPassword";
 
$headers = array (
         'From' => $from,
         'To' => $to,
         'Subject' => $subject);
 
$smtp = Mail::factory('smtp',
      array (
            'host' => $host,
            'port' => $port,
            'auth' => true,
            'username' => $username,
            'password' => $password));
 
$mail = $smtp->send($to, $headers, $body);
 
if (PEAR::isError($mail)) die($mail->getMessage());
 
?>

A couple of important things to note with this code.

First off, its a template, at least have a basic understanding of what it does before you try to use it or ask a question.

Second, yes, its rough and the error handling is poor at best, see the first point.

Third, and if you’ve gotten this far, you probably knew this already, the $smtp->send() function will wait for the socket to close before continuing, so this would probably be best for queued or deferred processing of email.

  1. #1 by Anton on August 28, 2012 - 4:07 pm

    Hey,

    I am doing pretty much the same thing, except my $body is HTML and that seems to break it.
    I posted the full question on stackoverflow, still waiting for answers… maybe you can give me an insight? Thanks!
    http://stackoverflow.com/questions/12166468/sending-html-message-through-pear-while-using-smtp-authentication-returns-an-err

  2. #2 by jeffro on August 28, 2012 - 4:38 pm

    Have you verified your code works by sending a plain text email message first? I always like to get a baseline working, then build from there. It cuts down on the number of variables that could be the culprit.

  3. #3 by Anton on August 28, 2012 - 4:43 pm

    Thank for the reply Jeff!
    Here is what I’ve tried to see what is going wrong:
    1. Using the same script using ‘Mail’ instead of ‘smtp’ i.e.
    $smtp = Mail::factory(‘Mail’);
    This works just fine but I want to use SMTP.
    2. Using the same script w/o the mime.php, this also works while sending the plain text but doesn’t allow me to send an HTML email.
    Any Ideas?

  4. #4 by jeffro on August 28, 2012 - 5:04 pm

    @Anton, did you see this post on StackOverflow..

    http://stackoverflow.com/questions/4535209/how-to-use-pear-mail-mime

    Looks like they had the same issue when using

    $mime = new Mail_mime($crlf);
    they tried
    $mime = new Mail_mime(array('eol' => $crlf));

    and also swapped

    $body = $mime->get();
    for this
    $body = $mime->getMessageBody();

    and it worked. Hope that helps.

  5. #5 by Anton on August 28, 2012 - 5:18 pm

    Haven’t seen that one, but still no luck…
    Thanks again!

  6. #6 by George Fisher on March 8, 2013 - 6:17 pm

    Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: Connection timed out (code: -1, response: )]

  7. #7 by jeffro on March 16, 2013 - 1:22 pm

    @ George Fisher

    No clue George, the code works in my environment even today so I know Google didn’t change anything (which they do with out warning a lot). Have you tried telnetting to that address to ensure you actually have connectivity? If that works then you did something wrong in the code. If not, then its a firewall issue.

    It should give you something like the following:

    #>telnet smtp.gmail.com 465
    Trying 173.194.73.108…
    Connected to gmail-smtp-msa.l.google.com.
    Escape character is ‘^]’.

  8. #8 by Stuart on November 17, 2013 - 8:29 am

    For anyone having issues with gmail please remember that you need to set up and use an application specific password, not the general one you use for your account.

(will not be published)