1

我正在尝试创建一个 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;
4

3 回答 3

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();

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

于 2011-11-02T18:19:52.770 回答
0

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
于 2014-09-01T00:56:41.770 回答
0

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

$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()));
于 2017-11-05T11:33:55.173 回答