Best Way to Send Email
Posted: Thu Sep 23, 2004 7:04 am
Hi,
I am currently writing some software to run a mailing list with PHP4 and MySQL.
I am looking at a number of methods of doing this and I would like some advice on the most efficient method. It is first worth noting that the PHP I'm using is in safe mode. Secondly it is worth noting I am sending to an array (taken from a MySQL db of about 1000 email addresses)
The problem is I can't keep testing these methods on 1000 people as it will annoy them if they keep getting test emails from the script... so I just have to try it whenever I have an email I actually have to send, which is not very often.
Initially I started using this function to send emails:
This works fine but does take a while to send, understandably, because mail() is opening a socket, sending, closing, opening, sending, closing etc... not very efficient.
As a result I was advised to use a socketmail method, i.e. directly conversing with the SMTP server. I borrowed the code of the internet to test the principle, I don't claim this code as my own!
This second instance functions correctly for the first load of emails but timesout after 30 seconds. I don't know then how fast it is, or how I might stop the script timing out.
A third alternative is to use sendmail via the popen command, thus:
I've no idea what would happen if I tried this.
I would like to hear any suggestions about the best way to send the emails; the first instance just seems to take so long even if it works so I'd like a quicker alternative if at all possible. I've taken this as far as I can with my limited understanding of the PHP architecture. I'm hoping someone with a bit more knowledge might be able to help me out!
Cheers
Nick.
I am currently writing some software to run a mailing list with PHP4 and MySQL.
I am looking at a number of methods of doing this and I would like some advice on the most efficient method. It is first worth noting that the PHP I'm using is in safe mode. Secondly it is worth noting I am sending to an array (taken from a MySQL db of about 1000 email addresses)
The problem is I can't keep testing these methods on 1000 people as it will annoy them if they keep getting test emails from the script... so I just have to try it whenever I have an email I actually have to send, which is not very often.
Initially I started using this function to send emails:
Code: Select all
function sendEmail($subject, $to, $message, $from) {
$headers = "From: $from \r\nReply-To: $from\r\nX-Mailer: Van Mildert JCR Mailing Lists";
if (is_array($to)) {
for($y = 0; $y < sizeof($to); $y++) {
$sendto = $to[$y];
$sendmail = mail($sendto, $subject, $message, $headers);
if (!$sendmail) { $error =+ $to[$y] . "\n"; }
}
if ($error) { print "An error occured. Emails could not be sent to the following people:";
print "<pre>" . $error . "</pre>";
exit;
}
}
else {
mail ($to, $subject, $message, $headers)
or die("Error: Mail not sent..");
}
}As a result I was advised to use a socketmail method, i.e. directly conversing with the SMTP server. I borrowed the code of the internet to test the principle, I don't claim this code as my own!
Code: Select all
function socket_mail($toArray, $subject, $message, $fromF) {
// Setup
$fromEmail = "van-mildert.jcr@durham.ac.uk";
$fromMailer = "Socketmail v2.0";
$smtp = "localhost";
$smtp_port = 25;
$charset = "ISO-8859-1";
ini_set(sendmail_from, $fromEmail);
$connect = @fsockopen ($smtp, $smtp_port, $errno, $errstr, 5);
if (!$connect) return false;
$rcv = fgets($connect, 1024);
fputs($connect, "HELO {$_SERVER['SERVER_NAME']}\r\n");
$rcv = fgets($connect, 1024);
foreach ($toArray as $to) {
$toBits = explode(" ", $to);
$toRcpt = trim($toBits[count($toBits) - 1], "<> ");
fputs($connect, "RSET\r\n");
$rcv = fgets($connect, 1024);
fputs($connect, "MAIL FROM:$fromEmail\r\n");
$rcv = fgets($connect, 1024);
fputs($connect, "RCPT TO:$toRcpt\r\n");
$rcv = fgets($connect, 1024);
fputs($connect, "DATA\r\n");
$rcv = fgets($connect, 1024);
fputs($connect, "Subject: $subject\r\n");
fputs($connect, "From: $fromF\r\n");
fputs($connect, "To: $to\r\n");
fputs($connect, "X-Sender: <$fromF>\r\n");
fputs($connect, "Return-Path: <$fromF>\r\n");
fputs($connect, "Errors-To: <$fromF>\r\n");
// fputs($connect, "Message-Id: <".md5(uniqid(rand())).".".preg_replace("/[^a-z0-9]/i", "", $from)."@$smtp>\r\n");
fputs($connect, "X-Priority: 3\r\n");
fputs($connect, "Date: ".date("r")."\r\n");
fputs($connect, "Content-Type: text/plain; charset=$charset\r\n");
fputs($connect, "\r\n");
fputs($connect, $message);
fputs($connect, "\r\n.\r\n");
$rcv = fgets($connect, 1024);
}
fputs ($connect, "QUIT\r\n");
$rcv = fgets ($connect, 1024);
fclose($connect);
ini_restore(sendmail_from);
return true;
}A third alternative is to use sendmail via the popen command, thus:
Code: Select all
$mailer = popen ("/usr/sbin/sendmail -t -i","w");
fwrite ($mailer,"Subject: $subject
From: $sender
To: $toArray
$message
");
pclose ($mailer);I would like to hear any suggestions about the best way to send the emails; the first instance just seems to take so long even if it works so I'd like a quicker alternative if at all possible. I've taken this as far as I can with my limited understanding of the PHP architecture. I'm hoping someone with a bit more knowledge might be able to help me out!
Cheers
Nick.