doukuipai8544 2010-06-22 11:59
浏览 143
已采纳

php发送带附件的电子邮件

I can't seem to find the problem with this php function i wrote that should send an e-mail with attachment. I've been struggling with it for quite a while.

function myMail($to, $subject, $mail_msg, $filename, $contentType){

    $random_hash = md5(date('r', time()));
    $headers = "From: webmaster@example.com
Reply-To: ".$to;
    $headers .= "
Content-Type: ".$contentType.
        "; boundary=\"PHP-mixed-".$random_hash."\"";

    $attachment = chunk_split(base64_encode(file_get_contents($filename)));
    ob_start();

    echo "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"

--PHP-alt-$random_hash
Content-Type: text/plain; charset=\"utf-8\"
Content-Transfer-Encoding: 7bit

$mail_msg

--PHP-alt-$random_hash

--PHP-mixed-$random_hash--
Content-Type: text/plain; name=\"$filename\" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

$attachment
--PHP-mixed-$random_hash--
";
    $message = ob_get_clean();
    $mail_sent = @mail( $to, $subject, $message, $headers );
    return $mail_sent ? "Mail sent" : "Mail failed";
}

Edit The problem is that the message of the mail is mixed with the file and send as an attachment.

  • 写回答

4条回答 默认 最新

  • duanluan8390 2010-06-22 12:58
    关注

    Artefacto made me look at the output with more attention and i've found the fix:

    function myMail($to, $subject, $mail_msg, $filename, $contentType, $pathToFilename){
        $random_hash = md5(date('r', time()));
        $headers = "From: webmaster@mysite.com
    Reply-To: ".$to;
        $headers .= "
    Content-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
        $attachment = chunk_split(base64_encode(file_get_contents($pathToFilename)));
        ob_start();
     echo "
    --PHP-mixed-$random_hash
    Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"
    
    --PHP-alt-$random_hash
    Content-Type: text/plain; charset=\"utf-8\"
    Content-Transfer-Encoding: 7bit
    
    $mail_msg
    
    --PHP-alt-$random_hash--
    
    --PHP-mixed-$random_hash
    Content-Type: $contentType; name=\"$filename\" 
    Content-Transfer-Encoding: base64 
    Content-Disposition: attachment 
    
    $attachment
    --PHP-mixed-$random_hash--
    ";
    $message = ob_get_clean();
    $fh=fopen('log.txt','w');
    fwrite($fh,$message);
    $mail_sent = @mail( $to, $subject, $message, $headers );
    return $mail_sent ? "Mail sent" : "Mail failed";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么