Can't send an email using a google apps account with PHPMailer

岁酱吖の 提交于 2019-12-22 09:34:32

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/5301213/cant-send-an-email-using-a-google-apps-account-with-phpmailer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!