All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Code To Send Email From Website

Last Updated : Mar 11, 2024

PHP Code To Send Email From Website

In this article we will show you the solution of PHP code to send email from website, emails can be sent from PHP scripts by using PHP mail, which is a PHP built-in function.

These parameters are accepted by the mail function - Email address, Subject and Message.

  • Important events can be notified to users in a cost-effective manner.
  • Provide a contact form on the website that users can use to get in touch with you via email.
  • It provides developers with an email address for receiving system errors
  • It can be used to send emails to your newsletter subscribers.
  • Password reset links can be sent to users who have forgotten their passwords using this tool
  • You can use it to email activation/confirmation links. This is useful when registering users and verifying their email addresses

The PHP mailer sends mail using a Simple Mail Transmission Protocol (SMTP).

PHP's installation folder contains the "php.ini" file that allows the configuration of SMTP mail settings.

In the example below, users need to complete a contact us form and submit it. For secure mail, it's essential to validate and sanitize data prior to sending it. Users can inject code into headers either intentionally or accidentally.

Data sanitization and validation can be easily performed with the PHP built-in function filter_var()

Step By Step Guide On PHP Code To Send Email From Website :-

<?php
if (!isset($_POST["submit"])) { ?>
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
   To: <input type="text" name="to_email"><br>
   From: <input type="text" name="from_email"><br>
   Subject: <input type="text" name="subject"><br>
   Message: <textarea rows="10" cols="20" name="message"></textarea><br>
   <input type="submit" name="submit" value="Send Email">
  </form>
<?php
} else {
  if (isset($_POST["to_email"])) {
    $to_email = $_POST["to_email"];
    $from_email = $_POST["from_email"];
    $subject = $_POST["subject"];
    $body = $_POST["message"];
    if ( mail($to_email, $subject, $body, $headers)) {
      echo("Email successfully sent to $to_email...");
    } else {
      echo("Email sending failed...");
    }
  }
}
?>
  1. The short tags start with "<?" and end with "?>". Short style tags are only available when they are enabled in the php.ini configuration file on servers.
  2. For the validation, we have to verify that the data submitted from the registration form is available in the $_POST variable array and is not empty. As we don't want to allow empty values when all form input fields are required for the user registration process.
  3. then we use <form id="form" tag for creating a form in the program.
  4. It is not necessary to use parentheses with the echo () function as it is not a true function.
  5. then we use input type for creating a text box, password, email, and username.
  6. These validations will check if the fields are not empty or null. For the Email, upon submitting the registration form the system will check if the value entered in the Email field is a valid email.
  7. These validations will check if the fields are not empty or null. For the Email, upon submitting the registration form the system will check if the value entered in the Email field is a valid email.
  8. You do not need to use parentheses when using echo (), because echo () is not actually a function.

Conclusion :-

You can use a custom function to sanitize and validate the values before the mail is sent instead of using PHP's mail () built-in function to send mail.

I hope this article on PHP code to send email from website helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪