Sending jpeg attachments with mail

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eight17
Forum Newbie
Posts: 1
Joined: Sat Dec 28, 2002 8:48 pm

Sending jpeg attachments with mail

Post by eight17 »

I have a form which sends HTML mail, optionally with jpeg images as attachments. Without the images it works fine. when images are sent, they seem to be attached fine but will not open as if they are corrupt or something. :(

The form (with enctype="multipart/form-data" of course) sends $fname, $lname, $testimony and optionally the uploaded files $before and $after to the following script. Note, this is only the part that handles the mail if there is an image sent through the form:

Code: Select all

// determine the file types of the images being uploaded
    $imgType = "";
    if ($before_size) $bType = $before_type;
    if ($after_size) $aType = $after_type;
    
    if (!strpos($bType, "jpeg") || !strpos($aType, "jpeg")) echo "<p>One of the images you tried to upload were not jpeg! Please click the "back" button and try another file or files</p>";
    else {
        // prepare input for mailing
        $mail_result = "<p>";
        $fname = trim($fname);
        $lname = trim($lname);
        $testimony = nl2br(htmlentities(stripslashes(trim($testimony))));
    
        if (isset($WINDIR)) {
            $before = str_replace("\","", $before);
            $after = str_replace("\","", $after);
        }
        
        // get image file information and make sure their the right format
        // and, of course, don't mail anything if there's a problem
        if ($before_size) {
            $bFile = fopen($before, "r");
            $beforeImg = fread($bFile, filesize($before));
            $beforeImg = chunk_split(base64_encode($beforeImg));
        }
        if ($after_size) {
            $aFile = fopen($after, "r");
            $afterImg = fread($aFile, filesize($after));
            $afterImg = chunk_split(base64_encode($afterImg));
        }

        // construct multi-part header
        $mail_boundary = md5(uniqid(time()));
        $headers = "Mime-Version: 1.0\r\nContent-Type: multipart/mixed;boundary="$mail_boundary"";
        $headers .= "\r\n\r\nThis is a multi-part message in MIME format.\r\n\r\n";
        
        // format all input for e-mailing
        $headers .= "From: "Herbagirlz Testimonials page"<testimonials@someurl.com>\n";
        $subject = "$name has sent a testimonial.";
        $mailto = "webmaster@someurl.com";

        // html portion of the e-mail
        $body = "--$mail_boundary\n";
        $body .= "Content-Type: text/html\n";
        $body .= "Content-transfer-encoding: 8bit\r\n\r\n";
        $body .= "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n<title>Catalog Request</title>\n<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />\n<meta name="Author" content="Ben Lawson, EightSeventeen Development" /><style type="text/css">\n<!--\nbody, td {font: 12px Verdana, Arial, Helvetica, sans-serif; color: #000000}\n-->\n</style>\n</head>";
        $body .= "<body bgcolor="#FFFFFF" text="#000000" link="#0000CC" vlink="#0000CC" alink="#0000CC">\n";

        $body .= "<p>My name is $fname $lname and I have the following testimonial to share:</p>\n";
        $body .= "<p>$testimony</p>\n\n";
        $body .= "\n</body>\n</html>\r\n\r\n";
        
        // attachements portion of the e-mail
        if ($before_size) {
            $body .= "--$mail_boundary\n";
            $body .= "Content-Type: $imgType; name=before.jpg\n";
            $body .= "Content-transfer-encoding: base64\r\n\r\n";
            $body .= $before . "\r\n\r\n";
        }
        
        if ($after_size) {
            $body .= "--$mail_boundary\n";
            $body .= "Content-Type: $imgType; name=after.jpg\n";
            $body .= "Content-transfer-encoding: base64\r\n\r\n";
            $body .= $after . "\r\n\r\n";
        }
        
        $body .= "--$mail_boundary--";
    
        // send mail message
        if ($sendSuccess = (mail($mailto, $subject, $body, $headers))) $mail_result = "<p>Your testimony and image(s) has been sent. Thank you!</p>";
        else $mail_result = "<p><b>There was an error in submitting your request. Please click your "back" or "refresh" button and try submitting again.</b></p>"; 
        //end of mail form
        echo $mail_result;
    }
Please let me know what I am doing wrong! This is driving me up a wall. :x
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Sending jpeg attachments with mail

Post by kendall »

egith17,

Yo man believe it or not im having the same damn porblem...its driving me up the wal too....i'm suprised were haven't met on the same wall..

any way here my thread...
viewtopic.php?p=59764#59764

I have researched this problem and have noted that someone said
Then send the thing, using the mail function as described in the manual. You will probably have to have a few goes at this before you get it right. The linbreaks need to be exactly right for this to work, and if it doesn't, that's probably why.
Now i dont no WTF that means...im trying the kitchen sink...But thats my 2 cents...hope it helps...i think not

Kendall
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Yo man,

Check out my thread i may have gotten the answer...i just tried it and it works

Kendall
Post Reply