无法使用 Google Apps 帐户与 PHPMailer 发送电子邮件

发布于 2024-10-21 14:24:16 字数 1284 浏览 1 评论 0原文

请注意,我使用的是 Google Apps 帐户,而不是 Gmail 帐户。我正在尝试使用我的 google apps 帐户和 php 简单地发送电子邮件。我可以使用端口 587 主机 smtp.googlemail.com 并启用 SSL 在 .net 应用程序中发送电子邮件。用户名是我的完整电子邮件地址。

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();
}

还没能让它发挥作用,但我已经尝试了几种不同的变体。

$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"

我不在乎如何,但我需要在这里使用 gmail 发送电子邮件。它可以与此库或不同的库一起。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2024-10-28 14:24:16

这是我们在网站其他地方使用的,效果很好。

    $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;
    }

编辑另外 - 我们已经不再使用 PHPMailer 并开始使用 SwiftMailer,它也适用于 gmail (http://www.swiftmailer.org/wikidocs/v3/connections/smtp)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文