JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Sending Emails Using the Mail Form in PHP

Author:JIYIK Last Updated:2025/04/12 Views:

This article will demonstrate installing sendmailthe library and sending an email through the PHP mail form.


Install sendmailto send email from PHP on local server

PHP has a built-in function mail()to send emails. However, this function will not work until you install its library.

To install sendmail, follow the steps below.

  • Download and extract sendmail.

    Download sendmail. Then extract the zip file to C:\sendmail\.

  • Configuration sendmail.ini.

    Now, sendmailopen from the Home folder sendmail.ini. Search and set the configuration as below.

    smtp_server=smtp.gmail.com
    
    smtp_port=587
    
    auth_username=The_email_from@gmail.com
    
    auth_password=Email Password
    
    force_sender=your_address@gmail.com
    

    This setting is for sending emails through Gmail, but you can also send emails from other people or your server.

    You need to set it in these parameters. auth_usernameand auth_passwordwill be the email and password you want to send emails from.

  • Configuration php.ini.

    To configure, open php.inithe ./configure file and search for sendmail_path. Then set this parameter to C:\sendmail\sendmail.exe -t.

    ; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
    ; http://php.net/sendmail-path
    sendmail_path ="C:\sendmail\sendmail.exe -t"
    

    Restart your local server and you're done.

  • Test sending an email.

    You can test it by sending an email with a simple one-line code as shown below.

    <?php
    mail("example@hotmail.com","Test subject", "This is a test Email");
    ?>
    

    Output:

    Test Email

    As you can see, the output is the email sent to the specified address.


Creating and sending emails using the mail form in PHP

First, you must create an HTML form to send the email data to the PHP code. Use mail()the function to send an email with that data.

example:

<?php 
if(isset($_POST['submit']))
{
    $email_address = $_POST['email_address'];
    $subject = "This is a test email";

    $email_message = "The following message is sent in email:" . "\n\n" . $_POST['email_message'];
    mail($email_address,$subject,$email_message);
    echo "Email sent to ".$email_address;
    // You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
    <title>PHP Mail Form</title>
</head>
<body>
    <form action="" method="post">
    Email: <input type= "email" name= "email_address"><br>
    Message:<br> <textarea rows= "10" name= "email_message" cols= "50"> </textarea> <br>
    <input type= "submit" name= "submit" value= "Send Email">
    </form>
</body>
</html>

Output:

Email sent to example@outlook.com

PHP Sending Emails Using Mail Form

The code above generates a form with a emailand messageboxes. The code then sends what is written in the message box to the email address given in the email field.

Additional fields such as first name, last name, and date can also be added to the form to send an email with more information.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial