I have a form where I want users to be able to select an image to upload. The image gets temporarily uploaded to the wp-content/uploads folder, is attached to the e-mail and then is destroyed.

Right now, the form is submitting and I can see that the image is being processed in the response headers, but there is no image included in the e-mail and there is no image file in the uploads folder. Does anyone see any issues with this code?

Here's the form:

<form id="shareForm" action="<?php echo get_template_directory_uri(); ?>/library/file-form.php" method="post" enctype="multipart/form-data">
    <input type="text" id="name" name="name" placeholder="Name" />
    <textarea rows="4" cols="50" name="message" id="message" placeholder="Message"></textarea>
    <input type="file" id="photo" name="photo" class="upload">
    <input type="submit" name="submit" value="Submit">

And here's my PHP mail script:

    require( '../../../../wp-load.php' );


    $name = $_POST["name"];
    $message = $_POST["message"];

    $fName = str_replace(array(' ', ',', '\'' ), '-');

    $date = new DateTime(null, new DateTimeZone('America/Chicago'));
    $myDate = $date->format("m/d/Y H:i");
    $fileDate = $date->format("m-d-Y");
    $homeUrl = home_url('/');

    if($_FILES["file"]["type"] != ""){

        $allowedExts = array("jpg", "png", "gif");
        $extension = end(explode(".", $_FILES["file"]["name"]));
        $newFileName = $fName . $fileDate."." .$extension;

        if ($_FILES["file"]["error"] > 0) {
            $myPath =  $homeUrl . 'share/?upload=error';
            wp_redirect($myPath); exit;     

        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], WP_CONTENT_DIR . "/uploads/user-submitted/" . $newFileName);


    $to = '[email protected]';
    $subject = $fName . " submitted a photo";

    $message = "Date: " . $myDate . " EST \n";
    $message .= "Message:  " .$message. "\n";

    $attachments = array(WP_CONTENT_DIR . "/uploads/user-submitted/".$newFileName); 
    $headers[] = 'From: Photo Submissions <[email protected]>';

    wp_mail( $to, $subject, $message, $headers, $attachments);

    unlink(WP_CONTENT_DIR . "/uploads/user-submitted/" . $newFileName);

    $myPath =  $homeUrl . 'share/?upload=success';


  • You should really consider looking at wp_mail(). Just search the site to see some examples.
    – kaiser
    Commented Jul 2, 2014 at 22:09

1 Answer 1


Attachments should always use the absolute filesystem path.

Also to change the Content-Type of the email you should use the wp_mail_content_type filter.

function my_custom_email() {
    $to  = '[email protected]';
    $subject = 'WordPress wp_mail';
    $message = '
        <table rules="all" style="border-color: #666;" cellpadding="10">
          <tr>Hello WordPress</tr>

    $attachments = array(  WP_PLUGIN_DIR . '/my-plugin/uploads/sample_photo_01.jpg' );
    $headers[] = 'From: '.get_option( 'blogname' ).' <'.get_option( 'admin_email' ).'>';
    add_filter( 'wp_mail_content_type', 'my_custom_email_content_type' );
    return wp_mail( $to, $subject, $message, $headers, $attachments );

function my_custom_email_content_type() {
    return 'text/html';

I have placed the entire code in a function so that the wp_mail_content_type filter applies only to this email.




OR You can try this code

 $attachments = array( WP_CONTENT_DIR . '/uploads/file_to_attach.zip' );
   $headers = 'From: My Name <[email protected]>' . "\r\n";
   wp_mail('[email protected]', 'subject', 'message', $headers, $attachments );

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.