By using PHP scripts, emails can be sent directly.



PHP mail() Function

PHP mail() function is used to send emails.

Syntax:

mail(to,subject,message,headers,parameters) 

This mail() function accepts five parameters as follows and (the last two are optional).

Parameters Details
to The recipient's email address.
subject The email's subject line.
message The actual email body where you can insert main messages.
headers Additional parameters such as "From", "Cc", "Bcc" etc.
parameters Optional parameters.

PHP Simple Email

Example:

<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a test email message.";
$from = "[email protected]";
$headers = "From:" . $from;

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

PHP Email Form

Example:

<?php
if(isset($_POST["SubmitBtn"])){

$to = "[email protected]";
$subject = "Contact mail";
$from=$_POST["email"];
$msg=$_POST["msg"];
$headers = "From: $from";

mail($to,$subject,$msg,$headers);
echo "Email successfully sent.";
}
?>

<form id="emailForm" name="emailForm" method="post" action="" >
<table width="100%" border="0" align="center" cellpadding="4" cellspacing="1">
<tr>
  <td colspan="2"><strong>Online Contact Form</strong></td>
</tr>
<tr>
  <td>E-mail :</td>
  <td><input name="email" type="text" id="email"></td>
</tr>
<tr>
  <td>Message :</td>
  <td>
  <textarea name="msg" cols="45" rows="5" id="msg"></textarea>
  </td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><input name="SubmitBtn" type="submit" id="SubmitBtn" value="Submit"></td>
</tr>
</table>
</form>

Sending HTML Email

When you send a text message using PHP, then email content will be treated as ordinary text. HTML tags also.

You have to specify a Mime-version, content type and character set to send an HTML email.

Example:

<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a <strong>HTML</strong> test email message.";
$from = "[email protected]";
$headers = "From:" . $from\r\n;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";

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


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram