使用PHPMailer通过SMTP

时间:2016-09-13 20:36:22

标签: php email smtp phpmailer

当我设置$mail->isSMTP();时,我已经了解了为什么我的PHP脚本没有发送任何电子邮件。

在我遇到问题之前,我想指出我使用mailgun.org作为我的SMTP服务器。我的PHP脚本很简单,我有一个HTML表单,可以将数据转发到PHP文件,然后PHP文件调用PHPMailer脚本并发送电子邮件。

以下是我的PHP脚本的代码:

date_default_timezone_set('Asia/Manila');

require 'phpmailer/PHPMailerAutoload.php';
// include 'phpmailer/class.phpmailer.php';
// require 'phpmailer/class.smtp.php';

$mail = new PHPMailer;
//$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.mailgun.org";
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = ""; //Username removed
$mail->Password = ""; //Password removed
$mail->setFrom('', ''); //Email address and Name removed
$mail->addReplyTo($userMail, $firstName);
$mail->addAddress('emailOne@example.com', 'Name'); //Actual Values Changed
$mail->addAddress('emailTwo@example.com', 'Name'); //Actual Values Changed
$mail->addAddress('emailThree@example.com', 'Name'); //Actual Values Changed
$mail->Subject = 'New Application for website by '. $firstName;
$mail->msgHTML($theMessage);
$mail->AltBody = $theMessage;
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else { //Some HTML code here
}

现在,正如您所看到的,我已经注释了$mail->isSMTP();行。这是因为每当我尝试使用它时,代码都不会加载,经过漫长的等待会产生500错误。 (遗憾的是,通过cPanel设置不允许我查看apache日志)。

我尝试仅导入class.phpmailer.php文件,但这给了我一个致命错误,即未定义类SMTP,这是预期的。然后我添加了class.smtp.php文件,它给了我相同的500错误。

我浏览了一下StackOverflow并遇到了this answer,但它对我的情况没有帮助。

我查看了Mailgun的日志,但是即使尝试连接,它们也没有脚本记录。

Additional Information: If it matters, I have the following files in the same directory:
- sendMail.php //The script above
- class.phpmailer.php
- class.smtp.php
- PHPMailerAutoload.php
- index.html //Not important in this situation.

我希望有人能够帮助我,我现在依靠非smtp方法。 :/

1 个答案:

答案 0 :(得分:0)

试试这个工作正常..!

date_default_timezone_set('Asia/Manila');

require 'phpmailer/PHPMailerAutoload.php';
// include 'phpmailer/class.phpmailer.php';
// require 'phpmailer/class.smtp.php';

$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; 
$mail->Username = ""; //Username removed
$mail->Password = ""; //Password removed
$mail->addReplyTo($userMail, $firstName);
$mail->SetFrom('emailOne@example.com', 'my name');
$mail->Subject = 'New Application for website by '. $firstName;$mail->MsgHTML($theMessage);
$mail->AddAddress('aswad@yahoo.com', 'my name');
if($mail->Send()) {
    echo "Message sent!";
}else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
相关问题