问题
I am being troubled from the send mail function using phpmailer running at localhost.
I use XAMPP, phpmailer
The $mail->IsSMTP()
or $mail->IsSendmail()
is working well on the hosting, but not localhost.
I had tried many solution mentioned on the internet, such as enabled the OPENSSL (extension=php_openssl.dll)
For using $mail->IsSendmail() of phpmailer, it shown "Could not execute: /usr/sbin/sendmail".
For using $mail->IsSMTP() with well configured, all are setting correctly.
It shows
"SMTP Error: Could not authenticate.",
I had the info from its debugger, there is info "...SMTP -> ERROR: Password not accepted from server:..."
But those had no problem on the live hosting environment, just only not work at localhost.
Hope someone can give me some idea. Many Thanks.
回答1:
isSendmail
is unlikely to work on Windows - use isMail
if you have a local mail server installed, and if you do, make sure it's up and running (e.g. telnet localhost 25
). isSMTP
sends directly and is unaffected by local mail config, which is why it's working for you. You should also read the troubleshooting docs.
回答2:
Download & INSTALL PHPMailer
https://github.com/PHPMailer/PHPMailer Extract class.phpmailer.php and class.smtp.php uncomment ;extension=php_openssl.dll in php.ini normaly located under xampp/php/ folder find [mail function] and change (for Microsoft)
SMTP=smtp.gmail.com
smtp_port=465
sendmail_from = yrmailaddress@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Edit class.smtp.php, host and port parameters like (for Linux): $host="ssl://smtp.gmail.com" $port=465
[edit delete sendmail params]
Example code for sending mail:
<?php
require_once "phpmailer/class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "yourusername@gmail.com"; // Enter your SMTP username
$mail->Password = "yourpassword“; // SMTP password
$webmaster_email = "yourusername@yourdoamin.com"; //Add reply-to email address
$email="yourusername@domain.com"; // Add recipients email address
$name="name"; // Add Your Recipient’s name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = <strong>50</strong>; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is your subject";
$mail->Body = "Hi, this is your email body, etc, etc" ; //HTML Body
$mail->AltBody = "Hi, this is your email body, etc, etc"; //Plain Text Body
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
?>
来源:https://stackoverflow.com/questions/28260634/phpmailer-not-working-running-from-localhost-xampp