10/10/2018, 09:24
Hỏi về PHPMailer và SMTP
Mình mới down PHPMailer về và test thử chức năng gửi mail bằng smtp của nó, đây là vì dụ đi kèm của nó (mình có thêm 1 chút), nhưng gửi ko được và cũng không báo lỗi gì hết, account email(smtp,user,pass) của mình đã thử gửi thành công trên Outlook. Mong các bạn coi giúp mình nó sai chỗ nào.
Code:
<?php
//error_reporting(E_ALL);
error_reporting(E_STRICT);
date_default_timezone_set('America/Toronto');
//date_default_timezone_set(date_default_timezone_get());
include_once('class.phpmailer.php');
include_once('phpmailer.lang-en.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[]",',$body);
$mail->Mailer ="smtp";
$mail->SMTPSecure="tls"; // telling the class to use SMTP
$mail->Host = "mail.domain.com"; // SMTP server
$mail->SMTPAuth = true;
$mail->Username ="admin+domain.com";
$mail->Password ="UxCk8zKfGM";
$mail->From = "admin@domain.com";
$mail->FromName = "First Last";
$mail->Subject = "Test";
//$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->AddAddress("***@gmail.com", "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
Bài liên quan





=> Bạn thêm port cho nó xem sao:?
$mail->Port = 587;
Code của mình sử dụng bạn có thể xem (Mình xài SMTP của Gmail)
$dirname = dirname(__FILE__); require_once $dirname.'/mailer/class.phpmailer.php'; $mail = new PHPMailer(); $mail->SetLanguage('en', $dirname.'/mailer/language/'); $body = $meg; $body = str_replace("\n", "<br>", $body); $order = array("\r\n", "\n", "\r", "<br />"); $replace = '<br />'; $body = str_replace($order, $replace, $body); $body = eregi_replace("[\]",'',$body); $order = array("\r\n", "\n", "\r", "<br />"); $replace = '<br />'; $body = str_replace($order, $replace, $body); $mail->IsSMTP(); $mail->SMTPAuth = true; // enable SMTP authentication $mail->SMTPSecure = "ssl"; // sets the prefix to the servier $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server $mail->Port = 465; // set the SMTP port for the GMAIL server $mail->Username = "username@gmail.com"; // GMAIL username $mail->Password = "password"; // GMAIL password $mail->AddReplyTo($from, "Administartor"); $mail->From = $from; $mail->FromName = "Administartor"; $mail->Sender = $from; $mail->Subject = $subject; $mail->AltBody = $meg; // optional, comment out and test $mail->WordWrap = 50; // set word wrap $mail->MsgHTML($body); $mail->AddAddress($to); $mail->IsHTML(true); // send as HTML if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { //echo "Message sent!{$from}"; }Bạn thử chuyển:
error_reporting(E_STRICT);
sang thành:
error_reporting(E_ALL);
Để coi bị báo lỗi gì để biết đường khắc phục.
Khi mình sử dụng gmail thì nó báo lỗi:
Nếu không thì bạn vào www.inbox.com đăng ký một tài khoản, nó cho SMTP đó. Đặc biệt là nó cũng không bị dính Blacklist ở đâu hết nên gởi toàn vào Inbox và nhanh lắm