How to send various kinds of email using PHP

How to send various kinds of email using PHP

Email is one of the most popular services over the internet. A considerable amount of emails are sent and delivered every day. We can easily send emails by using PHP scripts. Today I’m going to show 3 types of email sending techniques 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 sends 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 by sending a simple text email. PHP provides a function called “mail()”, which allows you to send emails. This function takes five parameters, where three parameters are basic and two parameters are optional. Three basic parameters are– “email address you want to send mail”, “Subject of your email”, and “Body of your message”. Finally mail () function returns a Boolean value If your mail is sent successfully then it returns true otherwise, it returns false.

$to = '[email protected]';

$subject = 'Test email from';

$message = "Hey Man!\n\nThis is my first mail.";

$headers = "From: [email protected]\r\nReply-To: [email protected]";

$mail_sent = @mail( $to, $subject, $message, $headers );

echo $mail_sent ? "Mail sent" : "Mail failed";

We can see that it is easy to send email using the mail() function. You can also send multiple emails like “[email protected], [email protected],” and so on. If you have faced any issues about sending emails 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 emails.

– How to send HTML emails Using PHP:

After successfully sending text emails, now we are going to learn how to send HTML emails. Basically, HTML emails are sent for product promotion or to publish newsletters for a website. In HTML email, you can add links, images, colors, and backgrounds like normal web page. You can also add a design in your email. In one single sentence, you can design your email body as you want.

$to  = '[email protected]' . ',[email protected] ';

$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-

$headers.='From: Email From <[email protected]>'."\r\n";

mail($to, $subject, $message, $headers,'');

Some notes about HTML emails:

In HTML email, you need to add some additional parameters in the mail function. You need to add an additional header Content-type: multipart/alternative in there.

– How to send Email with attachment:

Finally, we are going to learn how to send emails with attachments. To send an email with an attachment we need to use a header called Content-type: multipart/mixed MIME type.

So, you can see that it’s very easy to attach a file with an email. In addition, to send a file with email, we need to convert file data into a string and encode it with base64 format, then split them into small chunks and include them as attachment.

Was this information helpful? What other tips would you like to read about in the future? Share your comments, feedback, and experiences with us by commenting below!

Total 0 Votes

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Comment

Back To Top