3

Please note that I'm using a google apps account, NOT a gmail account. I'm trying to simply send an email using my google apps account with php. I am able to send an email in a .net application using the port 587 host smtp.googlemail.com and SSL enabled. The username is my full email address.

require_once('PHPMailer_v5.1\class.phpmailer.php');

try {
    $mail  = new PHPMailer();
    $mail->Mailer   = 'smtp';
    $mail->SMTPSecure = 'tls';
    $mail->Host     = $host;
    $mail->Port     = 587;
    $mail->SMTPAuth = true;
    $mail->Username = $from;
    $mail->Password = $password;

    $mail->AddAddress($to, $to_name);   
    $mail->From       = $from;
    $mail->FromName   = $from_name;
    $mail->Subject    = $subject;
    $mail->MsgHTML($body);
    $mail->IsHTML(true);

    $mail->Send();
} catch (phpmailerException $e) {
    echo $e->errorMessage();
} catch (Exception $e) {
    echo $e->getMessage();
}

Haven't been able to get this to work, but I've tried several different variations of this.

$mail->SMTPSecure = 'ssl'; 
$mail->Port     = 465;
// Error: Could not connect to SMTP host. This is expected as this isn't supported anymore.

$mail->SMTPSecure = 'tls';
$mail->Port     = 587;
// Takes forever, then I get "this stream does not support SSL/crypto PHPMailer_v5.1\class.smtp.php"

I don't care how, but I need to send an email using gmail here. It can be with this library or a different one.

Chris
  • 2,341
  • 5
  • 23
  • 21
  • Are you able to telnet into the googlemail server from your server? `telnet smtp.googlemail.com 587` or `telnet smtp.googlemail.com 465` as mentioned in the Google Mail documentation http://mail.google.com/support/bin/answer.py?hl=en&answer=78775 – Treffynnon Mar 14 '11 at 16:15
  • I can connect to both of those using telnet from the server. – Chris Mar 14 '11 at 16:18
  • Looks like you're using the wrong secure method (should be SSL) and the wrong port (should be 465). – Bart S. Mar 14 '11 at 16:57

1 Answers1

5

This is what we use elsewhere on our website and it works fine.

    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }

Edit Also - We have moved away from PHPMailer and have started using SwiftMailer, which also works with gmail (http://www.swiftmailer.org/wikidocs/v3/connections/smtp)

Kevin
  • 512
  • 4
  • 15