wp_mail发送带有编码字符串作为附件的电子邮件

时间:2017-03-02 03:04:53

标签: php wordpress base64

我该怎么办?基本上我有下面的代码,$ pdf是pdf文件的base64编码字符串

add_action('wp_ajax_email_portfolio', 'ajax_email_portfolio');

function ajax_email_portfolio()
{

    check_ajax_referer('ajax-email-portfolio', 'security');
    $pdf = $_POST['pdf'];
    $email_address = $_POST['email_address'];


    #NOTIFY USER
    $subject = 'Portfolio from somewhere';
    $headers = 'From: Someone <no-reply@somewhere.com>';
    $email_body = 'Portfolio from Someone';
    $destination_email = $email_address;

    wp_mail($destination_email, $subject, $email_body, $headers);

    echo json_encode(array('success' => true,'pdf'=> $pdf,'email_address'=>$email_address));

    die();
}

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

这是我的代码,它适用于gmail,但如果发送到outlook,它会输出`$ current_user = wp_get_current_user();

$pdf = $_POST['pdf'];
$email_address = $_POST['email_address'];
$eol = PHP_EOL;
$uid = md5(uniqid(time()));
$destination_email = $email_address;
$attachment_content = chunk_split($pdf);
$filename = "portfolio.pdf";
#NOTIFY USER
$subject = 'Portfolio from xxxxxx';
$headers = 'From: '. $current_user->user_firstname . ' <' . $current_user->user_email . '> '. $eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
$email_body = "--".$uid.$eol;
$email_body .= "Content-Transfer-Encoding: 7bit".$eol;
$email_body .= "Portfolio from xxxxxx".$eol;
// attachment
$email_body .= "--".$uid.$eol;
$email_body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$email_body .= "Content-Transfer-Encoding: base64".$eol;
$email_body .= "Content-Disposition: attachment".$eol;
$email_body .= $attachment_content.$eol;
$email_body .= "--".$uid."--";


wp_mail($destination_email, $subject, $email_body, $headers);`