Introduction to Phpmail Functions
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Where: $to required. Specify the recipient of the message
$subject required. Specify the subject of the message. The parameter cannot contain any line-wrapping characters.
$message required. Specifies the message to be sent.
$additional _headers Specifies additional headers, such as from, Cc, and Bcc
Additional parameters for the _parameters regulation of the SendMail program $additional
Reference code (test valid under Linux server)
<?phpclass sendmailapi{/** * @param $to recipient * @param $subject message subject * @param message sent $message * @pa Ram $from Sender * @param $content _type type * @param $attache attachment */Public Function SendMail ($to, $subject, $message, $from, $content _type, $attache = Array ()) {if (!empty ($from)) $head = "From: $from \ n"; if (Empty ($content _type)) $content _type = "Text/plain"; if (Is_array ($attache)) {$boundary = "= = =". MD5 (Uniqid ("")). " ==="; $head. = "mime-version:1.0\ncontent-type:multipart/mixed; Boundary=\ ""; $head. = "$boundary \" \n\nthis is a multi-part message in MIME format.\n\n "; $head. = "--$boundary \ n"; $head. = "Content-type: $content _type\n"; $head. = "\n$message\n\n"; while (list ($key, $val) = All ($attache)) {$fd = fopen ("$val", "R") or Die ("Unable to open file$val"); $contents = Chunk_split (basE64_encode (Fread ($FD, FileSize ("$val"))); Fclose ($FD); $head. = "--$boundary \ n"; $head. = "Content-type:application/octet-stream; Name=\ "". BaseName ($val); $head. = "\" \ncontent-transfer-encoding:base64\n "; $head. = "content-disposition:attachment; Filename=\ "". BaseName ($val); $head. = "\" \ n \ nthe ". $contents. "\ n"; } $head. = "--". $boundary. "--\n\n"; } else {if (!empty ($content _type)) {$head. = "Content-type: $content _type\n"; $head. = "\n$message\n"; }} return mail ($to, $subject, "", $head); Public Function Sendmailtest () {$to = "[email protected]"; Mail Recipient $subject = "Test"; Message title $from = "[email protected]"; Mail Sender $subject = "=? UTF-8? B? ". Base64_encode ($subject). "?="; $order _bn = Date ('Ymd '). substr (Implode (NULL, Array_map (' Ord ', Str_split (substr (), 7, 13), 1)), 0, 8); $memer _id = Self::mt_rand (1000000,9999999); $ship _mobile = ' 139xxxxxxxx '; Generates random data $res = Array (0 = = Array (' order_bn ' = = $order _bn, ' member_id ' = $memer _id, ' ship_mobile ' and ' = $ship _mobile, '), 1 = = Array (' order_bn ' = $order _bn, ' member_id ' = $me mer_id, ' ship_mobile ' = $ship _mobile,), 2 = = Array ( ' Order_bn ' = $order _bn, ' member_id ' and $memer _id, ' Ship_mobi Le ' = $ship _mobile,), 3 = = Array (' order_bn ' = $or Der_bn, ' member_id ' = $memer _id, ' Ship_Mobile ' = $ship _mobile,), 4 = = Array (' order_bn ' = $order _bn, ' member_id ' = $memer _id, ' ship_mobile ' and $ship _mobile, ), 5 = = Array (' order_bn ' = $order _bn, ' Membe r_id ' + $memer _id, ' ship_mobile ' = $ship _mobile,), 6 = Array (' order_bn ' = $order _bn, ' Member_i d ' = $memer _id, ' ship_mobile ' = $ship _mobile,)); $csv _header = Array (' Order number ', ' Member id ', ' mobile number '); $file _name = Date ("y-m-d"). ". csv"; $fp = fopen ("$file _name", ' a '); Handle Header $header = Implode (', ', $csv _header). Php_eol; Processing content $content = '; foreach ($res as $k = = $v) { $content. = Implode (', ', $v). Php_eol; }//Splicing $csv = $header. $content; Write and close resource fwrite ($FP, $csv); Fclose ($FP); Add Attachment $attache = Array ($file _name); $message = "This is a test"; Message body $headers = "From: $from". "\ n";//header information set $headers. = "mime-version:1.0". "\ n"; $headers. = "content-type:text/html; Charset=uft-8 ". "\ r \ n"; $headers. = "Content-transfer-encoding:8bit"; $rst = $this->sendmail ($to, $subject, $message, $from, ", $attache); @unlink ($file _name); Var_dump ($rst); }}//Test $mailobj = new Sendmailapi (), $rst = $mailObj->sendmailtest ();
Test effect
Send mail and mail with attachments based on PHP's own mail function