WP_MAIL未发送附件

时间:2018-11-09 作者:Simeon Mark Fernandes

我的邮件功能如下

add_action(\'wp_ajax_order_processing\', \'order_processing\');
function order_processing(){
include \'mail-translations.php\';

require_once( ABSPATH . \'wp-admin/includes/file.php\' );
global $wp_filesystem;
$upload_dir = wp_upload_dir();   
$dir = trailingslashit( $upload_dir[\'basedir\'] )  . \'order-confirm/\';

WP_Filesystem();
$wp_filesystem->mkdir( $dir );

$build_id=md5(uniqid(cvf_td_generate_random_code(), true));

$message = "<p>Hello</p>";    

$mpdf = new mPDF(\'utf-8\', \'A4-P\'); 
$mpdf->debug = true;
$mpdf->dpi = 150;
$mpdf->img_dpi = 150;
$mpdf->setAutoTopMargin = \'stretch\'; 
$mpdf->setAutoBottomMargin = \'stretch\';

$src = get_template_directory_uri() . \'/images/rsz_bomann-logo-black.png\';
$mpdf->SetWatermarkImage($src);
$mpdf->showWatermarkImage=true;
$mpdf->SetHTMLHeader(\'\');
// PDF footer content                      
$mpdf->SetHTMLFooter(\'\');
$mpdf->SetFont(\'helvetica\');


$mpdf->WriteHTML($message); // Writing html to pdf
// FOR EMAIL

$content = $mpdf->Output( $dir . "wardrobe-".$build_id.".pdf", \'F\'); 
$content = chunk_split(base64_encode($content));

$attachment = $upload_dir[\'baseurl\'] . "/order-confirm/wardrobe- 
".$build_id.".pdf";


// Set Mail Headers
$headers = "MIME-Version: 1.0" . "\\r\\n";
$headers .= "Content-type: text/html; charset=" . get_bloginfo(\'charset\') . 
"" . "\\r\\n";

$subject = __(\'Order Confirmation for Order No. \', \'skapnet\');

$order_mail = wp_mail("[email protected]", $subject, \'message\' , 
$headers, array($attachment));

echo $order_mail;

}
附件返回来自服务器的绝对路径。因为当我把它粘贴到url中时,我得到了pdf文件。邮件也会被发送,但没有附件。请帮忙。

1 个回复
最合适的回答,由SO网友:butlerblog 整理而成

您在OP中说返回了绝对路径,但您使用的是“baseurl”值,该值为您提供了可浏览到的文件的URL。这是一个URL,与文件系统中文件的路径不同。

这一行是您的问题:

$attachment = $upload_dir[\'baseurl\'] . "/order-confirm/wardrobe- 
".$build_id.".pdf";
这将为您提供如下URL:

http://yoursite.com/directory/order-confirm/wardrobe-something.pdf
You can\'t attach a file to an email from a URL. 您需要服务器上文件的文件路径,以便可以附加它。使用basedir 值来生成文件路径。

$attachment = $upload_dir[\'basedir\'] . "/order-confirm/wardrobe- 
".$build_id.".pdf";
Thebasedir 结果将为您提供如下信息:

/yourserver/somepath/directory/order-confirm/wardrobe-something.pdf
通过文件的路径,您可以将其附加到电子邮件中。

这是您修改后要使用的函数basedir 而不是baseurl (在我更改的地方加了一条注释):

add_action(\'wp_ajax_order_processing\', \'order_processing\');
function order_processing(){
    include \'mail-translations.php\';

    require_once( ABSPATH . \'wp-admin/includes/file.php\' );
    global $wp_filesystem;
    $upload_dir = wp_upload_dir();   
    $dir = trailingslashit( $upload_dir[\'basedir\'] )  . \'order-confirm/\';

    WP_Filesystem();
    $wp_filesystem->mkdir( $dir );

    $build_id=md5(uniqid(cvf_td_generate_random_code(), true));

    $message = "<p>Hello</p>";    

    $mpdf = new mPDF(\'utf-8\', \'A4-P\'); 
    $mpdf->debug = true;
    $mpdf->dpi = 150;
    $mpdf->img_dpi = 150;
    $mpdf->setAutoTopMargin = \'stretch\'; 
    $mpdf->setAutoBottomMargin = \'stretch\';

    $src = get_template_directory_uri() . \'/images/rsz_bomann-logo-black.png\';
    $mpdf->SetWatermarkImage($src);
    $mpdf->showWatermarkImage=true;
    $mpdf->SetHTMLHeader(\'\');
    // PDF footer content                      
    $mpdf->SetHTMLFooter(\'\');
    $mpdf->SetFont(\'helvetica\');


    $mpdf->WriteHTML($message); // Writing html to pdf
    // FOR EMAIL

    $content = $mpdf->Output( $dir . "wardrobe-".$build_id.".pdf", \'F\'); 
    $content = chunk_split(base64_encode($content));

    // Use \'basedir\' instead of \'baseurl\':
    $attachment = $upload_dir[\'basedir\'] . "/order-confirm/wardrobe- 
    ".$build_id.".pdf";


    // Set Mail Headers
    $headers = "MIME-Version: 1.0" . "\\r\\n";
    $headers .= "Content-type: text/html; charset=" . get_bloginfo(\'charset\') . 
    "" . "\\r\\n";

    $subject = __(\'Order Confirmation for Order No. \', \'skapnet\');

    $order_mail = wp_mail("[email protected]", $subject, \'message\' , 
    $headers, array($attachment));

    echo $order_mail;

}

结束

相关推荐

Register email as username

我在Wordpress多站点中使用Wordpress 4.9.4,我需要允许用户使用其电子邮件地址作为用户名进行注册。查看代码,我发现阻止我完成的是ms函数。php有一个正则表达式过滤除a-z以外的其他字符的用户名,因此当电子邮件是用户名时,我会得到“用户名只能包含小写字母(a-z)和数字。”我在ms函数中更改了该正则表达式。php,它工作得很好。现在我的问题是,我不想每次更新wordpress时都重新编码wpmu\\u validate\\u user\\u signup函数。在这个问题上有什么好的做法