微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 如何从localhost发送smtp邮件

尝试从我的本地主机发送电子邮件时,我总是收到此消息.

SMTP错误:无法连接到SMTP主机.无法发送消息.
邮件程序错误:SMTP错误:无法连接到SMTP主机.

以下是我的代码:请帮忙

<?PHP

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require("class.PHPmailer.PHP");

$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.PHP script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server

$mail->SMTPAuth = true;     // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer@bradm.inmotiontesting.com
// pass: password
$mail->Username = "project@reliable.com.pk";  // SMTP username
$mail->Password = "Nov112014"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("mani9418@gmail.com", "Usman Ali Siddiqui");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "You have received Feedback from your website Etutionhub!";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message Could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

解决方法:

a) Open the "PHP.ini". For XAMPP,it is located in C:\XAMPP\PHP\PHP.ini. Find out if you are using WAMP or LAMP server. Note : Make a backup of PHP.ini file

b) Search [mail function] in the PHP.ini file.

You can find like below.
[mail function]
; For Win32 only.
; http://PHP.net/smtp
SMTP = localhost
; http://PHP.net/smtp-port
smtp_port = 25


; For Win32 only.
; http://PHP.net/sendmail-from
;sendmail_from = postmaster@localhost


Change the localhost to the smtp server name of your ISP. No need to change the smtp_port. Leave it as 25. Change sendmail_from from postmaster@localhost to your domain email address which will be used as from address..

So for me, it will become like this.
[mail function]
; For Win32 only.
SMTP = smtp.example.com
smtp_port = 25
; For Win32 only.
sendmail_from = info@example.com


c) Restart the XAMPP or WAMP(apache server) so that changes will start working.

d) Now try to send the mail using the mail() function ,

mail("example@example.com","Success","Great, Localhost Mail works");

邮件将从localhost发送到“example@example.com”,主题行为“Success”,正文为“Great,Localhost Mail”

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐