【问题标题】:SMTP MIME header with attachment带有附件的 SMTP MIME 标头
【发布时间】:2011-12-20 13:53:08
【问题描述】:

我正在尝试创建一个 PHP 函数来使用带有附件的 SMTP 发送电子邮件。我很难创建 MIME 标头,因为我希望正文包含 HTM 格式的文本和要附加的文件。我使用的是非常旧版本的 PHP (4.3.8),这是唯一的方法那是有效的。尝试过 PEAR,但无法正确验证 SMTP。

这是我目前所拥有的: 我已经编辑了这段代码,因为现在我正确收到了消息,但文件已损坏。我把文件改成文本文件,一开始还不错,但后来出现了很多垃圾文本。

$newLine = "\r\n";
$attachment="myFile.zip";
$message = "<br><h1><center>TEST MESSAGE</center></h1>" ;   


    //Body
    $headers .= "Content-Type: multipart/mixed;" . $newLine;
    $headers .= "     boundary=\"_e9e06aa5-1550-464d-ace4-e85b575d1899_\"" . $newLine . $newLine;   
    $newLine . $newLine;

    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine;
    $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
    $headers .= "Content-Transfer-Encoding: quoted-printable" . $newLine;
    $headers .= "     boundary=\"_7fdd1316-6f68-41bb-93f7-134933fc9aad_\"" . $newLine . $newLine;

    $headers .=  $message . $newLine;

    //$headers .= "--_7fdd1316-6f68-41bb-93f7-134933fc9aad_--" . $newLine; // If I leave this line it appears in the end of the message area.

    //Attachment
    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_" . $newLine;

    $headers .= "Content-Transfer-Encoding: base64" . $newLine;
    $headers .= "Content-Type: text/plain;" . $newLine;
    $headers .= "Content-Disposition: attachment;" . $newLine;
    $headers .= "    filename=" . $attachment . $newLine . $newLine;

    $handle = fopen($attachment, "rb");
    $contents = '';
    while (!feof($handle)) {
        $contents .= fread($handle, filesize($attachment));
    }
    fclose($handle);

    $contents = base64_encode($contents);

    $headers .= $contents . $newLine;
    $headers .= "--_e9e06aa5-1550-464d-ace4-e85b575d1899_--" . $newLine;

【问题讨论】:

    标签: php email smtp mime email-headers


    【解决方案1】:

    使用像PHPMailer 这样的就绪类怎么样?这要容易得多。并且有PHP4的版本。

    例子:

    require_once '../class.phpmailer.php';
    
    $mail = new PHPMailer();
    $mail->AddReplyTo('name@yourdomain.com', 'First Last');
    $mail->AddAddress('whoto@otherdomain.com', 'John Doe');
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    $mail->AddReplyTo('name@yourdomain.com', 'First Last');
    $mail->Subject = 'PHPMailer Test Subject via mail()';
    $mail->MsgHTML('Hello world');
    $mail->AddAttachment('file.zip');
    $mail->Send();
    

    制作自定义标题会让人头疼。

    【讨论】:

    【解决方案2】:

    SMTP 数据流如下所示:(空行很重要,都以 CRLF 结尾)

    To: <joeuser@roadrunner.com> 
    From: mary@roadrunner.com 
    MIME-Version: 1.0 
    Subject:email subject line 
    Content-Type: multipart/mixed; 
      boundary="Z--Z=_CGI_Perl_Cookbook_=1409529510"
    
    This is a multi-part message in MIME format.
    
    --Z--Z=_CGI_Perl_Cookbook_=1409529510
    Content-Transfer-Encoding: quoted-printable 
    Content-Type: text/plain;   charset="us-ascii" 
    
    Email message 
    text 
    
    --Z--Z=_CGI_Perl_Cookbook_=1409529510
    Content-Disposition: attachment; 
       filename=attachment.txt
    Content-Type: plain/text 
    Content-Transfer-Encoding: None 
    
    sometext data
    attached to
    the email
    
    --Z--Z=_CGI_Perl_Cookbook_=1409529510--
    
    . << \n\.\n to terminate the email
    

    【讨论】:

      【解决方案3】:

      多部分/混合 mime 消息创建脚本:https://github.com/breakermind/PhpMimeParser/blob/master/PhpMimeClient_class.php 带有附件和内联消息,抄送和密件抄送

      $m = new PhpMimeClient();
      // Add to
      $m->addTo("email@star.ccc", "Albercik");
      $m->addTo("adela@music.com", "Adela");
      // Add Cc
      $m->addCc("hello@email.be", "Ben");
      $m->addCc("zonk@email.au");
      // Add Bcc
      $m->addBcc("boos@domain.com", "BOSS");    
      // Add files inline
      $m->addFile('photo.jpg',"zenek123");
      // Add file
      $m->addFile('sun.png');
      // create mime
      $m->createMime("Witaj księżniczko Alabambo",'<h1>Witaj księżniczko Alabambo <img src="cid:zenek123"> </h1>',"Wesołych świąt życzę!","Heniek Wielki", "heniek@domain.com");
      // get mime
      // $m->getMime();
      // Show mime
      echo nl2br(htmlentities($m->getMime()));
      

      【讨论】:

        猜你喜欢
        • 2021-11-05
        • 1970-01-01
        • 2012-09-12
        • 2015-02-26
        • 2019-03-20
        • 2011-04-15
        • 2012-01-18
        • 2011-07-17
        • 2014-03-30
        相关资源
        最近更新 更多