
虽然有现成的类库(如PEAR)能够很方便地完成附件增加和发送,然则关于一些小站点(服务器硬件、网站范围都不抱负),装置PEAR可能会带来不必要的累赘,下降WEB顺序运转效力。
经由过程对邮件花样的熟悉,我们能够写一个脚原本发送附件。代码并不长:
[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]
更多PHP相干学问,请接见ki4网!
以上就是PHP发送邮件:怎样自定义reply-to头部以及附件的细致内容,更多请关注ki4网别的相干文章!