In Send Mail Attachments with PHP Mail(), PHP mail() function is widely used for sending emails without using any external mail library, for example, PHPMailer.
Here we will use only the PHP mail() function for PHP Sendmail. We create a PHP mailing system step by step to get PHP assignment help from experts.
Send Mail Attachments with PHP Mail() Demo
Mail send with attachments in PHP step by step:
- First, we create a simple HTML form with form attribute
enctype="multipart/form-data"
- Creating some HTML input box, text area, and for attachment input
type="file"
- Then we will start with PHP code to send emails with attachments.
Creating HTML Form
1 2 3 4 5 6 7 8 9 |
<form method="post" enctype="multipart/form-data"> <input type="text" id="contact_name" class="form-control" placeholder="Name" name="user_name"/> <input type="text" id="contact_email" class="form-control" placeholder="Email Address" name="user_mail"> <textarea id="contact_message" class="form-control" rows="7" placeholder="Write a message" name="user_message"></textarea> <input name="attachment" type="file"> <input type="submit" value="Send mail" class="btn btn-primary pull-right"/> </form> |
On the above form
enctype="multipart/form-data"
, this attribute is important for sending attachment.
PHP code to send an email with the attachment
- First, we post all the data from the HTML Form using
$_POST
function. - Also, post the attachment using the name attribute of that field.
- For uploading file input type must have
type="file"
1 2 3 4 5 6 7 8 |
<?php if(isset($_POST) && !empty($_POST)) { if(!empty($_FILES['attachment']['name'])) { $file_name = $_FILES['attachment']['name']; $temp_name = $_FILES['attachment']['tmp_name']; $file_type = $_FILES['attachment']['type']; $base = basename($file_name); |
- Checking allowed an extension on the below code,
1 2 3 4 |
$extension = substr($base, strlen($base)-4, strlen($base)); //only these file types will be allowed $allowed_extensions = array(".doc", "docx", ".pdf", ".zip", ".png"); |
- If the file extension satisfies with allowed extensions then go to the next step,
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 |
//check that this file type is allowed if(in_array($extension,$allowed_extensions)) { //mail essentials $from = $_POST['user_mail']; $to = $_POST['user_mail']; $subject = $_POST['user_name']; $message = $_POST['user_message']; //things u need $file = $temp_name; $content = chunk_split(base64_encode(file_get_contents($file))); $uid = md5(uniqid(time())); //unique identifier //standard mail headers $header = "From: ".$from."\r\n"; $header .= "Reply-To: ".$replyto. "\r\n"; $header .= "MIME-Version: 1.0\r\n"; //declare multiple kinds of email (plain text + attch) $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n"; $header .="This is a multi-part message in MIME format.\r\n"; //plain txt part $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n"; $header .= $message. "\r\n"; //attch part $header .= "--".$uid."\r\n"; $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n"; $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n"; $header .= $content."\r\n"; //chucked up 64 encoded attch //sending the mail - message is not here, but in the header in a multi part if(mail($to, $subject, $message, $header)) { echo "success"; }else { echo "fail"; echo error_get_last()['message']; } }else { echo "file type not allowed"; } //echo an html file }else { echo "no file posted"; } } ?> |
On the above PHP code, we create mail headers for sending PHP mail attachments.
Mail with attachments Using PHP code (Complete Source 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php if(isset($_POST) && !empty($_POST)) { if(!empty($_FILES['attachment']['name'])) { $file_name = $_FILES['attachment']['name']; $temp_name = $_FILES['attachment']['tmp_name']; $file_type = $_FILES['attachment']['type']; $base = basename($file_name); $extension = substr($base, strlen($base)-4, strlen($base)); //only these file types will be allowed $allowed_extensions = array(".doc", "docx", ".pdf", ".zip", ".png"); //check that this file type is allowed if(in_array($extension,$allowed_extensions)) { //mail essentials $from = $_POST['user_mail']; $to = "phpcodertech@gmail.com"; $subject = $_POST['user_name']; $message = $_POST['user_message']; //things u need $file = $temp_name; $content = chunk_split(base64_encode(file_get_contents($file))); $uid = md5(uniqid(time())); //unique identifier //standard mail headers $header = "From: ".$from."\r\n"; $header .= "Reply-To: ".$replyto. "\r\n"; $header .= "MIME-Version: 1.0\r\n"; //declare multiple kinds of email (plain text + attch) $header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n"; $header .="This is a multi-part message in MIME format.\r\n"; //plain txt part $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n"; $header .= $message. "\r\n"; //attch part $header .= "--".$uid."\r\n"; $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n"; $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n"; $header .= $content."\r\n"; //chucked up 64 encoded attch //sending the mail - message is not here, but in the header in a multi part if(mail($to, $subject, $message, $header)) { echo "success"; }else { echo "fail"; echo error_get_last()['message']; } }else { echo "file type not allowed"; } //echo an html file }else { echo "no file posted"; } } ?> <form method="post" enctype="multipart/form-data"> <input type="text" id="contact_name" class="form-control" placeholder="Name" name="user_name"/> <input type="text" id="contact_email" class="form-control" placeholder="Email Address" name="user_mail"> <textarea id="contact_message" class="form-control" rows="7" placeholder="Write a message" name="user_message"></textarea> <input name="attachment" type="file"> <input type="submit" value="Send mail" class="btn btn-primary pull-right"/> </form> |
Please comment below if any issues. Here is the complete source with a live view for Send Mail with PHP Mail() function.
Also, check officially on PHP https://www.php.net/manual/en/function.mail.php
Happy Coding..!
mail sent with attachment . But attachment not able to open .pls give solution.
You can use above complete code for solution. Also check the demo.
You can use above complete code for solution. Also check the demo.
[…] Send Mail Attachments with PHP Mail() […]
[…] Send Mail Attachments with PHP Mail() […]
[…] Send Mail Attachments with PHP Mail() […]
[…] Send Mail Attachments with PHP Mail() […]
[…] Send Mail Attachments with PHP Mail() […]
[…] Send Mail Attachments with PHP Mail() […]
How to Get attachment in zip file?????
[…] Send Mail Attachments with PHP Mail() […]
[…] Send Mail Attachments with PHP Mail() […]
[…] Send Mail Attachments with PHP Mail() […]
[…] Send Mail Attachments with PHP Mail() […]
Hi,
Thank you so much for sharing all this wonderful info about Mail Issues!!! It is so appreciated!!!
but my message is not show in mail
using this code we can send attachment but not other message can u help me out
[…] Send Mail Attachments with PHP Mail() […]