Php Email with example

What is PHP mail?

PHP mail is the built in PHP function that is used to send emails from PHP scripts.

The mail function accepts the following parameters;

  • Email address
  • Subject
  • Message
  • CC or BCC email addresses

Syntax

mail($to_email_address,$subject,$message,[$headers],[$parameters]);

 HERE,

  • “$to_email_address” is the comma separated email address of the mail recipients
  • “$subject” is the email subject
  • “$message” is the message to be sent.
  • “[$headers]” is optional, it can be used to include information such as CC, BCC
    • CC is the acronym for carbon copy. It’s used when you want to send a copy to an interested person i.e. a complaint email sent to a company can also be sent as CC to the complaints board.
    • BCC is the acronym for blind carbon copy. It is similar to CC. The email addresses included in the BCC section will not be shown to the other recipients.
  • “$parameters” is optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option)

Example to Send a simple email:

<?php

// the message
$msg = "Example to send a simple email";
// send email
mail("[email protected]","Your subject",$msg);

?>

The mail method will returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

Example to Send an HTML email:

<?php
$to = "[email protected], [email protected]";
$subject = "Example of the HTML email";

$message = "
<html>
<head>
<title>Example of the HTML email</title>
</head>
<body>
<table>
<tr>
<th>Name</th>
<th>Job</th>
</tr>
<tr>
<td>tony stark</td>
<td>Actor</td>
</tr>
</table>
</body>
</html>
";

// Set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

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

mail($to,$subject,$message,$headers);
?>

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *