drbe16008 2016-03-23 10:11
浏览 65
已采纳

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

i want to send an email with attachment using wordpress' wp_mail function.

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.

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • dongyun7571 2016-03-23 11:33
    关注

    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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名