使用pdf附件发送PHP电子邮件
Trying to send an email with pdf attachment, tried using swiftmailer and that did not work, this code worked with a zip but not PDF :(
尝试发送带有pdf附件的电子邮件,尝试使用swiftmailer但是没有用,这段代码使用了zip而不是PDF :(
$attachment = chunk_split(base64_encode(file_get_contents($filename)));
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?php echo $message."<br /><br />";
?>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/octet-stream; name="<?php echo $filename?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?php echo $filename?>"
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
Mails gets sent fine and I get the mail: but the attachment is not there and in the meial has all the base64 encoded in the email like:
邮件被发送正常,我收到邮件:但附件不存在,并在meial中有所有base64编码在电子邮件中,如:
ontent-Type: application/octet-stream; name="media.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="media.pdf" JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1BhZ2Vz IDMgMCBSID4+CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9PdXRsaW5lcyAvQ291bnQgMCA+Pgpl bmRvYmoKMyAwIG9iago8PCAvVHlwZSAvUGFnZXMKL0tpZHMgWzYgMCBSCjE2IDAgUgpdCi9Db3Vu dCAyCi9SZXNvdXJjZXMgPDwKL1Byb2NTZXQgNCAwIFIKL0ZvbnQgPDwgCi9GMSA4IDAgUgovRjIg OSAwIFIKPj4KL1hPYmplY3QgPDwgCi9JMSAxMiAwIFIKL0kyIDE1IDAgUgovSTMgMjAgMCBSCi9J NCAyMyAwIFIKPj4KPj4KL01lZGlhQm94IFswLjAwMCAwLjAwMCA2MTIuMDAwIDc5Mi4wMDBdCiA+ PgplbmRvYmoKNCAwIG9iagpbL1BERiAvVGV4dCAvSW1hZ2VDIF0KZW5kb2JqCjUgMCBvYmoKPDwK L0NyZWF0b3IgKERPTVBERikKL0NyZWF0aW9uRGF0ZSAoRDoyMDEzMDgyMzAyMDA0NCswMCcwMCcp Ci9Nb2REYXRlIChEOjIwMTMwODIzMDIwMDQ0KzAwJzAwJykKPj4KZW5kb2JqCjYgMCBvYmoKPDwg L1R5cGUgL1BhZ2UKL1BhcmVudCAzIDAgUgovQW5ub3RzIFsgMTAgMCBSIDEzIDAgUiBdCi9Db250 ZW50cyA3IDAgUgo+PgplbmRvYmoKNyAwIG9iago8PCAvRmlsdGVyIC9GbGF0ZURlY29kZQovTGVu Z3RoIDI3OCA+PgpzdHJlYW0KeJyFkb1Ow0AQhHs/xZRQsNm9/2tRAnJEA1wXpUBJSIOFIAWvz9ox FwMKyNLJmtuZb3evYWJmTM/3fXNdYJIljhbRGnIxoWwxuzEQT4zyDKwuSlvuFpdrlCUWpfmd83Cr YvT4AGOJFbDWn21Tg00mYYsO1iTKJlXlBY/fnYaMH90uE0tAZNVSVrMPhnJyVfnL3NcGzkNtCG50
ontent-Type:application / octet-stream; name =“media.pdf”Content-Transfer-Encoding:base64 Content-Disposition:attachment;文件名= “media.pdf” JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1BhZ2Vz IDMgMCBSID4 + CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9PdXRsaW5lcyAvQ291bnQgMCA + PGPL bmRvYmoKMyAwIG9iago8PCAvVHlwZSAvUGFnZXMKL0tpZHMgWzYgMCBSCjE2IDAgUgpdCi9Db3Vu dCAyCi9SZXNvdXJjZXMgPDwKL1Byb2NTZXQgNCAwIFIKL0ZvbnQgPDwgCi9GMSA4IDAgUgovRjIg OSAwIFIKPj4KL1hPYmplY3QgPDwgCi9JMSAxMiAwIFIKL0kyIDE1IDAgUgovSTMgMjAgMCBSCi9J NCAyMyAwIFIKPj4KPj4KL01lZGlhQm94IFswLjAwMCAwLjAwMCA2MTIuMDAwIDc5Mi4wMDBdCiA + PgplbmRvYmoKNCAwIG9iagpbL1BERiAvVGV4dCAvSW1hZ2VDIF0KZW5kb2JqCjUgMCBvYmoKPDwK L0NyZWF0b3IgKERPTVBERikKL0NyZWF0aW9uRGF0ZSAoRDoyMDEzMDgyMzAyMDA0NCswMCcwMCcp Ci9Nb2REYXRlIChEOjIwMTMwODIzMDIwMDQ0KzAwJzAwJykKPj4KZW5kb2JqCjYgMCBvYmoKPDwg L1R5cGUgL1BhZ2UKL1BhcmVudCAzIDAgUgovQW5ub3RzIFsgMTAgMCBSIDEzIDAgUiBdCi9Db250 ZW50cyA3IDAgUgo + PgplbmRvYmoKNyAwIG9iago8PCAvRmlsdGVyIC9GbGF0ZURlY29kZQovTGVu Z3RoIDI3OCA + PgpzdHJlYW0KeJyFkb1Ow0AQhHs / xZRQsNm9 / 2tRAnJEA1wXpUBJSIOFIAWvz9ox FwMKyNLJmtuZb3evYWJmTM / 3fXNdYJIljhbRGnIxoWwxuzEQT4zyDKw uSlvuFpdrlCUWpfmd83Cr YvT4AGOJFbDWn21Tg00mYYsO1iTKJlXlBY / fnYaMH90uEttNVSVrMPhnJyVfnL3NcGzkNtCG50
1 个解决方案
#1
0
Based on the partial message (ontent-Type: ...
), I'm guessing that the output buffer filled up and was automatically flushed, leaving only the output after the flush to be assigned to $message
.
基于部分消息(ontent-Type:...),我猜测输出缓冲区已填满并自动刷新,只留下刷新后的输出分配给$ message。
There are also two extra blank lines between --PHP-mixed-<?php echo $random_hash; ?>
and Content-Type: application/octet-stream; ...
which could cause trouble.
在-PHP-mixed - 和Content-Type:application / octet-stream; ......这可能会带来麻烦。
Relying on output buffering to construct a string is both error prone and completely unnecessary. It is much better to use PHP's HEREDOC syntax instead:
依靠输出缓冲来构造字符串既容易出错又完全没有必要。使用PHP的HEREDOC语法要好得多:
$message = <<<MSG
--PHP-mixed-$random_hash
Content-Type: multipart/alternative; boundary="PHP-alt-$random_hash"
--PHP-alt-$random_hash
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
$message<br /><br />
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: application/octet-stream; name="$filename"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$filename"
$attachment;
--PHP-mixed-$random_hash--
MSG;
$mail_sent = @mail( $to, $subject, $message, $headers );
Note that the line endings in a mail must be CRLF (eg. \r\n). If the above doesn't work, you may have to construct a string with explicit line endings:
请注意,邮件中的行结尾必须是CRLF(例如\ r \ n)。如果上述方法不起作用,则可能必须构造一个带有显式行结尾的字符串:
$message = "--PHP-mixed-$random_hash\r\n"
. "Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n"
. "\r\n"
/* ... */
. $attachment
. "\r\n--PHP-mixed-$random_hash--"
. "\r\n";
See PHP's mail() manual page for some further details.
有关更多详细信息,请参阅PHP的mail()手册页。
更多相关文章
- php逐个汉字遍历字符串
- php 把驼峰样式的字符串转换成下划线样式的字符串
- 返回key包含此字符串的值
- PHP邮件脚本占用了大量资源
- PHP将邮件发送到多个电子邮件地址
- 如何在发送之前更改SOAP请求中的名称空间前缀(皇家邮件 - 发送请
- 仅在两个字符串之间移除空白。
- PHP使用gmail发邮件
- 在PHP中写入xml时,从字符串中删除&