How to send email in php


Let's code a small application in PHP to send email from the contact form of a website. When the user visits the contact page of the website, they can enter details about the email such as their name, the subject of the email, the sender's email address and the message. When they've finished filling the form, they can click the send button and the mail will be sent to the specified email address.

index.php:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sending email with php</title>
</head>
<body>
<form method="post" action="send_script.php">
  Name: <input type="text" name="name" > <br />
  email: <input type="email" name="email" > <br />
  Subject: <input type="text" name="subject" > <br />
  Message: <textarea name="msg"></textarea>
  <button type="submit" name="send_message_btn">Send</button>
</form>
</body>
</html>

When the form send button is clicked, the values are submitted to the send_script.php file which sends the email address:

<?php 
if (isset($_POST['send_message_btn'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $msg = $_POST['msg'];
  // Content-Type helps email client to parse file as HTML 
  // therefore retaining styles
  $headers = "MIME-Version: 1.0" . "\r\n";
  $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  $message = "<html>
  <head>
  	<title>New message from website contact form</title>
  </head>
  <body>
  	<h1>" . $subject . "</h1>
  	<p>".$msg."</p>
  </body>
  </html>";
  if (mail('website_owner@example.com', $subject, $message, $headers)) {
   echo "Email sent";
  }else{
   echo "Failed to send email. Please try again later";
  }
}
?>

When the user clicks the send button on the contact form, the values in the form are submitted to the send_script.php file. This file grabs the values from the form and forwards them to the email address specified. The email can be sent in HTML format as above and CSS can be used to apply some styles to the email. 

Note:

Sending email using the mail() function only works when there is internet connection. Until your application is hosted on a server on the internet before it can send an email. 

 

More about the PHP mail() function

A basic usage of the mail() function looks like this:

<?php
  mail("receiver@example.com","My subject","Hey there! How you doin'");
?>

The above piece of code will actually send an email to 'receiver@example.com' with subject 'My Subject' and message 'Hey there! How you doin'' when the script is executed.

PHP's mail() function has the following signature:

bool mail ( string $to , string $subject , string $message, string $headers )

1.) $to

Receiver's email address. As specified in the PHP docs, the receiver's email address should take the following formats

  • user@example.com
  • user@example.com, anotheruser@example.com (Sending to multiple users)
  • User <user@example.com> (Specifying the user's username)

2.) $subject: 

The subject of the email to be sent

3.) $message: 

The message. Each line should contain no more than 70 characters. Each line should be separated with a CRLF (\r\n). You could solve this simply by wrapping the message text around the PHP wordwrap() function. Like so:

$message = wordwrap($message, 70);

4.) $headers:

This one is optional. But If you want to do some styling on the email you send, you must specify the Content-Type in the header as text/html;charset=UTF-8 to tell email clients to parse the email as HTML. 

Thank you for following this tutorial. We would really appreciate it if you share this tutorial with your friends on any of the social media!

You might also like:

Share Google Plus Linkedin


Comments