Sending an e-mail with an attachment - having trouble...

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
sstarzyk
Forum Newbie
Posts: 2
Joined: Tue Oct 06, 2009 5:28 pm

Sending an e-mail with an attachment - having trouble...

Post by sstarzyk »

Hi,

I'm fairly new to php, and I'm trying to do an auto-respond type email when someone submits a form. I need the email body to have some text in it and then a pdf attached as well. I've pieced together this code from tutorials online, so it may be way off...but I successfully receive the email with the pdf attached, but no matter what I do, the body of the email is blank, no text at all.

Code: Select all

 
 
$file = 'filename.pdf';
 
//create a boundary string. 
 
$random_hash = md5(date('r', time()));
             
//define the headers we want passed. Note that they are separated with \r\n
 
$headers = 'From: ' . $replyFrom . "\r\nReply-To: " . $replyTo;
             
//add boundary string and mime type specification
 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-{$random_hash}\"";
             
//read the atachment file contents into a string, encode it with MIME base64
 
$attachment = chunk_split(base64_encode(file_get_contents($file)));
             
//define the body of the message.
 
$replybody = "Thank you for having an interest in our program.\n\n";
$replybody .= "Please view the attachment to learn about more the specific prices. Call us for more info!\n\n";
            
$emailBody = "--PHP-mixed-{$random_hash}\r\n"
          . "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"\r\n"
          . "--PHP-alt-{$random_hash}\r\n"
          . "Content-Type: text/html; charset=\"iso-8859-1\r\n"
          . "Content-Transfer-Encoding: 7bit\r\n"
          . "{$replybody}\r\n\r\n"  
          . "--PHP-alt-{$random_hash}\r\n"
          . "--PHP-mixed-{$random_hash}\r\n"
          . "Content-Type: application/pdf; name=\"Copier Packages.pdf\"\r\n"
          . "Content-Transfer-Encoding: base64\r\n"
          . "Content-Disposition: attachment\r\n\r\n"
          . "{$attachment}\r\n"
          . "--PHP-mixed-{$random_hash}--\r\n\r\n";
                  
$emailBody = wordwrap($emailBody, 75);
            
//send the email - these variable are defined earlier in the code...
 
$mail_sent = mail($replyEmail, $replySubject, $emailBody, $headers);
            
//if the message is sent successfully redirect back to page 
            
    if(!$mail_sent) {
        header('index.html');
        exit;
    }
 
 
Could someone help me with this please?? Thank you in advance!

Steve
eliavozeri
Forum Newbie
Posts: 7
Joined: Sat Sep 19, 2009 1:40 pm

Re: Sending an e-mail with an attachment - having trouble...

Post by eliavozeri »

sstarzyk wrote:Hi,

I'm fairly new to php, and I'm trying to do an auto-respond type email when someone submits a form. I need the email body to have some text in it and then a pdf attached as well. I've pieced together this code from tutorials online, so it may be way off...but I successfully receive the email with the pdf attached, but no matter what I do, the body of the email is blank, no text at all.

Code: Select all

 
 
$file = 'filename.pdf';
 
//create a boundary string. 
 
$random_hash = md5(date('r', time()));
             
//define the headers we want passed. Note that they are separated with \r\n
 
$headers = 'From: ' . $replyFrom . "\r\nReply-To: " . $replyTo;
             
//add boundary string and mime type specification
 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-{$random_hash}\"";
             
//read the atachment file contents into a string, encode it with MIME base64
 
$attachment = chunk_split(base64_encode(file_get_contents($file)));
             
//define the body of the message.
 
$replybody = "Thank you for having an interest in our program.\n\n";
$replybody .= "Please view the attachment to learn about more the specific prices. Call us for more info!\n\n";
            
$emailBody = "--PHP-mixed-{$random_hash}\r\n"
          . "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"\r\n"
          . "--PHP-alt-{$random_hash}\r\n"
          . "Content-Type: text/html; charset=\"iso-8859-1\r\n"
          . "Content-Transfer-Encoding: 7bit\r\n"
          . "{$replybody}\r\n\r\n"  
          . "--PHP-alt-{$random_hash}\r\n"
          . "--PHP-mixed-{$random_hash}\r\n"
          . "Content-Type: application/pdf; name=\"Copier Packages.pdf\"\r\n"
          . "Content-Transfer-Encoding: base64\r\n"
          . "Content-Disposition: attachment\r\n\r\n"
          . "{$attachment}\r\n"
          . "--PHP-mixed-{$random_hash}--\r\n\r\n";
                  
$emailBody = wordwrap($emailBody, 75);
 
Could someone help me with this please?? Thank you in advance!
Steve
hi steve i'm sort of new to the php world too but did notice one thing.
the text in $emailBody if you try and print on screen will return a parse error
it should look like this:

$emailBody = "--PHP-mixed-{$random_hash}\r\n"
$emailBody =$emailBody . "Content-Type: multipart/alternative; boundary=\"PHP-alt-{$random_hash}\"\r\n"
$emailBody =$emailBody . "--PHP-alt-{$random_hash}\r\n"

and so on in order for all the information to be in the $emailbody variable..
check the $replybody and $headers as well.....
good luck
ceiroa
Forum Newbie
Posts: 11
Joined: Thu Oct 08, 2009 4:14 pm

Re: Sending an e-mail with an attachment - having trouble...

Post by ceiroa »

Steve,
The way you construct $emailBody seems fine to me. I think the problem is that all you have as email body should actually be in the headers.

You don’t show the line that actually sends the email, but I guess it’s there somewhere, since you say it works but it does not have text in the body. Try setting all the headers separate from the email body, as follows:

Code: Select all

 
$to      = 'you@fake.com';
$subject = 'Subject';
$message = 'body of the message';
$headers = 'From: me@fake.com' . "\r\n”;
$headers .= 'Reply-To: me@fake.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 
And then pass the headers as an argument to the mail function:

Code: Select all

 
mail($to, $subject, $message, $headers); 
 
-----------------------------
Carlos R. M. Eiroa
PHP Consultant
sstarzyk
Forum Newbie
Posts: 2
Joined: Tue Oct 06, 2009 5:28 pm

Re: Sending an e-mail with an attachment - having trouble...

Post by sstarzyk »

Thank you everyone for you help!!

I was still researching this as well, and I found the easiest way to do it without problems is just to have someone else do it for you! I found phpmailer to do exactly what I need, it was finding the right example of how to use it. Phpmailer is easy to use and does all the behind the scenes mime stuff for you, works great.

Thanks again!
Post Reply