note 43263 modified in ref.mail by aidan

From: Date: Sat, 26 Jun 2004 03:19:29 +0000
Subject: note 43263 modified in ref.mail by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-71713@lists.php.net to get a copy of this message
hey, i wrote a few functions that improve on some of the functoins below, this will send emails w/ attachments, the params of the sendMail function are as follows: <?php $host = smtp server to connect to $to = array of emails to send email to, ex. array("meep@blah.moo", "blah@meep.moo") ; $from = single email of sender, goes into From header, and is sent as the mail from: $headers = array of headers, in the form of array("header" => "value") $msg = the message $attachments = array of paths to files to be attached, can be left out function getMime($filename) { //add more to array if needed :-P $types = array( ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".png" => "image/png" ) ; $filename = basename($filename) ; $ext = strtolower(substr($filename, strrpos($filename, "."))) ; return $types[$ext] ; } function sendRequest($fp, $req, $rc) { fputs($fp, "$req\r\n") ; $ret = sscanf(fgets($fp), "%d") ; $ret = $ret[0] ; if($ret != $rc) { fputs($fp, "quit\r\n") ; fclose($fp) ; die("Invalid response from server, $req returned $ret, expected $rc<br/>") ; } } function sendMail($host, $to, $from, $headers, $msg, $attachments="") { $bound = uniqid(time()."_") ; $content = "From: $from\r\n" ; $heloFrom = "" ; foreach($headers as $header => $value) $content .= "$header: $value\r\n" ; if(is_array($attachments)) { $content .= "MIME-Version: 1.0\r\n" ; $content .= "Content-Type: multipart/mixed; boundary=\"$bound\"\r\n" ; $content .= "--$bound\r\n" ; $content .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" ; $content .= "Content-Transfer-Encoding: 8bit\r\n\r\n$msg\r\n\r\n" ; foreach($attachments as $attch) { if($fp = fopen($attch, "rb")) { $bn = basename($attch) ; $content .= "--$bound\r\n" ; $content .= "Content-Type: ".getMime($attch)."; name=\"$bn\"\r\n" ; $content .= "Content-Transfer-Encoding: base64\r\n" ; $content .= "Content-Disposition: inline; filename=\"$bn\"\r\n\r\n" ; $content .= chunk_split(base64_encode(fread($fp, filesize($attch))))."\r\n" ; fclose($fp) ; } else { echo "Error, could not open $attch." ; return false ; } } $content .= "--$bound\r\n" ; } else { $content .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" ; $content .= "Content-Transfer-Encoding: 8bit\r\n\r\n$msg\r\n\r\n" ; } $to = array_chunk($to, 5) ; foreach($to as $batch) { $fp = fsockopen($host, 25, $errno, $errstr, 60) ; if(!$fp) die("Connection failed to $host:$port: ($errno) $errstr<br/>") ; do { $res = fgets($fp) ; } while($res[3] == "-") ; sendRequest($fp, "helo $heloFrom", 250) ; sendRequest($fp, "mail from: $from", 250) ; foreach($batch as $rcpt) sendRequest($fp, "rcpt to: $rcpt", 250) ; sendRequest($fp, "data", 354) ; sendRequest($fp, $content."\r\n.", 250) ; sendRequest($fp, "quit", 221) ; fclose($fp) ; } } ?> --was-- hey, i wrote a few functions that improve on some of the functoins below, this will send emails w/ attachments, the params of the sendMail function are as follows: $host = smtp server to connect to $to = array of emails to send email to, ex. array("meep@blah.moo", "blah@meep.moo") ; $from = single email of sender, goes into From header, and is sent as the mail from: $headers = array of headers, in the form of array("header" => "value") $msg = the message $attachments = array of paths to files to be attached, can be left out function getMime($filename) { //add more to array if needed :-P $types = array( ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".png" => "image/png" ) ; $filename = basename($filename) ; $ext = strtolower(substr($filename, strrpos($filename, "."))) ; return $types[$ext] ; } function sendRequest($fp, $req, $rc) { fputs($fp, "$req\r\n") ; $ret = sscanf(fgets($fp), "%d") ; $ret = $ret[0] ; if($ret != $rc) { fputs($fp, "quit\r\n") ; fclose($fp) ; die("Invalid response from server, $req returned $ret, expected $rc<br/>") ; } } function sendMail($host, $to, $from, $headers, $msg, $attachments="") { $bound = uniqid(time()."_") ; $content = "From: $from\r\n" ; $heloFrom = "" ; foreach($headers as $header => $value) $content .= "$header: $value\r\n" ; if(is_array($attachments)) { $content .= "MIME-Version: 1.0\r\n" ; $content .= "Content-Type: multipart/mixed; boundary=\"$bound\"\r\n" ; $content .= "--$bound\r\n" ; $content .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" ; $content .= "Content-Transfer-Encoding: 8bit\r\n\r\n$msg\r\n\r\n" ; foreach($attachments as $attch) { if($fp = fopen($attch, "rb")) { $bn = basename($attch) ; $content .= "--$bound\r\n" ; $content .= "Content-Type: ".getMime($attch)."; name=\"$bn\"\r\n" ; $content .= "Content-Transfer-Encoding: base64\r\n" ; $content .= "Content-Disposition: inline; filename=\"$bn\"\r\n\r\n" ; $content .= chunk_split(base64_encode(fread($fp, filesize($attch))))."\r\n" ; fclose($fp) ; } else { echo "Error, could not open $attch." ; return false ; } } $content .= "--$bound\r\n" ; } else { $content .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n" ; $content .= "Content-Transfer-Encoding: 8bit\r\n\r\n$msg\r\n\r\n" ; } $to = array_chunk($to, 5) ; foreach($to as $batch) { $fp = fsockopen($host, 25, $errno, $errstr, 60) ; if(!$fp) die("Connection failed to $host:$port: ($errno) $errstr<br/>") ; do { $res = fgets($fp) ; } while($res[3] == "-") ; sendRequest($fp, "helo $heloFrom", 250) ; sendRequest($fp, "mail from: $from", 250) ; foreach($batch as $rcpt) sendRequest($fp, "rcpt to: $rcpt", 250) ; sendRequest($fp, "data", 354) ; sendRequest($fp, $content."\r\n.", 250) ; sendRequest($fp, "quit", 221) ; fclose($fp) ; } } http://php.net/manual/en/ref.mail.php

« previous php.notes (#71713) next »