简体   繁体   English

如何发送带有附件的wp_mail(变量中的文件路径)

[英]How to send wp_mail with attachment (file-path in variable)

i want to send an email with attachment using wordpress' wp_mail function. 我想使用wordpress的wp_mail函数发送带有附件的电子邮件。

With this function it works fine to send emails but the attachment isn't there when i check my email. 使用此功能可以很好地发送电子邮件,但是当我检查电子邮件时附件不存在。

function dd_send_email(){

    $dd_path = $_POST['upload_image'];
    // echo $dd_path;

    $email_sent = false;

    // get email template data
    $email_template_object = dd_get_current_options();


    // if email template data was found
    if ( !empty( $email_template_object ) ):

        // setup wp_mail headers
        $wp_mail_headers = array('Content-Type: text/html; charset=UTF-8');
        $mail_attachment = $dd_path; 

        // use up_mail to send email
        $email_sent = wp_mail( array( 'example@mail.no') , $email_template_object['dd_emne_forhaandsbestilling'], $email_template_object['dd_email_text_forhaandsbestilling'], $wp_mail_headers, $mail_attachment );

    endif;

    return $email_sent;

}

The variable $dd_path (something like: http://localhost/norskeanalyser/wp-content/uploads/Testanalyse-2.pdf ) contains the path of the file which i do upload from the media uploader in another function. 变量$dd_path (类似: http://localhost/norskeanalyser/wp-content/uploads/Testanalyse-2.pdf )包含文件的路径,我确实在另一个函数中从媒体上传器上传了文件。

Thanks for your help! 谢谢你的帮助!

I did found the answer by myself. 我确实找到了答案。 Because wp_mail needs the file path like this /uploads/2016/03/example.jpg we have to convert our url to a path like this. 因为wp_mail需要像/uploads/2016/03/example.jpg这样的文件路径,所以我们必须将url转换成这样的路径。

I did achive this by doing the following and thought i can share it with you: 我确实做到了以下几点,并认为可以与您分享:

    // change url to path   
    $dd_url = $_POST['upload_image'];
    $dd_path = parse_url($dd_url);

    // cut the path to right directory
    $array = explode("/norskeanalyser/wp-content", $dd_path['path']);
    unset($array[0]);
    $text = implode("/", $array);

and then i did save the $text into $mail_attachment and called it in wp_mail like above. 然后我确实将$text保存到$mail_attachment并像上面在wp_mail中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM