1

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">
</form>

And here's my PHP mail script:

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

if(isset($_POST["name"])){

    $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 = 'my@email.com';
    $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 <my@email.com>';

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

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

    $myPath =  $homeUrl . 'share/?upload=success';
    wp_redirect($myPath); 
    exit;   

}

?>
  • You should really consider looking at wp_mail(). Just search the site to see some examples. – kaiser Jul 2 '14 at 22:09
0

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.

<?php
function my_custom_email() {
    $to  = 'my@email.com';
    $subject = 'WordPress wp_mail';
    $message = '
    <html>
    <body>
        <table rules="all" style="border-color: #666;" cellpadding="10">
          <tr>Hello WordPress</tr>
        </table>          
    </body>
    </html>
    ';

    $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.

Sources:

http://codex.wordpress.org/Function_Reference/wp_mail

http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail_content_type

OR You can try this code

 $attachments = array( WP_CONTENT_DIR . '/uploads/file_to_attach.zip' );
   $headers = 'From: My Name <myname@example.com>' . "\r\n";
   wp_mail('test@example.org', 'subject', 'message', $headers, $attachments );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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