Email is one of the most popular services over the internet. A huge amount of email sent and delivers every day. We can easily send email by using PHP scripts. Today I’m going to show 3 type of email sending technique using PHP.
First of all, we want to send emails from our web application. Email can be used for various purposes. Like someone just sends emails for regular conversation, someone send emails for product publicity and most of the website owners send emails as a newsletter.
So, In today’s tutorial you will learn –
– How to send simple text email
– How to send HTML email.
– How to send email with attachments.
– How to send simple text email:
We start our tutorial with sending simple text email. PHP provide a great function called “mail()”, which allow you to send emails. This function takes five parameters, where 3 parameters are basic and 2 parameters are optional. 3 basic parameters are– “email address you want to send mail”, “Subject of your email”, “Body of your message”. Finally mail () function return Boolean value means If your mail send successfully then it return true otherwise it return false.
$to = 'email@example.com'; $subject = 'Test email from Coolajax.net'; $message = "Hey Man!\n\nThis is my first mail."; $headers = "From: firstname.lastname@example.org\r\nReply-To: email@example.com"; $mail_sent = @mail( $to, $subject, $message, $headers ); echo $mail_sent ? "Mail sent" : "Mail failed";
We can see that, it’s too much easy to send email using mail () function. You can also send multiple recover like “firstname.lastname@example.org, email@example.com” and so on. If you have faced any issue about sending email then you need to check your server settings. What happened local server doesn’t allow you to send emails, so you need a live hosting server for sending email.
– How to send HTML emails Using PHP:
After successfully sending text email, now we are going to learn how to send HTML emails. Basically HTML emails send for product promotion or publish newsletters for a website. In HTML email you can add links, images, colors, background like normal web page. You can also add design in your email. In one single sentence, you can design your email body as you want?.
$to = 'firstname.lastname@example.org' . ',email@example.com '; $subject = "Sending a HTML email"; $message = 'Write Your HTML Code In Here'; $headers='MIME-Version: 1.0'."\r\n"; $headers.='Content-type: text/html; charset=utf-8; charset=iso-8859-1'."\r\n"; $headers.='From: Email From Coolajax.net <firstname.lastname@example.org>'."\r\n"; mail($to, $subject, $message, $headers,'');
Some notes about HTML emails:
In HTML email you need to add some additional parameters in mail function. Like you need to add additional header Content-type: multipart/alternative in there.
– How to send Email with attachment:
Finally we are going to learn how to send email with attachment. To send an email with attachment we need to use a header called Content-type: multipart/mixed MIME type.
SO, you can see that it’s very easy to attached file with an email. In additional, to send a file with email we need to convert file data in to a string and encode it with base64 format, then split them in to small chunk and include them as attachment.
Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!