• 技术文章 >后端开发 >php教程

    PHP发送邮件:如何自定义reply-to头部以及附件

    angryTomangryTom2019-10-15 15:09:41转载984
    9.jpg

    虽然有现成的类库(如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相关知识,请访问PHP中文网

    以上就是PHP发送邮件:如何自定义reply-to头部以及附件的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:博客园,如有侵犯,请联系admin@php.cn删除
    专题推荐:邮件
    上一篇:php程序员经常忽略的冷门知识点 下一篇:php访问url的四种方式
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【活动】充值PHP中文网VIP即送云服务器• php如何发送电子邮件?使用PHPMailer实现• thinkphp5利用phpmailer发送邮件• [PHP] 邮件发送mail()函数失败问题(sendmail命令与postfix )• php验证电子邮件是否正确
    1/1

    PHP中文网