dqcwl02022 2018-04-13 13:36
浏览 55

与梨库的smtp电子邮件无法正常工作给出错误

my code is as below,

include('../include/pear/Mail.php');
include('../include/pear/Mail/mime.php');



$from = "myemailaddress@domail.com";
    $to = "myfriendemailaddress@domail.com"; 
    $subject = 'Weekly Summary';
    $headers = array('From' => $from, 'To' => $to, 'Subject' => $subject);
    $html = '<html><body>Hello,<br/>Please find attached file</body></html>';
    $file = $filepath.$filename;
    $crlf = "
";
    $mime = new Mail_mime($crlf);
    $mime->setHTMLBody($html);
    $mime->addAttachment($file, 'application/octet-stream');
    $body = $mime->get();
    $headers = $mime->headers($headers);
    $host = "smtp.gmail.com";
    $username = "username@gmail.com";
    $password = "password";
    $port = 465;
    $smtp = Mail::factory('smtp', array(
                'host' => $host,
                'auth' => false,
                'port' => $port,
                'username' => $username,
                'password' => $password,
                'timeout' => 20,
                'debug' => false,
                'persist' => true));
    $mail = $smtp->send($to, $headers, $body);
    if (PEAR::isError($mail)) {
        echo("<p>" . $mail->getMessage() . "</p>");
    } else {
        echo("<p>Message successfully sent!</p>");
    }
    echo "completed";
    die;

I have also try port 587 but it always gives me below error,

Failed to connect to smtp.gmail.com:465 [SMTP: Invalid response code received from server (code: -1, response: )]

if I remove 'timeout' => 20 from above code it gives me error

Failed to connect to smtp.gmail.com:465 [SMTP: Failed to connect socket: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (code: -1, response: )]

Please help me to resolve above issue

  • 写回答

1条回答 默认 最新

  • duan198727 2018-04-13 13:38
    关注

    https://support.google.com/accounts/answer/6010255?hl=en

    You probably have to allow "Less Secure Apps" on the used google account.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?