2015-06-20 01:03
浏览 63


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

    $mail = new PHPMailer;

    $mail->Host = '';
    $mail->SMTPAuth = true;
    $mail->Username = '';
    $mail->Password = '*********';
    $mail->Port = 25;

    $mail->From = '';
    $mail->FromName = 'Shamir Towsif';
    $mail->addAddress('', 'Shamir Towsif');
    $mail->addReplyTo('', 'Information');


    $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  =''; 
 $ mail-&gt; SMTPAuth = true; 
 $ mail-&gt;用户名=''; 
 $ mail-&gt;密码='*  ********'; 
 $ mail-&gt; Port = 25; 
 $ mail-&gt; From =''; 
 $ mail-&gt; FromName  ='Shamir Towsif'; 
 $ mail-&gt; addAddress('','Shamir Towsif'); 
 $ mail-&gt; addReplyTo('',  '信息'); 
 $ 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“Mailer Error:”。  $ mail-&gt; ErrorInfo; 

这是我得到的错误 。

 无法发送消息。 邮件程序错误: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,

    解决 无用
    打赏 举报
  • 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 ( 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 = ''; $mail->Port = 587; $mail->SMTPSecure = 'tls'; $mail->SMTPAuth = true; $mail->Username = ""; $mail->Password = "password"; $mail->setFrom('', 'Name'); $mail->addAddress('', '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:

    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:

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

    解决 无用
    打赏 举报

相关推荐 更多相似问题