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




  
  Sending email with php - www.pakainfo.com


User Name:
User email:
User Subject:
User Message:

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:

php code to send email on button click

sending_email.php


  
  	Welcome To Our Shop from Any Online website contact form
  
  
  	

" . $subject . "

".$emailSendMessage."

"; if (mail('website_owner@domain_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.

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:


The above piece of source code will actually send Parameters an email to ‘acceptor@domain_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

  • client@domain_name.com
  • client@domain_name.com, anotherclient@domain_name.com (Sending to multiple clients)
  • Client (Specifying the client’s clientname)

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.

Send an Email on Form Submission Using PHP

To send an email on button click in PHP, you need to use the mail() function. Here is an example code that demonstrates how to do this:










In this example, we first check if the submit button is clicked using the isset() function. Then, we fetch the data from the form using the $_POST superglobal variable. Finally, we use the mail() function to send the email with the data provided. If the email is sent successfully, we display a success message. If the email sending fails, we display an error message.
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!

Leave a Comment