doushi3202
2015-06-20 01:03
浏览 63
已采纳

无法使用PHPMailer发送电子邮件

I am trying to send an email using phpmailer. This is the code that I have written.

    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'shamir.towsif@gmail.com';
    $mail->Password = '*********';
    $mail->Port = 25;

    $mail->From = 'shamir.towsif@gmail.com';
    $mail->FromName = 'Shamir Towsif';
    $mail->addAddress('shamir.towsif@gmail.com', 'Shamir Towsif');
    $mail->addReplyTo('shamir.towsif@gmail.com', 'Information');

    $mail->isHTML(true);

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

    if(!$mail->send()) {
        echo "Message could not be sent.
";
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

Here is the error that I am getting.

Message could not be sent. Mailer Error: SMTP connect() failed.

What am I doing wrong. The other questions in SO is not helping. Thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用phpmailer发送电子邮件。 这是我写的代码。

  $ mail = new PHPMailer; 
 
 $ mail-&gt; isSMTP(); 
 $ mail-&gt; Host  ='smtp.gmail.com'; 
 $ mail-&gt; SMTPAuth = true; 
 $ mail-&gt;用户名='shamir.towsif@gmail.com'; 
 $ mail-&gt;密码='*  ********'; 
 $ mail-&gt; Port = 25; 
 
 $ mail-&gt; From ='shamir.towsif@gmail.com'; 
 $ mail-&gt; FromName  ='Shamir Towsif'; 
 $ mail-&gt; addAddress('shamir.towsif@gmail.com','Shamir Towsif'); 
 $ mail-&gt; addReplyTo('shamir.towsif@gmail.com',  '信息'); 
 
 $ mail-&gt; isHTML(true); 
 
 $ mail-&gt; Subject ='这是主题'; 
 $ mail-&gt; Body ='这是 HTML邮件正文&lt; b&gt;以粗体显示!&lt; / b&gt;'; 
 $ mail-&gt; AltBody ='这是非HTML邮件客户端的纯文本正文'; 
 
 if(!$  mail-&gt; send()){
 echo“无法发送消息。
”; 
 echo“Mailer Error:”。  $ mail-&gt; ErrorInfo; 
}其他{
 echo'消息已发送'; 
} 
   
 
 

这是我得到的错误 。

 无法发送消息。 邮件程序错误:SMTP连接()失败。
   
 
 

我做错了什么。 SO中的其他问题没有帮助。 提前致谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douhuan1901 2015-06-22 16:39
    已采纳

    I am facing a similar problem, but I think you should try adding this to your code:

    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    

    This is PHPMailer recommended settings for GMail, you can see an example in their Github page .

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsm42026 2017-12-28 15:41

    Solved an almost identical problem, by adding these lines to the standard PHPMailer configuration. Works like a charm.

    $mail->SMTPKeepAlive = true;   
    $mail->Mailer = “smtp”; // don't change the quotes!
    

    All of the standard settings for SMTP using TLS and default settings for ports etc were being followed. Eventually, I came across this code (from Simon Chen) while researching a solution here, https://webolio.wordpress.com/2008/03/02/phpmailer-and-smtp-on-1and1-shared-hosting/#comment-89

    评论
    解决 无用
    打赏 举报
  • dsl36367 2018-08-26 12:31

    Title: Sending Email From Server Using PhpMailer & Gmail

    Here's how I solved a similar issue:

    1. Download PHPMAILER from github (https://github.com/PHPMailer/PHPMailer) to your computer.

    2. Upload it to your server as .zip file, then extract it by clicking the extract icon. Rename the folder to 'phpmailer' or any name you wish.

    3. Inside the mail sending .php file put the following codes:

      require 'phpmailer/src/Exception.php'; require 'phpmailer/src/PHPMailer.php'; require 'phpmailer/src/SMTP.php'; $mail = new PHPMailer\PHPMailer\PHPMailer(); $mail->isSMTP(); $mail->SMTPDebug = 2; $mail->Host = 'smtp.gmail.com'; $mail->Port = 587; $mail->SMTPSecure = 'tls'; $mail->SMTPAuth = true; $mail->Username = "sender@gmail.com"; $mail->Password = "password"; $mail->setFrom('sender@gmail.com', 'Name'); $mail->addAddress('receiver@yahoo.com', 'Name'); $mail->Subject = 'Subject'; $mail->Body = 'This is a plain-text message body'; if (!$mail->send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; }

    4. Save the php file.

    5. Open your browser and sign in to your gmail(google) account that you used in the php script.

    6. Go to this link: https://support.google.com/accounts/answer/6009563

    7. Expand Your device or app might not support Google’s security standards.

    8. Click Less secure apps.

    9. Toggle Allow less secure apps: to ON (if it is OFF).

    10. Expand 2-Step Verification is not supported by the app

    11. Click the link: https://accounts.google.com/DisplayUnlockCaptcha

    12. Click Continue

    13. Open the email sending php file in the browser.

    14. Now the email will be sent.

    This technique worked for me. I hope it'll work for you guys as well.

    Happy Coding :-)

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题