We can categorize the email sending in 3 types
1. Sending Email as a Simple Text
2. Sending Email as the HTML Content
3. Sending Email with Attachments
1. Sending Email as a Simple Text.
In PHP we use the mail() function for sending email, it is just like as mail($to,$subject,$message,$headers,$parameters), In PHP mail() there are three basic and two optional parameters
$to //email address to send to $subject //the subject of the email $message //the message content to be sent $headers //Optional. for additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n) $parameters //Optional. for an additional parameter to the sendmail program
The mail() function returns True if the message is sent successfully and False when it fails.
<?php $to = "info@ewa.com"; //email address to send to $subject = "Contact form";//the subject of the email $message = "This is a simple email message for testing the email in PHP.";//the message content to be sent $from = "user@test.com"; $headers = "From:" . $from;//Optional. for additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n) $mail_sent = mail($to,$subject,$message,$headers); if($mail_sent){ echo "Mail Sent Successfully."; } else { echo "Mail Sent Fails"; } ?>
2. Sending Email as the HTML Content
For sending the email which contains the html tag it is very easy and you can send email as the HTML as well. Follow the example below:
<?php $to = "info@ewa.com"; //email address to send to $subject = "Contact form";//the subject of the email $message = ' <table> <tr> <td>Name : </td> </td>.'$name'.</td> </tr> <tr> <td>Email address : </td> </td>.'$email'.</td> </tr> <tr> <td>Phone : </td> </td>.'$phone'.</td> </tr> <tr> <td>Comments : </td> </td>.'$comment'.</td> </tr> </table>'; //the message content to be sent $from = "user@test.com"; $headers = "From:" . $from;//Optional. for additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n) $mail_sent = mail($to,$subject,$message,$headers); if($mail_sent){ echo "Mail Sent Successfully."; } else { echo "Mail Sent Fails"; } ?>
3. Sending Email with Attachments
For sending attachment in the email it is very important that Content-Type/MIME type should be multipart/mixed.
<?php $to = "help@ewa.com" . ", " ; // note the comma $to .= "info@ewa.com"; $subject = "Testing Email with an attachement"; $bound = sha1(date('r', time())); $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-{$bound}\""; // file attachment $attachedfile = file_get_contents('testattachment.zip'); $encoded_test = base64_encode($attachedfile); $attached_file = chunk_split($encoded_test); // headers $headers .= "To: Info <info@ewa.com>, We <help@ewa.com>\r\n"; $headers .= "From: Me <me@ewa.com>\r\n"; $headers .= "Cc: hello@ewa.com\r\n"; // Your message here: $message =<<<EOBODY --PHP-alt-{$bound} Content-Type: text/plain Hello Its for the testing purpose only. --PHP-alt-{$bound} Content-Type: text/html <html> <head> <title>Test HTML Mail with the file attachment.</title> </head> <body> <font>Just for the testing purpose.</font> </body> </html> --PHP-alt-{$bound}-- --PHP-mixed-{$bound} Content-Type: application/zip; name="attachment.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment {$attachedfile} --PHP-mixed-{$bound}-- EOBODY; // Finally, send the email mail($to, $subject, $body, $headers); ?>
Hope this will help to someone