All Tutorials

Your One-Stop Destination for Learning and Growth

Sending Emails Using PHP and Configuring it in XAMPP

PHP is a popular server-side scripting language that can be used to create dynamic web pages. One of the common functionalities required in many web applications is the ability to send emails. In this blog post, we'll go through the process of setting up and using PHP to send emails with XAMPP.

Prerequisites

  1. Ensure that you have XAMPP installed on your system. XAMPP is an open-source cross-platform web server solution stack package that includes Apache HTTP Server, MariaDB database, and interpreters for scripting languages like PHP. You can download it from Apache Friends.
  2. Familiarize yourself with the PHP mail function. This is a built-in feature in PHP which allows sending emails.

Configuring XAMPP for Sending Emails

Before you start sending emails using PHP, you need to configure your XAMPP environment for email functionality. Here's how:

  1. Open the php.ini file located in xampp\php. Make a backup of this file before making any changes.
  2. Find and uncomment the following lines:
    ; For Sendmail, you might need to specify your smtp server and return path.
    ; Uncomment these to suit your needs
    ;SMTP = localhost
    ;smtp_port = 25
    ;smtp_auth = true
    ;smtp_username =
    ;smtp_password =
    
  3. Set the SMTP, smtp_port, smtp_auth, and other related variables based on your email server's settings. For example, if you are using a Gmail account, set SMTP to smtp.gmail.com and smtp_port to 587. Also, update smtp_username and smtp_password with your Gmail account email and password respectively.
  4. Save the changes and restart Apache and MariaDB services in XAMPP.

Sending Emails Using PHP

Now that you have configured XAMPP for sending emails, let's create a simple PHP script to test this functionality.

<?php
$to = "recipient@example.com";
$subject = "Test Email";
$message = "Hello! This is a test email sent from PHP.";
$headers = "From: sender@example.com\r\nContent-type: text/plain; charset=UTF-8";

mail($to, $subject, $message, $headers);
if(mail($to, $subject, $message, $headers)) {
    echo "Email sent successfully.";
} else {
    echo "Failed to send email.";
}
?>

Replace recipient@example.com, sender@example.com, and the message text with your own values. Save this file as send_email.php. Run it by accessing it in a web browser or via an HTTP client like cURL.

If the email is sent successfully, you will see "Email sent successfully." displayed on the screen. If not, an error message will be shown instead.

Conclusion

In this blog post, we walked through the process of configuring XAMPP for sending emails using PHP. We also created a simple PHP script to test email functionality. With these steps in place, you can now send emails as part of your web applications built with PHP and XAMPP.

Published October, 2015