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

dahlsdata-tahira

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( '[email protected]') , $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!

dahlsdata-tahira

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Send Datauri string as attachment in wp_mail

If there is no attachment file is present at path mail should not send

wp_mail doesn't send the attachment in WordPress

wp_mail() - Sending 'file' input type from form as attachment

Send mail with file attachment

PHPMailer send mail but file attachment is not with it

How to send html format massage using wp_mail()?

How to send HTML form data to wp_mail function on wordpress?

How to construct a File from Byte Array to send the file as an attachment in Email without saving the file or file path to disk?

Send attachment file in outgoing mail body in rails

How to pass the variable in attachment array path?

How to send pdf file as an attachment in Django

How to send excel file as an attachment in email in laravel?

How to send mail in Katalon ( attachment Screenshot on failure )

How to send multiple attachment in single mail in php

Wordpress, wp_mail send twice

Send mail with large attachment?

Mailgun send mail with attachment

send mail attachment powershell

send mail with attachment body

Unable to create and send dynamic CSV file as attachment over mail

NGINX send path as variable to PHP except if path is a file path

How do I send a generated csv file as an email attachment in React

How to send file attachment to user using contact form 7

How to send file attachment using smartsheet api sdk

How to Send Multiple File as attachment in Multipart Forms [HTTP POST Request]

How to write a new CSV file and send as Email attachment

How to use wp_mail() function in wordpress

Send a file as attachment in objective c