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!";
 }
 
 ?>
Shellingfox viết 11:35 ngày 10/10/2018
$mail->SMTPSecure="tls";
=> 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)

Code:
	$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}";
	}
ttn47 viết 11:38 ngày 10/10/2018
CHo mình hỏi để chạy cai PHPMailer này thì có yêu cầu gì về host không, hoặc là phải chỉnh trong PHP hay Apache gì đó. Mình test thử trên localhost và trên host đều không được
Shellingfox viết 11:30 ngày 10/10/2018
Mình test trên local của mình thì bình thường không có chuyện gì hết. Và kể cả trên live host.

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.
LinuXpert viết 11:33 ngày 10/10/2018
Có thể host của bạn không cho phép kết nối SMTP ra ngoài để ngăn spam, bạn dùng thử SMTP trên chính server đó trước xem được không?
ttn47 viết 11:29 ngày 10/10/2018
Mình sử dụng đoạn code của Shellingfox
Khi mình sử dụng gmail thì nó báo lỗi:
Code:
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:465 (Unable to find the socket transport "ssl" - did you forget to enable it when you configured PHP?) in C:\xampp\htdocs\mail\class.smtp.php on line 122
Mailer Error: SMTP Error: Could not connect to SMTP host.
Khi sử dụng tài khoản email của host cấp thì nó báo lỗi
Code:
Notice: Undefined variable: extra in C:\xampp\htdocs\mail\class.smtp.php on line 173

Warning: stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in C:\xampp\htdocs\mail\class.smtp.php on line 194
Mình sử dụng XAMMP chạy trên localhost.
Shellingfox viết 11:35 ngày 10/10/2018
Do xampp của bạn chưa có config OpenSSL cho PHP. Bạn thử enable lên xem sao?

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
ttn47 viết 11:40 ngày 10/10/2018
Mình enable OpenSSL bằng cách bỏ dấu ';' extension=php_curl.dll trong file php.ini ko biết có đúng ko. Nó vẫn báo lỗi như cũ. hic
lovelycesar viết 11:33 ngày 10/10/2018
Bạn muốn enable OpenSSL thì cài thêm OpenSSL trên host, theo mình biết thì bộ XAMMP cho Windows không có SSL. Cài SSL trên Windows thì mình chưa thử, nếu bạn dùng trên Unix hay Linux thì bạn cho biết distro bạn đang dùng.
Code:
Mình enable OpenSSL bằng cách bỏ dấu ';' extension=php_curl.dll trong file php.ini ko biết có đúng ko. Nó vẫn báo lỗi như cũ.
Vậy là bạn dùng OS là Windows rồi. php_curl có liên quan đến việc rewrite url thôi.
ttn47 viết 11:39 ngày 10/10/2018
Mình làm được rồi, cám ơn bạn nhiều nha
Bài liên quan
0