带梨库的smtp电子邮件无法正常显示错误

时间:2018-04-13 13:36:27

标签: php email smtp pear

我的代码如下,

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 = "\n";
    $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;

我也尝试端口587,但它总是给我以下错误,

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

如果我删除&#39;超时&#39; =&GT;从上面的代码20它给我错误

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

请帮我解决上述问题

1 个答案:

答案 0 :(得分:1)

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

您可能必须允许&#34;不太安全的应用&#34;在使用过的谷歌帐户上。