PHPmailer,使用Gmail SMTP从localhost发送

时间:2014-09-13 11:23:39

标签: php email phpmailer

我正在尝试使用带有Gmail SMTP的localhost中的PHPmailer发送邮件。它不起作用。有时我收到谷歌已阻止某人试图登录我的帐户的电子邮件。有时不是。但电子邮件没有发送。

我的代码:

require("include/gmail-smtp.php");
                try{

                $mail->From = 'xxx@gmail.com';
                $mail->FromName = 'xxx';
                $mail->addAddress($email, $username);
                $mail->addReplyTo('xxx@gmail.com', 'xxx');
                $mail->WordWrap = 50;
                $mail->isHTML(true);
                $mail->Subject = 'Using PHPMailer';
                $mail->Body    = 'Hi Iam using PHPMailer library to sent SMTP mail from localhost';
                $mail->send();


                echo 'Message has been sent';
            } catch (Exception $e) {
                echo "There was an error, please try again later. <br>";
                echo $e;
            }   

这是gmail-smtp.php

    <?php 
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->host="ssl://smtp.gmail.com";
$mail->port=465;
$mail->SMTPAuth = true;
$mail->Username = 'xxx@gmail.com';
$mail->Password = 'xxx';
?>

0 个答案:

没有答案