You probably know how to send email with PHP, but it gets little trickier when you want to send attachment with PHP email. Here’s another example you can use to send attachment with PHP mail.
Create a HTML form with file input field similar to below, enctype attribute should be “multipart/form-data“, so that form-data is encoded as “multipart/form-data” when sent to server.
1
2 3 4 5 6 7 8 |
<form name=“form1” enctype=“multipart/form-data” method=“post” action=“”>
<label> <input type=“file” name=“my_file” /> </label> <label> <input type=“submit” name=“button” value=“Submit” /> </label> </form> |
PHP mail with Attachment
Here’s the complete PHP code that sends PHP email with attachment.
Just create a PHP file with code below and point your HTML form to the
file, and don’t forget to change recipient address and email address in
the code.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
if($_POST && isset($_FILES[‘my_file’]))
{ $sender_email = ‘sender_mail@example.com’; //sender email //get file details we need if($file_error>0) $boundary = md5(“fluxntech”); //plain text //attachment $sentMail = @mail($sender_email, $subject, $body, $headers); if($sentMail) //output success or failure messages }
|
That’s it, you can capture subject and message body from HTML form
and use them in the code. I hope this code will be useful in your PHP
projects. Good luck!
These content links are provided by Content.ad. Both Content.ad and the web site upon which the links are displayed may receive compensation when readers click on these links. Some of the content you are redirected to may be sponsored content. View our privacy policy here.
Family-Friendly Content
Website owners select the type of content that appears in our units. However, if you would like to ensure that Content.ad always displays family-friendly content on this device, regardless of what site you are on, check the option below. Learn More
To learn how you can use Content.ad to drive visitors to your content or add this service to your site, please contact us at info@content.ad.
0 comments:
Post a Comment