douping5226 2014-08-20 14:01
浏览 74
已采纳

我有一个问题,使用PHP发送带附件的邮件

I have a problem with sending mail with attachment using php. The mail is sent with broken attachment, for example when I try to send example.doc with size 2 Mb, in the mail I receive noname with size 1Kb
I use two files php.

The 1st one mailClass.php contain :

<?php 
class mail
{

function emailWithAttach($fromAdress,$toAdress,$mailSubject,$mailMessageHead,
                         $mailMessageMain,$mailMessageSign,$filePath,$fileName)
        {
            $fileatt_name = $fileName;
            $fileatt = $filePath.$fileName;
            $fileatt_type = "application/doc";
            $email_from = $fromaddress;
            $email_subject = $mailSubject;

            $email_message = $mailMessageHead. "<br>";
            $email_message .= $mailMessageMain. "<br>";
            $email_message .= $mailMessageSign;

            $email_to = $toAdress;
            $headers = "From: " .$email_from;

            $file = fopen ($fileatt."rb");
            $data = fread ($file, filesize($fileatt));
            fclose($file);

            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


            $headers .= "
MIME-Version: 1.0 
".
            "Content-Type : multipart/mixed:
".
            "boundary=\"{$mime_boundary}\"";

            $email_message .=  "This is a multip-part message in MIME format. 

".
            "--{$mime_boundary}
".
            "Content-Type : text/html; charset=\"iso-8859-1\"
".
            "Content-Transfer-Encoding: 7bit

".
            $email_message .= "

"; 

            $data = chunk_split(base64_encode($data));

            $email_message .= "--{$mime_boundary}
".
            "Content-Type: {$fileatt_type}:
".
            "name=\"{$fileatt_name}\"
".
            "Content-Transfer-Encoding: base64 

".
            $data .= "

".
            "--{$mime_boundary}--
";

            if(@mail($email_to,$email_subject,$email_message,$headers))
            {
                return true;
            }  
        }
} 

?>

The second file index.php contain :

<?php
include "mailClass.php";

$testEmail = new mail;

$from = "sender@gmail.com";
$sendTo = "receiver@gmail.com";
$subject = "email with attachment";
$bodyHead = "welcome to the attachment email test";
$bodyMain = "hello iteb";
$bodyEnd = "Thank you";
$filePath = "";
$fileName = "example.doc";

if ($testEmail->emailWithAttach($from,$sendTo,$subject,$bodyHead,$bodyMain,$bodyEnd,$filePath,$fileName))
{
    echo "Email Send successful!!";
}   
else
{
    echo "Email Send Failed";
}

?>
  • 写回答

1条回答 默认 最新

  • duanshai4484 2014-08-20 14:33
    关注

    Use phpmailer to send a mail: Get it here

    It also has the advantage that you can send out many mails without opening and closing the connection each time as php's mail() function does.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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