Course Banner


Email

Email sending is one of the important parts of any website developer. Whether its order confirmation email or college registration form, the confirm is sent through emails and other sms etc.

So, as a developer you need to know how to send email through PHP

Send Plain Text Email:In this format of the email, we cannot use HTML tags. Only plain text email is supported in this.

Example :

< ?php
  1. $to = 'sazumar@email.com';
  2. $subject = 'new course has arrived';
  3. $message = 'Hy guys! new course, Meanstack has been launched.';
  4. $from = 'enquiry@acesoftech.com';
  5. // Sending email
  6. if(mail($to, $subject, $message)){
  7.       echo' Email successfully sent!';
  8. } else{
  9.       echo'There is problem in sending email';
  10. }
  11. ?>

In the above program you can see there is a function called mail() which takes few parameter and then sends email. Mail() is a built-in PHP function which is responsible for seining emails.

Below are the list of parameters it can take.

Parameter Name Description to This represents recipient email subject Subject of the email which the user will see message This is the actual body of the message where the recipient will receive the main message. header This is an optional parameter. This is normally used for CC and BCC message sending. parameters This is also an optional parameter which is used to send extra parameters.

HTML email sending

In the above email code, it can send plain text email only but there are higher chances that we can send rich HTML email. For example newsletter, Invitation email etc. For that we will require to add HTML tags. Good thing is that it’s possible to send HTML email through PHP. For that we need to specify MIME version and Content type :

Below is the code for sending HTML email:

< ?php
  1. // Multiple recipients
  2. $to = 'sazumar@gmail.com, keenukola@email.com'; // note the comma
  3. // Subject
  4. $subject = 'Discount for new course';
  5. // Message
  6. $message = '

<html>
<head>
<title >50% Discount for new course </title >
</head>
<body>
<h2>Here are the birthdays upcoming in August! </h2 >
<table>
<tr>
<th>Course name </th > <th>Duration </th > <th>Fees </th > <th>Discount </th >
</tr>
<tr>
<td>Mean Stack </td > <td>4 Months </td > <td>32000 </td > <td>50% </td >
</tr>
<tr>
<td>Java </td > <td>5 Months </td > <td>18000 </td > <td>205 </td >
</tr>
</table>
</body>
</html>

// To send HTML mail, the Content-type header must be set
  • $headers[]= 'MIME-Version: 1.0';
  • $headers[ ] = 'Content-type: text/html; charset=iso-8859-1';
  • // Additional headers
  • $headers[] = 'To: Umar , Rajesh ';
  • $headers[ ] = 'From: Course Discount ';
  • $headers[] = 'Cc: heena@example.com';
  • $headers[ ] = 'Bcc: kiran@example.com';
  • // Mail it
  • mail($to, $subject, $message, implode("rn", $headers));
  • ?>