admin管理员组文章数量:1404927
My mail function is as follows
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;
}
Attachment returns an absolute path from the server. Because when i pasted it in the url, I got the pdf file. The mail also gets sent, but without the attachment. Please help.
My mail function is as follows
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;
}
Attachment returns an absolute path from the server. Because when i pasted it in the url, I got the pdf file. The mail also gets sent, but without the attachment. Please help.
Share Improve this question asked Nov 9, 2018 at 13:47 Simeon Mark FernandesSimeon Mark Fernandes 131 silver badge4 bronze badges1 Answer
Reset to default 2You said in your OP that the absolute path was returned, but you are using the 'baseurl' value which gives you a URL to the file that you can browse to. That's a URL, which is not the same as the path to the file in the file system.
This line is your issue:
$attachment = $upload_dir['baseurl'] . "/order-confirm/wardrobe-
".$build_id.".pdf";
This gives you a URL like this:
http://yoursite/directory/order-confirm/wardrobe-something.pdf
You can't attach a file to an email from a URL. You need the file path to the file on the server so you can attach it. Use the basedir
value to build the file path instead.
$attachment = $upload_dir['basedir'] . "/order-confirm/wardrobe-
".$build_id.".pdf";
The basedir
result will give you something like the following:
/yourserver/somepath/directory/order-confirm/wardrobe-something.pdf
With the path to the file, you can attach it to your email.
Here's your function revised to use basedir
instead of baseurl
(noted with a comment where I changed it):
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;
}
本文标签: emailwpmail not sending attachment
版权声明:本文标题:email - wp_mail not sending attachment 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744875000a2629862.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论