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

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.

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

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Sending email with php - www.pakainfo.com</title>
</head>
<body>
<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>
</form>
</body>
</html>

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:

Read Also:  Create Contact Form And Send Email using PHP

php code to send email on button click

sending_email.php

<?php 
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>
  <head>
  	<title>Welcome To Our Shop from Any Online website contact form</title>
  </head>
  <body>
  	<h1>" . $subject . "</h1>
  	<p>".$emailSendMessage."</p>
  </body>
  </html>";
  if (mail('[email protected]_name.com', $subject, $message, $headers)) {
   echo "Send Sucessfully, Good Luck Email sent";
  }else{
   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.

Read Also:  PHP Send Email using SMTP Authentication

Note:

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:

<?php
  mail("[email protected]_name.com","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]_name.com’ 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!

Read Also:  PHP Google No CAPTCHA reCAPTCHA jQuery Ajax

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to uninstall cordova and ionic in ubuntu
  2. Read Also:  How to Send Emails Using PHP Scripts
  3. Read Also:  Export Json Array to CSV using php Example
  4. Read Also:  How To Create Contact Form With PHPMailer To Send Mails
  5. Read Also:  How to get all products in WooCommerce
  6. Read Also:  Check WordPress Plugin Dependencies
  7. Read Also:  Create Contact Form Sending Email via SMTP PHPMailer
  8. Read Also:  PHP Laravel Eloquent Increments and Decrements value
  9. Read Also:  php - SMTP Send mail using PHPMailer PHP Contact Form
  10. Read Also:  How to declare a global variable in Laravel 5.8?

Leave a Comment

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