note 25933 added to function.mail

From: Date: Fri, 11 Oct 2002 15:30:21 +0000
Subject: note 25933 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-37941@lists.php.net to get a copy of this message
here's a quick function I built whittling some SMTP code down to the bare bones I needed: function socketmail($from, $toArray, $subject, $message) { $fromTitle = substr($from, strpos($from, '"'), strrpos($from, '"')-strpos($from, '"')+1); $fromAddr = substr($from, strpos($from, '<'), strrpos($from, '>')-strpos($from, '<')+1); if (strlen($fromAddr) == 1) $fromAddr = substr($from, strrpos($from, '"')+1, strlen($from)); if (strlen($fromTitle) == 1) $fromAddr = $from; $connect = fsockopen (ini_get("SMTP"), 25, $errno, $errstr, 30) or die("Could not talk to the sendmail server!"); $rcv = fgets($connect, 1024); foreach($toArray as $to) { $toTitle = substr($to, strpos($to, '"'), strrpos($to, '"')-strpos($to, '"')+1); $toAddr = substr($to, strpos($to, '<'), strrpos($to, '>')-strpos($to, '<')+1); if (strlen($toAddr) == 1) $toAddr = substr($to, strrpos($to, '"')+1, strlen($to)); if (strlen($toTitle) == 1) $toAddr = $to; fputs ($connect, "MAIL FROM:$fromAddr"."\r\n"); $rcv = fgets ($connect, 1024); fputs ($connect, "RCPT TO:$toAddr\r\n"); $rcv = fgets ($connect, 1024); fputs ($connect, "DATA\r\n"); $rcv = fgets ($connect, 1024); fputs ($connect, "Subject: $subject" . "\r\n" ); fputs ($connect, "From: \"$fromTitle\" $fromAddr" . "\r\n" ); fputs ($connect, "To: \"$toTitle\" $toAddr" . "\r\n" ); fputs ($connect, "\r\n" ); fputs ($connect, "$message " . "\r\n" ); fputs ($connect, ".\r\n"); $rcv = fgets ($connect, 1024); fputs ($connect, "RSET\r\n"); $rcv = fgets ($connect, 1024); } fputs ($connect, "QUIT\r\n"); $rcv = fgets ($connect, 1024); fclose($connect); } I run a service which needed a fast way to send perhaps 30 emails out to the members without using a mailing-list, long lists of addresses in the To: field and Bcc: which some people automatically block as spam. Using mail() looping through 30 addresses takes 60 seconds or more. With the function above, you can feed it an array of addresses, it will open the sendmail socket once, send all the messages, and close it again. This cut the time for sending 30 messages to about 5 seconds. It will accept addresses (both To: and From:) in the following four formats: "Addressee Name" <email@address.com> "Addressee Name" email@address.com <email@address.com> email@address.com Make an array of all the addresses you wish to send a message to, and send it to the function. Although headers aren't an option here, they should be pretty easy to add if you need them. GreyWyvern -- http://www.php.net/manual/en/function.mail.php http://master.php.net/manage/user-notes.php?action=edit+25933 http://master.php.net/manage/user-notes.php?action=delete+25933 http://master.php.net/manage/user-notes.php?action=reject+25933

« previous php.notes (#37941) next »