0

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());
        }
  • Do you have a valid from address? – Keutelvocht Nov 28 '17 at 10:25
  • You're using an old version of PHPMailer, and you've based your code on an obsolete example. [Get the latest](https://github.com/PHPMailer/PHPMailer). Read the troubleshooting guide. Set `SMTPDebug = 2` to see what the server is saying. – Synchro Nov 28 '17 at 10:40
  • @Synchro So why does it sometimes successfully send and fail sometimes even in old version? – John Dale Ocaya Andil Dale Nov 29 '17 at 07:00
  • It's not possible to tell from your code, but if this is *all* your code, it will be run on every page load and doesn't contain any checks on what's been submitted, and browser extensions often load a page twice, which would explain a double send. – Synchro Nov 29 '17 at 08:22

0 Answers0