Although there are ready-made class libraries (such as PEAR) that can easily add and send attachments, for some small sites (the server hardware and website scale are not ideal), installing PEAR It may bring unnecessary burden and reduce the efficiency of WEB program operation.
By understanding the email format, we can write a script to send attachments. The code is not long:
[php]
function mailSend($to, $subject, $message, $attach, $from, $replyto) { //定义边界线 $boundary = uniqid(); //生成邮件头 $header = "From: $from Reply-to:$replyto Content-type: multipart/mixed; boundary=\"$boundary\""; //获取附件文件的MIME类型 $mimeType = mime_content_type($attach); //对附件文件进行编码和切分 $fp = fopen($attach, "r"); if ($fp) { $content = fread($fp, filesize($attach)); $content = chunk_split(base64_encode($content)); fclose($fp); } else { die("Failed to open file…"); } //生成邮件主体 $body = " –$boundary Content-type: text/plain; charset=utf-8; Content-transfer-encoding: 8bit $message –$boundary Content-Type: $mimeType; name=$attach Content-Disposition: attachment; filename=$attach Content-Transfer-Encoding: base64 $content –$boundary–"; //发送邮件 mail($to, $subject, $body, $header) or die("Failed to send mail…"); }
[/php]
For more PHP related knowledge, please VisitPHP Chinese website!
The above is the detailed content of Sending emails with PHP: How to customize reply-to headers and attachments. For more information, please follow other related articles on the PHP Chinese website!