我想从我的localhost发送一封包含mail.php文件的电子邮件。
这是我的test.php文件代码:
<?php
include_once("Mail.php");
$From = "Sender's name <testing123xyz@gmail.com>";
$To = "Recipient's name <test2@gmail.com>";
$Subject = "Send Email using SMTP authentication";
$Message = "This example demonstrates how you can send email with PHP using SMTP authentication";
$Host = "mail.gmail.com";
$Username = "testing123xyz";
$Password = "testing";
// Do not change bellow
$Headers = array ('From' => $From, 'To' => $To, 'Subject' => $Subject);
$SMTP = Mail::factory('smtp', array ('host' => $Host, 'auth' => true,
'username' => $Username, 'password' => $Password));
$mail = $SMTP->send($To, $Headers, $Message);
if (PEAR::isError($mail)){
echo($mail->getMessage());
} else {
echo("Email Message sent!");
}
?>
当我点击test.php文件的url时,服务器抛出以下错误:
无法连接到smtp.gmail.com:25 [SMTP:无法连接套接字:无法建立连接,因为目标计算机主动拒绝它。 (代码:-1,响应:)]
请帮帮我。 感谢。
答案 0 :(得分:1)
从github下载并使用phpmailer:https://github.com/PHPMailer/PHPMailer 另外,请查看本教程,它将对您有所帮助:http://phpmailer.worxware.com/?pg=tutorial
这是发送电子邮件的PHP代码:
<?php
function email($recipient_email_id,$senders_name){
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
//IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sender@gmail.com"; // SMTP username
$mail->Password = "xxx"; // SMTP password
$webmaster_email = "reply@gmail.com"; //Reply to this email ID
$email = "$recipient_email_id"; // Recipients email ID
$name = "$senders_name"; // sender's's name
$mail->From = $webmaster_email;
$mail->FromName = $name;
$mail->AddAddress($email, $name);
$mail->AddReplyTo($webmaster_email, "Name");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message has been sent";
}
}
?>
答案 1 :(得分:0)
我的猜测是你使用的是localhost。尝试使用Test Mail Server
编辑截至此link,您使用的是错误的主机。将其更改为smtp.gmail.com