我想在用户从购物车结账后发送电子邮件
我的控制器:
include('js/phpmailer/PHPMailerAutoload.php');
$mail = new PHPMailer();
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server Gmail
$mail->Mailer = "smtp";
$mail->Port = 465;
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPDebug = 1;
$mail->Username = "my gmail"; //
$mail->Password = "my pass"; // SMTP password
$webmaster_email = "my gmail"; //Reply to this email ID
$email = "recipient gmail"; // Recipients email ID
$name = "John"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Aryono King";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Goeboek I-Mut");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Subject Test";
$mail->Body = "Test Content"; //HTML Body
if(!$mail->Send()) {echo "Mailer Error: " . $mail->ErrorInfo;}
else {echo "<strong>Email Send</strong>";}
但是它显示了这样的错误
2015-05-20 21:46:43 SMTP ERROR: Failed to connect to server: (0) 2015-05-20 21:46:43 SMTP connect() failed. Mailer Error: SMTP connect() failed.
有什么问题吗?我找遍了所有地方都找不到答案,谁来帮帮我吧
看一下这里的例子。
我个人是这样做的:
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'ssl://smtp.googlemail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = $email_mail; // SMTP username
$mail->Password = $email_pass; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
试着检查你的,例如:$mail->Host = 'ssl://smtp.googlemail.com';
,它应该是不具有ssl://
。
在为gmail使用phpmailer之前需要检查两件事
-
需要检查SMTP端口和email +密码是否正确
-
你需要从你的gmail帐户认证从不安全的来源发送电子邮件,这意味着:当你第一次发送电子邮件,谷歌会给你发一封电子邮件询问你的许可,允许从你的id转发电子邮件,这里你需要点击允许
还有一件事,端口587对我来说是完美的,而不是465
,你好有更多的…我看到在你的计算机上安装的防病毒或防火墙可能会阻止通过本地主机发送电子邮件,因为它可能被认为是垃圾邮件攻击
使用此代码…
include 'PHPMailerAutoload.php';
function send_mail($mail_to,$mail_to_fname,$mail_to_lname,$subject,$message)
{ $mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = 'mail.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = " YOUR GMAIL ";
$mail->Password = " YOUR GMAIL PASSWORD";
$mail->setFrom(' YOUR GMAIL ', ' YOUR NAME ');
$mail->addAddress($mail_to,$mail_to_fname . " " . $mail_to_lname);
$mail->Subject = $subject;
$mail->msgHTML($message);
$mail->AltBody = ' ';
if (!$mail->send()){echo "FALSE" . $mail->ErrorInfo;}
else{echo "TRUE";}
}
// How to user
// send_email(" email address where to send "," reciepient first name ","reciepient last name "," subject ", " message as html code ");
你需要再做一步允许gmail发送消息…!(*)接受允许谷歌从不安全的应用程序发送电子邮件,该链接将发送到您的gmail,在您发送第一封电子邮件