PHPMailer不起作用

时间:2016-08-25 08:22:52

标签: phpmailer

require_once('class.phpmailer.php');
$mail = new PHPMailer();

$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure="tls";
$mail->Host = "smtp.mail.yahoo.com";
$mail->Port = 587;
$mail->Username = "********@yahoo.com";
$mail->Password = "*****";

$mail->SetFrom('*********@yahoo.com', 'my name');
$mail->Subject = "A Transactional Email From Web App";
$mail->MsgHTML('text');
$mail->AddAddress('*********@yahoo.com', 'my name');

if($mail->Send()) {
    echo "Message sent!";
}else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}

因此,我尝试使用PHPMailer发送电子邮件,但代码不起作用,localhost页面不起作用。我将class.phpmailer.php和class.smtp.php复制到/ var / www / html文件夹(在我指定require_once(/ usr / share / ...)中的路径之前)。我启用了ssl扩展。我使用Ubuntu 16.04并安装了libphp-phpmailer。我该怎么办? (使用smtp发送邮件的任何选项都是apreciated,已经尝试过:postfix和sendmail)。

2 个答案:

答案 0 :(得分:0)

浏览以上评论链接.. !!

试试这个

<?php
include "PHPMailer_5.2.4/class.phpmailer.php"; 
$mail = new PHPMailer(); 

$mail->IsSMTP(); 
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com";

$mail->Port = 465; 
$mail->IsHTML(true);
$mail->Username = "******@gmail.com";
$mail->Password = "*******";

$mail->SetFrom('aswad@yahoo.com', 'my name');
$mail->Subject = "A Transactional Email From Web App";
$mail->MsgHTML('text');
$mail->AddAddress('aswad@yahoo.com', 'my name');

if($mail->Send()) {
    echo "Message sent!";
}else {
    echo "Mailer Error: " . $mail->ErrorInfo;
}
?>

答案 1 :(得分:0)

尝试评论

$mail->IsSMTP();

还要检查SELinux配置

关于外壳类型

sestatus -b | grep sendmail

如果看到 httpd_can_sendmail关闭

只需键入以下两行:

  1. restorecon / usr / sbin / sendmail
  2. setsebool -P httpd_can_sendmail 1