pTutorial

New way to learn PHP Example

PHP mail function

How To send mail using PHP

Many Web applications have a need to send email messages. For example, a contact form script typically processes a form submitted by a visitor and emails the form information to the Web Master.

PHP provide built - in support for sending email messages, which makes it very easy to send the email from within your PHP scripts.

To send an email, use the mail() function with minimum three required argument to, subject and message.

Send mail example

<html>
<head>
</head>
<?php
if (!isset($_POST["sub"])) {
  ?>

<form  method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<table style="background: #AAFFFF; margin-left:100px; margin-top:50px; margin-bottom:100px; padding:10px; " >
  <tr>
    <td style="color:#333333;font-size:20px;">Name</td>
    <td><input name="name" type="text" required/></td>
  
  </tr>
  <tr>
    <td style="color:#333333;font-size:20px;">Email</td>
    <td><input name="email" type="email"  required/></td>
  
  </tr>
  <tr>
    <td style="color:#333333;font-size:20px;">Messege</td>
    <td><textarea name="mess" cols="15" rows="10" required ></textarea></td>
   
  </tr>
  <tr>
    <td><input name="res" type="reset"  value="Reset"/></td>
    <td><input name="sub" type="submit" value="Submit"></td>
   
  </tr>
</table>
</form>
 <?php
 } else { 
  if (isset($_POST["name"])) {
    $from = $_POST["name"]; // sender
    $subject = $_POST["email"];
    $message = $_POST["mess"];
	 mail("info@ptutorial.com",$subject,$message,"From: $from\n");
    echo "Thanks";
  }
}
?>


</html>


In the above example, we will make a simple HTML form with the table for proper formatting and after that pass the four arguments into the mail function.

  • Email: - Receiver’s email address.
  • Subject: -The email subject line, as a string.
  • Message: -The message body, as a string.
  • From: -The name of the sender, as a string.

QUOTES OF THE DAY

Whenever man commits a crime,heaven finds a witness.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER