简体   繁体   English

无法使用PHPmailer或Swift Mailer发送邮件

[英]Cannot send mail using PHPmailer or swift mailer

When using PHPmailer or swift mailer to send mail through mail.google.com, both timeout after 30 seconds. 使用PHPmailer或swift mailer通过mail.google.com发送邮件时,两者均在30秒后超时。 The PHPmailer code is as follows 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;
}

Here is the swift mailer code: 这是快速的邮件程序代码:

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

Because both are timing out, I'm assuming both are installed correctly but I've set something up incorrectly. 由于两者都超时,因此我假设两者均已正确安装,但设置不正确。 Thanks in advance. 提前致谢。

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

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

Also make sure you've enabled "Less secure apps" for your account so that password authentication is enabled for Gmail. 另外,请确保您已为帐户启用“安全性降低的应用程序”,以便为Gmail启用密码身份验证。 By default it is NOT enabled, and only OAUTH authentication is allowed (which can't be used over SMTP). 默认情况下,它不启用,并且只允许OAUTH身份验证(不能通过SMTP使用)。 https://support.google.com/accounts/answer/6010255?hl=en https://support.google.com/accounts/answer/6010255?hl=zh_CN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM