我有这个发送邮件功能,但有时会失败。
我希望始终将其成功发送给接收方。
有时它第一次成功发送,但有时也无法发送3-4次尝试。
我非常擅长用PHP发送邮件。
这是我的代码:
$sendTo = rawurldecode($sendTo);
$now = date("YmdHis");
$filePath = "/var/www/citest/csv/listmaster_".$now.".csv";
$fileName = "listmaster_".$now.".csv";
log_message('debug','export start');
$this->exportCsv($filePath, $searchName, $searchAdd, $prefKey, $searchKey, $withEmail, $withTel, $withFax);
log_message('debug','export done : ' . $fileName);
$toAddress = $sendTo;
try {
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->Host = self::$host;
$mail->Port = self::$port; // or 587
$mail->SMTPSecure = 'tls';
$mail->IsHTML(true);
$mail->CharSet = self::$charset;
$mail->Username = self::$username;
$mail->Password = self::$password;
$mail->SetFrom(self::$from, self::$fromAlias);
$mail->Subject = trim("Here's your csv.");
$mail->Body = trim("Thank you for using List Master Application. Please download your csv on this link. <a href='http://36.55.238.182:81/pagination/download?file=".$fileName."'>Here!</a>");
if(is_array($toAddress))
{
foreach($toAddress as $to_line)
{
$mail->AddAddress($to_line);
}
}
else
{
$mail->AddAddress($toAddress);
}
if ($mail->send())
{
log_message('debug', 'sucessfully send email to ' . $sendTo);
return TRUE;
}
else
{
log_message('debug', 'Failed to send email to ' . $sendTo);
return FALSE;
}
} catch (phpmailerException $e) {
log_message('debug', $e->errorMessage());
} catch (Exception $e) {
log_message('debug', $e->getMessage());
}