I have this send mail function but sometimes it fails.
I want to always successfully send it to the receiver.
There are times that it successfully sends for first time, but there are also times it is unable to send for 3-4 tries.
I am very new to sending mails in php.
Here is my code:
$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());
}