1
//1st:   
move_uploaded_file($_FILES["upload_file"]["tmp_name"], WP_CONTENT_DIR.'/uploads/'.basename($_FILES['attachment']['name']));
$attachments = array(WP_CONTENT_DIR ."/uploads/".$_FILES["attachment"]["name"]);

//2nd
$attachment = array($_FILES['upload_file'][tmp_name]);

//wp_mail function:
if ( wp_mail( $to_email, $email_subject, $email_body, $header, $attachment) ) {
            _e("Message Sent Successfully!", 'framework');
        } else {
            _e("Server Error: WordPress mail method failed!", 'framework');
        }

The file I want to send is named as "xxx.pdf"

For the 1st code, I can upload my file to my server, with the same name as the original file, but the email I received does not contain any attachment.

For the 2nd code, I receive the attachment but without any specified format (e.g. phpUOtAZU). However, when I add the file extension (e.g. phpUOtAZU.pdf), it will have the same content as the file that I want to be sent.

How can I receive a email with the original file as the attachment (e.g. xxx.pdf)? If more code's information is needed, please tell me.

1 Answer 1

0

I notice in #1 you have $_FILES['upload_file']['tmp_name'] but then $_FILES['attachment']['name'] - referencing a different file input ('attachment' rather than 'upload_file'). Couldn't be that could it?

You've also missed off the basename() function when defining $attachments. So does this work?

$attachment = WP_CONTENT_DIR.'/uploads/'.basename($_FILES['upload_file']['name']));
move_uploaded_file($_FILES["upload_file"]["tmp_name"], $attachment);

if ( wp_mail( $to_email, $email_subject, $email_body, $header, array($attachment)) ) {
    _e("Message Sent Successfully!", 'framework');
} else {
    _e("Server Error: WordPress mail method failed!", 'framework');
}

This works for me, I'm doing almost exactly the same thing.

If you're writing a plugin I think good WordPress etiquette would be to have your own folder within /upload/ which you could create on plugin activation and remove on deactivation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.