email with attachment

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
User avatar
abzu
Forum Newbie
Posts: 8
Joined: Tue Aug 29, 2006 3:39 pm

email with attachment

Post by abzu »

I have been trying to get a form on my site that will allow the user to send an email with an attachment. I located a script thorugh a search that is supposed to allow me to do just that with php. Unfortunately the script seems to be doing everything right except sending the email. I have played with it a bit and found that the problem is somewhere in the $hdr variable, because if I remove that variable from the mail() function, it goes through fine. Of course I kind of need the $hdr variable. Here is the script. Can anyone look at how $hdr is built and see if there are any glaring issues with the code that would cause this problem?

Code: Select all

<?php
if(!isset($_REQUEST['submit'])){
?>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">
<table>
<tr>
<td>To:</td>
<td><input type="text" name="to" size="40"/></td>
</tr>
<tr>
<td>From:</td>
<td><input type="text" name="from" size="40" /></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" name="re" size="40" /></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea cols="30" rows="5" name="comments"></textarea></td>
</tr>
<tr>
<td>Attachment:</td>
<td><input type="file" name="att" size="26" /></td>
</tr>
<td colspan="2"><input type="submit" name="submit" value="Send Form" /></td>
</tr>
</table>
</form>
<?php
}else{
extract($_POST);

        $fp = fopen( $att, "r");

        $file = fread( $fp, $att_size );

        /*
Encode The Data For Transition using base64_encode();
And get a 32-character hexadecimal number
*/
        $file = chunk_split(base64_encode($file));
        $num = md5( time() );

        /*
Define the main message headers
*/
        $hdr  = "From:".$_REQUEST['from']."\r\n";
        $hdr .= "MIME-Version: 1.0\r\n";
        $hdr .= "Content-Type: multipart/mixed; ";
        $hdr .= "boundary=".$num."\r\n";
        $hdr .= "--$num\r\n";
/*
Define message section
*/
        $hdr .= "Content-Type: text/plain\r\n";
        $hdr .= "Content-Transfer-Encoding: 8bit\r\n\n";
        $hdr .= "".$_REQUEST['comments']."\r\n";
        $hdr .= "--".$num."\n";
 
/*       
Define the attachment section
 */
        $hdr .= "Content-Type:". $att_type." ";
        $hdr .= "name=\"".$att_name."\"r\n";
        $hdr .= "Content-Transfer-Encoding: base64\r\n";
        $hdr .= "Content-Disposition: attachment; ";
        $hdr .= "filename=\"".$att_name."\"\r\n\n";
        $hdr .= "".$file."\r\n";
        $hdr .= "--".$num."--";

 /*       
Send the email
*/
	
	$to = $_REQUEST['to'];
	$re = $_REQUEST['re'];
	$comments = $_REQUEST['comments'];
	$from = "From: me@mysite.com\r\n";    
     
	if(mail( $to, $re, $comments, $from, $hdr)){
                   echo "Email Sent";}

        else{
                    echo "Email Sending Failed";} 

/*
Close the attachment
*/
        fclose( $fp );

}
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I would recommend using something like swiftmailer or phpmailer for that. Attachments can be kind of tricky business.

http://swiftmailer.org <-- The creator of that is a regular poster here, so it's usually pretty easy to get support :wink:
http://phpmailer.sourceforge.net/
User avatar
abzu
Forum Newbie
Posts: 8
Joined: Tue Aug 29, 2006 3:39 pm

Thanks

Post by abzu »

Thanks. I think I'll give swiftmailer a try. It still ticks me off that I can't figure out the problem with the script I posted though.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I know whatcha mean :evil: :twisted:
User avatar
abzu
Forum Newbie
Posts: 8
Joined: Tue Aug 29, 2006 3:39 pm

Post by abzu »

Okay so I took a look at both of these suggestion and they were an even bigger headache. With swiftmailer I couldn't even get past step 1 (telnet to see if you can connect) I couldn't. With phpmailer The first thing it said to do was to do something to the php.ini. I am being hosted and I'm pretty sure I don't have access to the php.ini. This sounds pretty straight forward but it still took a couple of hours to realize I was not going to figure this stuff out. so I am back to my original plea as I actually seemed to get nuch fartehr with the code I originally posted. It at least sends an email when there is no header info attaced. Can anyone see a problem with how the $hdr variable is built because that seems to be what is blocking the email from going.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Skip over the telnet thing and just go straight to the code (provided you've uploaded swift to your server). That telnet thing is only in there to identify issue up-front and save me an inbox full of "swift dunnit workz" emails ;)
Post Reply