我使用phpgmailer
发送电子邮件,但工作顺利。今天我测试了我的项目,现在还没有用。
<?php
require_once('class.phpgmailer.php');
$mail = new PHPGMailer();
$mail->Username = 'username@gmail.com';
$mail->Password = '********';
$mail->From = 'username@gmail.com';
$mail->FromName = "<blah>";
$mail->Subject = 'something';
$mail->AddAddress('xyz@gmail.com');
$mail->Body = "Hello Sir"."\n"."
Your Password is : ."."";
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Send();
if(!$mail->Send())
{
echo 'Message could not be sent.' ;
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
答案 0 :(得分:1)
我建议您下载PHPMailer并尝试以下代码:
require("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "ssl://smtp.gmail.com";
$mail->Port = 465;
$mail->Username = "from@gmail.com";
$mail->Password = "****";
$mail->FromName = "Sender name";
$mail->Subject = "test";
$mail->Body = "Test body";
$mail->AddAddress('sender@mail.com');
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
echo "Message has been sent";
}
答案 1 :(得分:0)
@goose是对的,删除第一个$mail->Send()
并将其留在if语句中。如果From
地址与用户名电子邮件相同,那么您不需要它,因为它将从您的Gmail帐户中获取。
试一试,看看它是否有效。
编辑:尝试添加以下内容;
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 0; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = 'email@gmail.com';
$mail->Password = 'password';
尝试一下,如果有错误,那么它应该会给你更多细节。
EDIT2:如果这不起作用,那么我建议尝试使用PHPMailer而不是PHPGmailer - 并按照以下教程进行操作:http://www.web-development-blog.com/archives/send-e-mail-messages-via-smtp-with-phpmailer-and-gmail/
答案 2 :(得分:0)
请确保:
$mail->Username
等于$mail->From
,并且是正确的$mail->Password
是正确的$mail->FromName
不包含“&lt;”和“&gt;”字符;试试$mail->FromName = 'Test';