How to send email in php? | php code to send email on button click

in php code to send email on button click, Let’s full code a small web application task based in PHP to send email from the users contact form of a online website. When the client visits the contact us our web page of the website, they can submit organic information about the email such as their user name, the Email subject of the email address, the sender’s email address as well as the message or comments. When they’ve finished filling the form, they can click the send button as well as the mail will be sent to the specified each email address.

here example to demo with php code to send email on button click

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

When the form send button is clicked using HTML, the all the users data values are submitted to the sending_email.php file which sends the email address:

Also Read This πŸ‘‰   Mail send with attachment in php demo

php code to send email on button click


if (isset($_POST['sending_email_btn'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $emailSendMessage = $_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>
  	<title>Welcome To Our Shop from Any Online website contact form</title>
  	<h1>" . $subject . "</h1>
  if (mail('[email protected]', $subject, $message, $headers)) {
   echo "Send Sucessfully, Good Luck Email sent";
   echo "sorry, Failed to send email. Please try again later";

Also Read: How to Send Emails Using PHP Scripts?

When the client clicks the send button on the contact HTML form, the all the data values in the form are submitted to the sending_email.php file. This data file grabs the values from the HTML form and forwards or send Parameters them to the email address each or specified. The each email address can be sent in HTML data format as above as well as CSS code can be used to apply or changes some styles to the email form element.

Also Read This πŸ‘‰   Laravel Eloquent Where Query Tutorial With Examples


Sending email using the PHP mail() function only simple works when there is data internet connection. Until your online web application is hosted on any server like as a godaddy on the get a data internet before it can send an email.

Also Read: php – SMTP Send mail using PHPMailer PHP Contact Form

More about the PHP mail() function

A default level usage of the php mail() function looks such as a:

  mail("[email protected]","Welcome To Pakainfo","Pakainfo – Online Web Tutorials – All About Computer");

The above piece of source code will actually send Parameters an email to ‘[email protected]’ with subject ‘Welcome To Pakainfo’ and message ‘Pakainfo – Online Web Tutorials – All About Computer’ when the script is executed.

here simple PHP’s mail() function has the bellow signature:

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

1.) $to

bellow simple data for Receiver’s email address. As specified in the PHP docs, the acceptor’s email address should take the following formats like bellow example

2.) $subject:

The user send subject Parameters of the email to be sent

3.) $message:

The message arguments. Each line should data contain string no more than 65 characters. Each line should be separated with a CRLF (\r\n). You could resolve this simply by wrapping the comments message text around the PHP wordwrap() function. Like as bellow:

$message = wordwrap($message, 65);

4.) $headers:

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

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