【问题标题】:Cannot send mail using PHPmailer or swift mailer无法使用 PHPmailer 或 swift mailer 发送邮件
【发布时间】:2018-06-20 19:59:52
【问题描述】:

当使用 PHPmailer 或 swift mailer 通过 mail.google.com 发送邮件时,两者都会在 30 秒后超时。 PHPmailer代码如下

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'mail.google.com';            // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[my email]@gmail.com';                 // SMTP username
    $mail->Password = '[password]';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[my email]@gmail.com', 'Adam Johnston');
    $mail->addAddress('[my email]@gmail.com', 'Adam Johnston');     // Add a recipient
    //$mail->addAddress('ellen@example.com');               // Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

这里是快速邮件代码:

require_once './vendor/autoload.php';

// Create the Transport
$transport = (new Swift_SmtpTransport('mail.google.com', 465))
  ->setUsername('[my email]@gmail.com')
  ->setPassword('[password]')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$message = (new Swift_Message('Wonderful Subject'))
  ->setFrom(['[my email]@gmail.com' => 'Adam Johnston'])
  ->setTo(['[my email]@gmail.com' => 'Adam Johnston'])
  ->setBody('Here is the message itself')
  ;

// Send the message
$result = $mailer->send($message);

因为两者都超时,我假设两者都安装正确,但我设置不正确。提前致谢。

【问题讨论】:

    标签: php xampp smtp phpmailer swiftmailer


    【解决方案1】:

    对于 gmail,SMTP 服务器(Host 字段)应为 smtp.gmail.com

    【讨论】:

    • 这给出了以下错误:未捕获的 Swift_TransportException:无法与主机 smtp.google.com 建立连接 [php_network_getaddresses: getaddrinfo failed: No such host is known。 #0]
    • 错误消息提到 smtp.google.com,而不是 smtp.gmail.com
    • 哇,我是个白痴。尽管如此,这给了我以下信息:致命错误:未捕获的 Swift_TransportException:预期响应代码 250,但得到代码“530”,消息“530 5.7.0 必须先发出 STARTTLS 命令。e4-v6sm1491895oti.75 - gsmtp”
    • 没关系。我通过更改为 TLS 并将端口更改为 587 来修复它。感谢您的帮助。
    【解决方案2】:

    我通过更改为 TLS 并将端口更改为 587 来修复它。感谢 Jiri 指出我愚蠢的错别字。

    【讨论】:

      【解决方案3】:

      同时确保您已为您的帐户启用“安全性较低的应用程序”,以便为 Gmail 启用密码验证。默认情况下未启用,仅允许 OAUTH 身份验证(不能通过 SMTP 使用)。 https://support.google.com/accounts/answer/6010255?hl=en

      【讨论】:

      • 您不必这样做。 OAuth 可用于电子邮件。设置起来很痛苦,但它确实有效,而且 PHPMailer 支持它,请参阅有关它的示例和文档。
      猜你喜欢
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-18
      • 2016-08-14
      • 2015-09-06
      • 2020-11-25
      相关资源
      最近更新 更多