我正在尝试使用dompdf创建文件并将其附加到mime,而不进行保存。我从https://stackoverflow.com/a/12008553/9727103得到了答案,所以我使用了它,但是电子邮件中包含了某些意外内容。
这是我的部分代码。
$pdf = $dompdf->output();
$mm = new Mail_mime();
$msg = 'Hello world to all.';
$mm->setTxtBody($msg);
$mm->addAttachment($pdf,'application/pdf','output.pdf', false);
$body = $mm->get();
$subject = 'Hello World - ['.$customer.']';
$recipients = 'a@b.com';
$headers['From']= 'ab@bc.com';
$headers['To']= 'a@b.com';
$headers['Subject'] = $subject;
$params['host'] = 'smtp.sendgrid.net';
$params['port'] = '587';
$params['auth'] = true;
$params['username'] = 'my@user.name';
$params['password'] = 'my@user.pass';
$mail_object = Mail::factory('smtp', $params); // Print the parameters you are using to the page
// Send the message
$mail_object->send($recipients, $headers, $body);
if (PEAR::isError($mail_object)) {
echo("<p>" . $mail_object->getMessage() . "</p>");
} else {
echo("Message sent!");
}
它发送成功,但是我收到的电子邮件是这个。
--=_0db58b483b7f7e450132520a9d0c50fe
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
Hello World to all.
--=_0db58b483b7f7e450132520a9d0c50fe
Content-Transfer-Encoding: base64
Content-Type: application/pdf;
name=output.pdf
Content-Disposition: attachment;
filename=output.pdf;
size=5444
JVBERi0xLjMKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZwovT3V0bGluZXMgMiAwIFIKL1BhZ2Vz
IDMgMCBSID4+CmVuZG9iagoyIDAgb2JqCjw8IC9UeXBlIC9PdXRsaW5lcyAvQ291bnQgMCA+Pgpl
bmRvYmoKMyAwIG9iago8PCAvVHlwZSAvUGFnZXMKL0tpZHMgWzYgMCBSCl0KL0NvdW50IDEKL1Jl
c291cmNlcyA8PAovUHJvY1NldCA0IDAgUgovRm9udCA8PCAKL0YxIDggMCBSCi9GMiA5IDAgUgo+
PgovWE9iamVjdCA8PCAKL0kxIDE0IDAgUgo+PgovRXh0R1N0YXRlIDw8IAovR1MxIDE1IDAgUgov
R1MyIDE2IDAgUgo+Pgo+PgovTWVkaWFCb3ggWzAuMDAwIDAuMDAwIDYxMi4wMDAgNzkyLjAwMF0K
ID4+CmVuZG9iago0IDAgb2JqClsvUERGIC9UZXh0IC9JbWFnZUMgXQplbmRvYmoKNSAwIG9iago8
PAovUHJvZHVjZXIgKP7/AGQAbwBtAHAAZABmACAAKwAgAEMAUABEAEYpCi9DcmVhdGlvbkRhdGUg
....
附加的文件转换为字符串,并且有
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
我没打算。当我使用https://www.freeformatter.com/base64-encoder.html#ad-output复制pdf字符串并单击“解码并下载”时,它会下载我附加的pdf文件,但我希望它只是附带附件的普通电子邮件。
有什么办法可以解决这个问题?任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
我认为您错过了重点。 PDF已正确附加到您的电子邮件中。您的电子邮件(根据您的帖子)包含:
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1
并且:
Content-Transfer-Encoding: base64
Content-Type: application/pdf;
name=output.pdf
Content-Disposition: attachment;
filename=output.pdf;
size=5444
如果您在电子邮件客户端中查看此电子邮件,您将看到PDF作为附件出现,并且电子邮件中的消息将是“所有人的世界您好”。纯文本格式(即无格式)。