Mail() From Start To Finish - Sending Emails With Attachments:
(Page 3 of 3 )
So you have learnt how to send basic text emails, html emails, but now you want to send emails with attachments. The example below shows you how to send a email with an attachment using a form with a "file" field to browse your computer for a file to attach.
<?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 */ mail( $_REQUEST['to'], $_REQUEST['re'], $_REQUEST['comments'], $hdr); /* Close the attachment */ fclose( $fp ); echo "Mail sent..."; } ?> |
Note: I have not hard coded in the $to variable it is part of the form.
So that’s it, you now should know how to, send text emails, send html emails, send emails with attachments. Wasnt it fairly easy? So now you can send html emails with attachments so theres no more links to download files or very plain emails.
About the Author
Andrew Walsh (Andrew on the forums) lives in the UK and is engaged in his secondary education. He wishes to persue a higher education in computer studies. He Started programming at the age of 13 years, currently he has knowledge of php/mysql, html, css, javascript.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |