我使用PHPMailer类发送信件,我使用gmail帐户作为smtp服务器。 问题是有时它运作良好,访问者得到字母,但有时没有,并告诉我错误“消息体是空的”或类似的。 我认为如果代码有问题,所以不要为网站访问者发送任何信件。 为什么这样开心?什么可能有问题?
答案 0 :(得分:1)
更改这些行:
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Port = 465;
使用:
$mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
$mail->Port = 587;
希望从现在开始一切顺利。
答案 1 :(得分:0)
我的代码是
function mail_send($type, $id, $mailadd, $maillname) {
global $lang;
global $site_adress;
global $pavadinimas;
global $tekstas;
global $ivadas;
global $full_date;
global $short_date;
require_once ('includes/phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$siteuser_name = SQL_ROW("users", "WHERE user_id='1'", "user_name");
$siteadminemail = SQL_ROW("users", "WHERE user_id='1'", "user_email");
$alttext = users_langs(altmailtext);
if ($type == "mailactiveletter") {
$subj = users_langs("mailactivesubject");
$linktur = "$site_adress/$lang/usermail_$type-$id.html#kat";
// var_dump($linktur,$type, $siteuser_name,$siteadminemail);
}
if ($type == "mailchangeadress") {
$subj = users_langs("prisijduomenupriminimas");
$linktur = "$site_adress/$lang/usermail_$type-$id.html#kat";
}
$body = file_get_contents("$linktur");
$body = eregi_replace("[\]", '', $body);
define('GUSER', 'uxxxx@gmail.com'); // Gmail username
define('GPWD', 'xxx'); // Gmail password
$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 = GUSER;
$mail->Password = GPWD;
$mail->CharSet = 'UTF-8';
//$mail->AddReplyTo("$siteadminemail", "$siteuser_name");
$mail->AddReplyTo("no-replay@xxx.co.uk", "$siteuser_name");
$mail->AddAddress("$mailadd", "$maillname");
// $mail->SetFrom("$siteadminemail", "$siteuser_name");
$mail->SetFrom("no-replay@xxx.co.uk", "$siteuser_name");
// $mail->AddReplyTo("$mailadd", "$maillname");
$mail->AddReplyTo("no-replay@xxx.co.uk", "$siteuser_name");
$mail->Subject = "$subj";
$mail->AltBody = "$alttext $linktur";
// optional, comment out and test
$mail->MsgHTML($body);
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "";
}
}
答案 2 :(得分:0)
添加$ mail->发件人属性。
例如: $ mail->发件人与$ mail->相同的价值来自和我添加两行。
$ mail-> From =" noreply@test.com" ;; $ mail-> Sender =" noreply@test.com" ;;