得到奇怪的结果在PHP邮件功能

时间:2012-10-07 17:09:53

标签: php email header

我正在使用此功能通过电子邮件向附件发送消息

function sendemail($to, $from, $subject, $filename, $attachment_name) {

    $random_hash = md5(date('r', time()));

    $headers = "From: $from\r\nReply-To: $from";

    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-" . $random_hash . "\"";

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

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

Hello World!
This is the simple text version of the email message.


--PHP-alt-$random_hash
Content-Type: text/html; charset=\"UTF8\"
Content-Transfer-Encoding: 7bit


<h2>Hello World!</h2>
<p>This is the <b>HTML</b> version of the email message.</p>

--PHP-alt-$random_hash--

--PHP-mixed-$random_hash
Content-Type: application/zip; name=$attachment_name
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--PHP-mixed-$random_hash--";

    if (@mail($to, $subject, $output, $headers)) {
        echo 'good';
    } else {
        echo 'bad';
    }
}

但是这是我得到的实际消息的屏幕截图:enter image description here

如何摆脱

--PHP-alt-a08184c8a852c31ac8c508fe6ca357d8
Content-Type: text/html; charset="UTF8"
Content-Transfer-Encoding: 7bit

在电子邮件中,实际上将其用作元

1 个答案:

答案 0 :(得分:2)

公共变量

$to = 'to@localhost.com';
$from = 'from@localhost.com';
$subject = 'Subject 123';
$body = '<b>Hello World</b>';
$filename = 'index.rar';

您的代码

function sendemail($to, $from, $subject, $body, $filename) {
    $random_hash = md5(date('r', time()));
    $headers = "From: $from\r\nReply-To: $from";
    $headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-" . $random_hash . "\"";
    $attachment = chunk_split(base64_encode(file_get_contents($filename)));
    $output = "
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary='\"PHP-alt-$random_hash\"'

" . strip_tags($body) . "

--PHP-alt-$random_hash
Content-Type: text/html; charset=\"UTF8\"
Content-Transfer-Encoding: 7bit

" . $body . "

--PHP-alt-$random_hash--

--PHP-mixed-$random_hash
Content-Type: application/zip; name=" . basename($filename) . "
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment
--PHP-mixed-$random_hash--";
    return mail($to, $subject, $output, $headers);
}
sendemail($to, $from, $subject, $body, $filename);

您发送的电子邮件

Date: Sun, 07 Oct 2012 19:54:06 +0200
Subject: Subject 123
To: to@localhost.com
X-PHP-Originating-Script: 0:index.php
From: from@localhost.com
Reply-To: from@localhost.com
Content-Type: multipart/mixed; boundary="PHP-mixed-488d21ec53bdcff8cfe3f5ab49010354"


--PHP-mixed-488d21ec53bdcff8cfe3f5ab49010354
Content-Type: multipart/alternative; boundary='"PHP-alt-488d21ec53bdcff8cfe3f5ab49010354"'

Hello World

--PHP-alt-488d21ec53bdcff8cfe3f5ab49010354
Content-Type: text/html; charset="UTF8"
Content-Transfer-Encoding: 7bit

<b>Hello World</b>

--PHP-alt-488d21ec53bdcff8cfe3f5ab49010354--

--PHP-mixed-488d21ec53bdcff8cfe3f5ab49010354
Content-Type: application/zip; name=index.rar
Content-Transfer-Encoding: base64
Content-Disposition: attachment

UmFyIRoHAM+QcwAADQAAAAAAAABUw3QgkC4AGQMAAIkGAAACwova3OadR0EdMwkAIAAAAGluZGV4
LnBocACwSVdsDYFRDMy9FBW9ipX8O1CJPYlHGddFJLQKBVTwEqiwR41JrY3sjYPpHuwtgI/7ngWy
N7r3N1xxyQLUHvPGN5085nRx/nM5zOrdzc3F8vvx2H0Svy7+9BHA4UhhMvmihcJNNJMVm6xkHDQt
zBR62LkxJdKY68ovAto6/Ixawy0NxWg9u+fZpb7bfToLwVa9Gg9uIceZAbZI2zkyk6qE02RBc+dJ
eIV0cvWWP+xbMgrhYZ0vphkoeFDBTXnZImQrNvIjuJTxX//R8XNj+dJRlQOfV5eHxeNbrHI9nU3w
4+LlJCfTHyp/hewtz6qtQQkjHETspVepfpeHMpzocmamNNMkbCfwcJGOkQGN1hZPXWKxa6wsFusa
6XcY/B+sB+t9Y+TrcfGV8ASJ7yieGYgmUQncho6RXNBg1Rb5BolmKETa5ABL9QT0TdEfwJLDnaxS
amVrdkXgkqifXTWhzRHTnlIdcWaFEUqTA0CDqI9gYplSgj6bPw9Frm5eZpbVq0fu2aLwUu1rA02m
G6/EFXAszhAH0cLSZAYWM6yebyOBshPaRlX8cykM47UQlFZg9ZcEfml0SrkoBpqwi7I7RPa2OOcJ
sQjPfsTYstmqzwPNqkUKyrYVW4U0yAHEjZSiXqq4q47+9WQ7s83SJOupHakJwBfqMQRAMe/HFyef
S7EdCiFDtdr2qwaCHD34McBqyFYz3YxQu/vf74XHKGhnQwX2G/ul2lpWbnDsascUw7ou1bEsg+yA
sshYFVN9iylLU1s+3amLTSuNu9MQ2aaNjYRZYiirft3X10rhr86L/5L9anEUfXNBpHKbnQSoNsvT
BkM3DBjq8XNT95XxcZCSHxfqwMr5eB0DnXC+8BbFW1v34ydPunaalVC5CitTPtR+fbcTZV6pBClH
lJZJq99yV1W9uBi1riDlCl2iHMgnOJd+XNbnFJW3NJY1XsekkVQLr2rfBQODjgNWwFZI4/2zi1ZK
Z19pZqfjee5gTAX8IuVxcsKeY648vgvUPfVr4aR4o9IGM/sr+njZkhRJjeFXiNzBKi0Oqki25qBB
c9AfpMQ9ewBABwA=

--PHP-mixed-488d21ec53bdcff8cfe3f5ab49010354--

Swift代码

require_once('swift/lib/swift_required.php');
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject)
    ->setFrom(array($from))
    ->setTo(array($to))
    ->setEncoder(Swift_Encoding::get7BitEncoding())
    ->setBody($body, 'text/html')
    ->addPart(strip_tags($body), 'text/plain')
    ->attach(Swift_Attachment::fromPath($filename))
;
$mailer->send($message);

Swift发送了电子邮件

Subject: Subject 123
To: to@localhost.com
X-PHP-Originating-Script: 0:SimpleMailInvoker.php
Message-ID: <1349632162.5071c0a2972db@localhost>
Date: Sun, 07 Oct 2012 19:49:22 +0200
From: from@localhost.com
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="_=_swift_v4_13496321625071c0a297e85_=_"


--_=_swift_v4_13496321625071c0a297e85_=_
Content-Type: multipart/alternative;
 boundary="_=_swift_v4_13496321625071c0a298e1b_=_"


--_=_swift_v4_13496321625071c0a298e1b_=_
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Hello World

--_=_swift_v4_13496321625071c0a298e1b_=_
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

<b>Hello World</b>

--_=_swift_v4_13496321625071c0a298e1b_=_--


--_=_swift_v4_13496321625071c0a297e85_=_
Content-Type: application/x-rar-compressed; name=index.rar
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=index.rar

UmFyIRoHAM+QcwAADQAAAAAAAABUw3QgkC4AGQMAAIkGAAACwova3OadR0EdMwkAIAAAAGluZGV4
LnBocACwSVdsDYFRDMy9FBW9ipX8O1CJPYlHGddFJLQKBVTwEqiwR41JrY3sjYPpHuwtgI/7ngWy
N7r3N1xxyQLUHvPGN5085nRx/nM5zOrdzc3F8vvx2H0Svy7+9BHA4UhhMvmihcJNNJMVm6xkHDQt
zBR62LkxJdKY68ovAto6/Ixawy0NxWg9u+fZpb7bfToLwVa9Gg9uIceZAbZI2zkyk6qE02RBc+dJ
eIV0cvWWP+xbMgrhYZ0vphkoeFDBTXnZImQrNvIjuJTxX//R8XNj+dJRlQOfV5eHxeNbrHI9nU3w
4+LlJCfTHyp/hewtz6qtQQkjHETspVepfpeHMpzocmamNNMkbCfwcJGOkQGN1hZPXWKxa6wsFusa
6XcY/B+sB+t9Y+TrcfGV8ASJ7yieGYgmUQncho6RXNBg1Rb5BolmKETa5ABL9QT0TdEfwJLDnaxS
amVrdkXgkqifXTWhzRHTnlIdcWaFEUqTA0CDqI9gYplSgj6bPw9Frm5eZpbVq0fu2aLwUu1rA02m
G6/EFXAszhAH0cLSZAYWM6yebyOBshPaRlX8cykM47UQlFZg9ZcEfml0SrkoBpqwi7I7RPa2OOcJ
sQjPfsTYstmqzwPNqkUKyrYVW4U0yAHEjZSiXqq4q47+9WQ7s83SJOupHakJwBfqMQRAMe/HFyef
S7EdCiFDtdr2qwaCHD34McBqyFYz3YxQu/vf74XHKGhnQwX2G/ul2lpWbnDsascUw7ou1bEsg+yA
sshYFVN9iylLU1s+3amLTSuNu9MQ2aaNjYRZYiirft3X10rhr86L/5L9anEUfXNBpHKbnQSoNsvT
BkM3DBjq8XNT95XxcZCSHxfqwMr5eB0DnXC+8BbFW1v34ydPunaalVC5CitTPtR+fbcTZV6pBClH
lJZJq99yV1W9uBi1riDlCl2iHMgnOJd+XNbnFJW3NJY1XsekkVQLr2rfBQODjgNWwFZI4/2zi1ZK
Z19pZqfjee5gTAX8IuVxcsKeY648vgvUPfVr4aR4o9IGM/sr+njZkhRJjeFXiNzBKi0Oqki25qBB
c9AfpMQ9ewBABwA=

--_=_swift_v4_13496321625071c0a297e85_=_--

查找电子邮件之间的差异或使用SWIFTmailer