Close

How to send Mail Using PHP

  • Home
  •  / 
  • PHP
  •  / 
  • How to send Mail Using PHP

How to send Mail Using PHP

Step 1: You need to create a form

Example:
<!– start .contact-form –>
<div class=”contact-form”>
<form method=”post” >
<div class=”input-field”>
Name: <br><input type=”text” class=”form-control” name=”name” required=”” title=”Please Enter Your Name Here”>
</div>
<div class=”input-field”>
Email id: <br><input type=”email” class=”form-control” name=”email” required=”” title=”Please Enter Your Email Here”>
</div>
<div class=”input-field”>
Mobile: <br><input type=”text” class=”form-control” name=”mobile” required=”” maxlength=”10″ title=”Please Enter Your Mobile Number Here”>
</div>
<div class=”input-field”>
Subject: <br><input type=”text” class=”form-control” name=”subject” required=”” title=”Please Enter Your Subject Here”>
</div>
<div class=”input-field message”>
Message:&nbsp;
<textarea name=”message” class=”form-control” required=”” title=”Please Enter Your Message Here”></textarea>
</div>
<input type=”submit” name=”submit” class=”btn btn-success pull-right” value=”SEND” id=”msg-submit”>
</form>
</div>
<!– end .contact-form –>
Above I have mentioned a sample of the form through which you can send the mail.
Step 2: Create the code for sending email
<!–Email send start–>
<?php
if(isset($_POST[‘submit’])){
$to = “abc@gmail.com“; // Email address
$cc = “xyz@gmail.com“; // CC
$from = $_POST[’email’]; // Sender’s Email address
$name = $_POST[‘name’]; // Name
$mobile = $_POST[‘mobile’]; //Mobile Number
$subject = “Contact Us Form | Subject : ” . $_POST[‘subject’]; //Subject for Reciever
$subject2 = “Copy of your Contact Us form submission to¬†websitename.com“; //Subject for Sender
$message = ” Name: ” . $name . “\n\n Email: ” . $from . “\n\n Mobile: ” . $mobile . “\n\n Subject: ” . $_POST[‘subject’] . “\n\n Message: ” . $_POST[‘message’]; //Message for Reciever
$message2 = “Hey: ” . $name . “\n\nHere is a copy of your message: ” . $_POST[‘message’]; //Message for Sender
$headers = “From:” . $from . “\r\n” . $cc;
//$headers = “From:” . $from; // This Header may use when you don’t want to insert CC
$headers2 = “From:” . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // Sends a copy of the message to the sender
echo “<script>alert(‘Mail Sent. Thank you ” . $name . “, we will contact you shortly.’)</script>”; // Submission popup
}
?>
<!–Email send end–>

PHP Developer

Related Posts

Leave a Reply

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