Send Email With File Attachment In PHP
Hi Developers, Thanks for reading send an email with file attachment In PHP. Email is a method of exchanging message between people by electronic devices. We can say It’s a very Common and Important functionality for Web and mobile application. Nowadays Most of the E-commerce website having functionality like Contact us, Feedback, Various Notification like order received, Order Shipped, Amount Get paid, Invoice etc..
The more important functionality of send email is implementing at time of registration a website or an application. We need to verify the registration email ID to get rid of spam or fake users from your websites or applications.
Here this blog section you will get the best example of the send email functionality with sample code and video tutorials for easy understanding. you will get an idea of how to send an email with attachment in the PHP script.
In General PHP gives us in-built send email functionality which is mail();
Syntax:
1 |
mail( $to, $subject, $message, $headers,$parameters ); |
$to= This parameter is for a receiver. [required]
$subject = This parameter is for Subject of Email.[required]
$message =This is for Message or email content.[required]
$headers =This contains additional headers, like From, Cc, Mime-Version, Bcc. [optional]
$parameters = use to pass additional parameter.[optional]
Table of Contents
Different Way to send email using PHP find below.
- Simple Email using PHP script.
- Send Email with HTML format.
- Send Email With attachment with a PHP script.
- Sending Email with dynamic Attachment when submitting the form using PHP script.
- Email With multiple Attachment in Core PHP script.
- Email With Dynamic Attachment when Submitting the form using Core PHP.
- Email Send Using SMTP in Core PHP.
- Email Send Using PHP mailer library in PHP.
- Email Send Using Codeigniter Framework.
- Email with Attached email Send Using Codeigniter framework.
- Email With Attached mail Sends Using Laravel Framework.
- Email With Attachment by Python.
Simple Email send using PHP
Example
3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $to_email = 'somename@company.com'; $subject = 'Send Email Using Simple PHP script'; $message = 'This mail is sent using the PHP mail function Free Online Projects'; $headers = 'From: noreply @ company . com'; // Sending email if(mail($to, $subject, $message,$headers)){ echo 'Your mail has been sent successfully.'; } else{ echo 'Unable to send email. Please try again.'; } ?> |
Send Email with HTML format in PHP
When we send simple email in PHP, we consider all the text is simple text, means it not contain any kind of HTML.
Here you have to send HTML formatted text. For that, we have to add some in-built features which can help to get HTML formatted email in PHP. you need to add some line of text in $headers parameter.
MIME – It stands for Multipurpose Internet Mail Extension.
To format any HTML tag, we can specify the MIME version, content type and character set of the header section.
Example
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 |
<?php $to = 'freeonlineprojects@email.com'; $subject = 'Test mail for HTML format'; $from = 'testing@email.com'; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Create email headers $headers .= 'From: '.$from."\r\n". 'Reply-To: '.$from."\r\n" . 'X-Mailer: PHP/' . phpversion(); // Compose a simple HTML email message $message = '<html><body>'; $message .= '<h1 style="color:#f40;">Hi Admin!</h1>'; $message .= '<p style="color:#080;font-size:18px;">Is this HTML formatted email.?</p>'; $message .= '</body></html>'; // Sending email if(mail($to, $subject, $message, $headers)){ echo 'Your mail has been sent successfully.'; } else{ echo 'Unable to send email. Please try again.'; } ?> |
Sensitization and validation is good practice before sending email in PHP.
Nowadays hackers are more active to make spam your website or inbox. To get rid of this kind of spam you need to sanitize or validate user’s email from input data. let see how to sanitize or filter these unknown emails or data inject from user-end from web forms.
PHP used a function called filter_var($email, SANITIZATION TYPE);
FILTER_VALIDATE_EMAIL – It returns true for valid email addresses and false for invalid email addresses.
FILTER_SANITIZE_EMAIL – It removed illegal characters from email addresses.
$email = support\@domain.com
$output = filter_var($email, FILTER_SANITIZE_EMAIL);
//output = support@domain.com;
FILTER_SANITIZE_URL – It removes illegal characters from URLs.
$email = “http://www.example@.cém”;
$output = filter_var($email, FILTER_SANITIZE_URL );
//$output =http://www.example@.com
FILTER_SANITIZE_STRING – It removes tags from string values.
$email = “<b>Some text</b>”;
$output = filter_var($email, FILTER_SANITIZE_STRING );
//$output =Some text;
Send Email with attachment in PHP
To send an attachment with the email, we need to set the Content-type as mixed/multipart and we have to specify the text and attachment sections within a Boundary.
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
<?php if(isset($_POST['submit'])) { $succ = '<h2>Thanks for your file and message!</h2>'; $to = $_POST['email']; $subject = 'Email With Attachment in PHP'; $message .= "Name:".$_POST['name']."\n"; $message .= "Email:".$_POST['email']."\n"; $message .= "Email:".$_POST['msg']."\n"; $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name']))); $filename = $_FILES['file']['name']; $boundary =md5(date('r', time())); $headers = "From: freeonlineprojectz@gmail.com\r\nReply-To: freeonlineprojectz@gmail.com"; $headers .= "MIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\""; $message="This is a multi-part message in MIME format. --_1_$boundary Content-Type: multipart/alternative; boundary=\"_2_$boundary\" --_2_$boundary Content-Type: text/plain; charset=\"iso-8859-1\" Content-Transfer-Encoding: 7bit $message --_2_$boundary-- --_1_$boundary Content-Type: application/octet-stream; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --_1_$boundary--"; mail($to, $subject, $message, $headers); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Free Bootstrap Contact Form With PHP - Send Email With Attachent</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" > <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" > <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <link rel="stylesheet" href="form.css" > <script src="form.js"></script> </head> <body > <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3" id="form_container"> <h2>Contact Us</h2> <p> Please send your email with file attachment in php example - Free online projects </p> <?php echo $succ; ?> <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"> <div class="row"> <div class="col-sm-12 form-group"> <label for="message"> Message:</label> <textarea class="form-control" type="textarea" name="msg" id="message" maxlength="6000" rows="7"></textarea> </div> </div> <div class="row"> <div class="col-sm-6 form-group"> <label for="name"> Your Name:</label> <input type="text" class="form-control" id="name" name="name" required> </div> <div class="col-sm-6 form-group"> <label for="email"> Email:</label> <input type="email" class="form-control" id="email" name="email" required> </div> </div> <div class="row"> <div class="col-sm-12 form-group"> <label for="file">File</label> <input type="file" class="form-control" name="file" id="file"> </div> </div> <div class="row"> <div class="col-sm-12 form-group"> <input type="submit" name="submit" id="submit" value="send" class="btn btn-lg btn-default pull-right"> </div> </div> </form> <div id="success_message" style="width:100%; height:100%; display:none; "> <h3>Posted your message successfully!</h3> </div> <div id="error_message" style="width:100%; height:100%; display:none; "> <h3>Error</h3> Sorry there was an error sending your form. </div> </div> </div> </div> </body> </html> |
Download source code & Demo