一些主机限制了每分钟/小时/天可以发送多少消息。
为了解决这个问题,我设置了第二个 Gmail 帐户以使用PHPMailer从脚本发送消息,然后制作了这个脚本(称为mail.php
):
<?php
include_once 'phpmailer/class.phpmailer.php';
function do_mail($from, $name, $to, $subject, $message, $debug = false) {
$blah = base64_decode('base64-encoded password here');
$mail = new PHPMailer();
$mail->IsSMTP();
if($debug) $mail->SMTPDebug = 2;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'username@gmail.com';
$mail->Password = $blah;
$mail->SetFrom($from, $name);
$mail->AddAddress($to, $to);
$mail->Subject = $subject;
$body = $message;
$mail->MsgHTML($body);
$mail->AltBody = $message;
if($mail->Send()) {
return true;
} else {
return $mail->ErrorInfo;
}
}
?>
然后,发送消息:
<?php
include_once 'mail.php';
$result = do_mail('username@gmail.com', 'First Last', 'someone@example.com', 'Subject here', 'message here');
// Or, with debugging:
$result = do_mail('username@gmail.com', 'First Last', 'someone@example.com', 'Subject here', 'message here', true);
// Print the result
var_dump($result);
?>