我们有一个DATAURL pdf文件,我们正在尝试将其转换并以wp_mail()
作为附件发送,
$str = "data:application/pdf;base64,JVBERi0xLjMKJf////8KNSAwIG9iago8PAovVHlwZSAvUGFnZQovUGFyZW50IDEgMCBSCi9NZWRpYUJveCBbMCAwIDU5NS4yOCA4NDEuODldCi9Db250ZW50cyAzIDAgUgovUmVzb3VyY2VzIDQgMCBSCj4+CmVuZG9iago0IDAgb2JqCjw8Ci9Qcm9jU2V0IFsvUERGIC9UZXh0IC9JbWFnZUIgL0ltYWdlQyAvSW1hZ2VJXQovRm9udCA8PAovRjEgNiAwIFIKPj4KPj4KZW5kb2JqCjcgMCBvYmoKPDwKL1Byb2R1Y2VyIChwZGZtYWtlKQovQ3JlYXRvciAocGRmbWFrZSkKL0NyZWF0aW9uRGF0ZSAoRDoyMDE4MDcwODA1MTE0OVopCj4+CmVuZG9iago5IDAgb2JqCjw8Ci9UeXBlIC9Gb250RGVzY3JpcHRvcgovRm9udE5hbWUgL0lCV0dRQStSb2JvdG8tUmVndWxhcgovRmxhZ3MgNAovRm9udEJCb3ggWy03MzYuODE2NDA2IC0yNzAuOTk2MDk0IDExNDguNDM3NSAxMDU2LjE1MjM0NF0KL0l0YWxpY0FuZ2xlIDAKL0FzY2VudCA5MjcuNzM0Mzc1Ci9EZXNjZW50IC0yNDQuMTQwNjI1Ci9DYXBIZWlnaHQgNzEwLjkzNzUKL1hIZWlnaHQgNTI4LjMyMDMxMwovU3RlbVYgMAovRm9udEZpbGUyIDggMCBSCj4+CmVuZG9iagoxMCAwIG9iago8PAovVHlwZSAvRm9udAovU3VidHlwZSAvQ0lERm9udFR5cGUyCi9CYXNlRm9udCAvSUJXR1FBK1JvYm90by1SZWd1bGFyCi9DSURTeXN0ZW1JbmZvIDw8Ci9SZWdpc3RyeSAoQWRvYmUpCi9PcmRlcmluZyAoSWRlbnRpdHkpCi9TdXBwbGVtZW50IDAKPj4KL0ZvbnREZXNjcmlwdG9yIDkgMCBSCi9XIFswIFs5MDggNz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wMDAwMDAyMTQgMDAwMDAgbiAKMDAwMDAwMTU4MCAwMDAwMCBuIAowMDAwMDAwMzA4IDAwMDAwIG4gCjAwMDAwMDA1NzIgMDAwMDAgbiAKMDAwMDAwMTA3OCAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDEyCi9Sb290IDIgMCBSCi9JbmZvIDcgMCBSCj4+CnN0YXJ0eHJlZgozMDEzCiUlRU9GCg==";
$str = explode(',', $str)[1];
$pdf = base64_decode($str);
$eol = PHP_EOL;
$uid = md5(uniqid(time()));
$to = 'toemail';
$attachment_content = chunk_split($pdf);
////Email content
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$email_body = "--" . $uid . $eol;
$email_body .= "Content-Transfer-Encoding: 7bit" . $eol;
$email_body .= "Test Mail" . $eol;
////Attachment
$email_body .= "--" . $uid . $eol;
$email_body .= "Content-Type: application/pdf; name=\"test.pdf\"" . $eol;
$email_body .= "Content-Transfer-Encoding: base64" . $eol;
$email_body .= "Content-Disposition: attachment; filename=\"test.pdf\"" . $eol. $eol;
$email_body .= $attachment_content . $eol;
$email_body .= "--" . $uid . $eol;
wp_mail($to, "Test Email", $email_body, $headers);
但是电子邮件仅以字符串发送。这是怎么回事?
答案 0 :(得分:2)
如果您在正文中发送带有不同“内容类型”的电子邮件,则必须在标头中使用“主要内容类型”。
此“主要内容类型”可能是:
您应为多份/混合。
问题是,在Stackoverflow上的此帖子“ Sending multipart (text/html) emails via wp_mail() will likely get your domain banned”和在trac上的相关帖子“ wp_mail() sets Content-Type header twice for multipart emails”之后,PhpMailer从电子邮件正文中检测到不正确的“ Content-Type”。
要解决此问题,Stackoverflow用户“ bonger”通过覆盖可插拔功能“ wp_mail”构建了一种解决方法。在他的解决方法中,变量“ message”可以是一个关联数组,使用数组键可以控制内容类型。
您可以在以下链接中找到原始代码:https://wordpress.stackexchange.com/a/191974
我稍微扩展了Bonger的代码,以便可以发送Base64编码的附件。
elseif ( is_array($message) ) {
foreach ($message as $type => $body)
{
if ($type === 'text/html') {
$phpmailer->Body = $body;
}
elseif ($type === 'text/plain') {
$phpmailer->AltBody = $body;
}
elseif ( $type === 'base64')
{
$phpmailer->addStringAttachment( $body['content'], $body['name'] );
}
else
{
$phpmailer->AddAttachment($body, '', 'base64', $type);
}
}
}
测试用例:
function my_test_func()
{
$to = "test@gmail.com";
$subject = 'wp_mail testing multipart';
// your pdf as base64
$str = "data:application/pdf;base64,JVBERi0xLjMKJf////8KNSAwIG9iago8PAovVHlwZSAvUGFnZQovUGFyZW50IDEgMCBSCi9NZWRpYUJveCBbMCAwIDU5NS4yOCA4NDEuODldCi9Db250ZW50cyAzIDAgUgovUmVzb3VyY2VzIDQgMCBSCj4+CmVuZG9iago0IDAgb2JqCjw8Ci9Qcm9jU2V0IFsvUERGIC9UZXh0IC9JbWFnZUIgL0ltYWdlQyAvSW1hZ2VJXQovRm9udCA8PAovRjEgNiAwIFIKPj4KPj4KZW5kb2JqCjcgMCBvYmoKPDwKL1Byb2R1Y2VyIChwZGZtYWtlKQovQ3JlYXRvciAocGRmbWFrZSkKL0NyZWF0aW9uRGF0ZSAoRDoyMDE4MDcwODA1MTE0OVopCj4+CmVuZG9iago5IDAgb2JqCjw8Ci9UeXBlIC9Gb250RGVzY3JpcHRvcgovRm9udE5hbWUgL0lCV0dRQStSb2JvdG8tUmVndWxhcgovRmxhZ3MgNAovRm9udEJCb3ggWy03MzYuODE2NDA2IC0yNzAuOTk2MDk0IDExNDguNDM3NSAxMDU2LjE1MjM0NF0KL0l0YWxpY0FuZ2xlIDAKL0FzY2VudCA5MjcuNzM0Mzc1Ci9EZXNjZW50IC0yNDQuMTQwNjI1Ci9DYXBIZWlnaHQgNzEwLjkzNzUKL1hIZWlnaHQgNTI4LjMyMDMxMwovU3RlbVYgMAovRm9udEZpbGUyIDggMCBSCj4+CmVuZG9iagoxMCAwIG9iago8PAovVHlwZSAvRm9udAovU3VidHlwZSAvQ0lERm9udFR5cGUyCi9CYXNlRm9udCAvSUJXR1FBK1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jAwMDAwMDEwMjEgMDAwMDAgbiAKMDAwMDAwMDk3MiAwMDAwMCBuIAowMDAwMDAxMzkxIDAwMDAwIG4gCjAwMDAwMDAxMjUgMDAwMDAgbiAKMDAwMDAwMDAxNSAwMDAwMCBuIAowMDAwMDAwODI0IDAwMDAwIG4gCjAwMDAwMDAyMTQgMDAwMDAgbiAKMDAwMDAwMTU4MCAwMDAwMCBuIAowMDAwMDAwMzA4IDAwMDAwIG4gCjAwMDAwMDA1NzIgMDAwMDAgbiAKMDAwMDAwMTA3OCAwMDAwMCBuIAp0cmFpbGVyCjw8Ci9TaXplIDEyCi9Sb290IDIgMCBSCi9JbmZvIDcgMCBSCj4+CnN0YXJ0eHJlZgozMDEzCiUlRU9GCg==";
$str = explode(',', $str)[1];
$message['text/plain'] = 'Hello world! This is plain text...';
$message['base64'] = array('content' => base64_decode($str), 'name' => 'test.pdf');
$message['text/html'] = '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>Hello World! This is HTML...</p>
</body>
</html>';
// send email
wp_mail( $to, $subject, $message );
}
add_action('init', 'my_test_func');
使用完整链接: