0

currently I'm trying to send emails with file attachements using PHPMailer. Unfortunatelly, each script execution takes approximatelly 60 seconds to send a message. here is my code:

 $time_start = microtime(true);
require_once('class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"


//$body             = file_get_contents('contents.html');
//$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->SetFrom('name@yourdomain.com', 'First Last');

$mail->AddReplyTo("name@yourdomain.com","First Last");

$address = "xxx@gmail.com";
$mail->AddAddress($address, "John Doe");

$mail->Subject    = "PHPMailer Test Subject via Sendmail, basic";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML("html body");

$mail->AddAttachment("abc.txt");      // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}
$time_end = microtime(true);
$execution_time = ($time_end - $time_start);
echo 'Total Execution Time:'.$execution_time.' Sec';

The time of execution is not very depending on file size, but 60 s of script execution time is a bit too much for me. How can I fix this?

Jakub Pastuszuk
  • 928
  • 4
  • 12
  • 31
  • with no attachment it takes how long? –  Aug 12 '14 at 21:41
  • Open phpmailer debugger and look at which part is slowing down your script. – HddnTHA Aug 12 '14 at 21:43
  • Set $mail->Body. Is not same as $mail->AltBody. Not saying is slow because of it just saying you should do it. You should get exception for not setting it. – Aleksandar Popovic Aug 12 '14 at 21:52
  • You'll only get exceptions if you enable them in the constructor. There is an error though: calling `msgHTML()` (which sets `Body`) also sets `AltBody`, so if you want to set `AltBody` yourself, you must do so *after* `msgHTML()`. Otherwise the best way to see what's slow is to use a debugger and/or profiler, as @HddnTHA said. Short of that, don't just record the overall time, stick a few `echo` statements at critical points in the script. Also that `eregi_replace` should not even be there... – Synchro Aug 13 '14 at 08:14
  • I added smtp to my phpmailer config: $mail->IsSMTP(); $mail->Host = "localhost"; and now it executes in 0.2 sec. – Jakub Pastuszuk Aug 16 '14 at 22:33

0 Answers0