- How to Send Mail using XAMPP Server with PHP
- You might like this:
- How to configure XAMPP to send Mail from Localhost in PHP ?
- First, go to the XAMPP installation directory and open the XAMPP folder and follow the below steps same: I’ve installed XAMPP in the C directory.
- How to Configure XAMPP to Send Email from Localhost with PHP
- Steps to Send an Email with the help of XAMPP
- Step 1: XAMPP Installation Directory
- Step 2: Go and Open php.ini
- Step 3: Find [mail function]
- Step 4: Pass Values
- Step 5: Open sendmail.ini
- Step 6: Find [sendmail]
- Step 7: Search and Pass Values
- Script for Sending Email
- What is XAMPP?
- Send Mail From Localhost in PHP Using XAMPP
- 4+ Steps to Send Mail From Localhost in PHP Using XAMPP
- Create an APP password to setup Gmail SMTP
- Modify [mail function] on the PHP config file (php.ini)
- Changes on [mail function] on php.ini file
- Modify [mail function] on the SendMail config file (sendmail.ini)
- Changes on [mail function] on sendmail.ini file
- How to configure XAMPP to send Mail from Localhost in PHP ?
How to Send Mail using XAMPP Server with PHP
Hello readers, Today in this blog you’ll learn how to Send Emails with PHP & Gmail | Send Mail from Localhost using XAMPP Server. Earlier I have shared a blog on How to configure XAMPP to send Mail from Localhost in PHP? If you haven’t still read that blog, I want to suggest you read that blog first. Generally, in this program, on the webpage, there is a mail send the form with three inputs — email address, subject, and message. When you click on the send button without filling the form completely, there is a display of an alert labeled as «All input fields are required!». And when you filled up all inputs and click on the send button, your mail will be sent to a particular email address that you’ve provided in the recipient field and there is also display a success message labeled as «Your mail sent successfully to someone email». If somehow mail can’t be sent, there is a display of an alert labeled as «Sorry, failed while sending mail!». If you’re feeling difficult to understand what I am saying. You can copy the codes from the given boxes or download the code files from the given link but I recommend you to download the source code files instead of copying codes. Click here to download code files.
You might like this:
PHP CODE:
Send Message
Send mail to anyone from localhost.
else < // PHP function to send mail if(mail($recipient, $subject, $message, $sender))< ?> else < ?> > > ?> "> CSS CODE:
/* custom css styling */ @import url('https://fonts.googleapis.com/css?family=Poppins:400,500,600,700&display=swap'); html,body < background: #007bff; >::selection < color: #fff; background: #007bff; >.container < position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-family: 'Poppins', sans-serif; >.container .mail-form < background: #fff; padding: 25px 35px; border-radius: 5px; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19); >.container form .form-control < height: 43px; font-size: 15px; >.container .mail-form form .form-group .button < font-size: 17px!important; >.container form .textarea < height: 100px; resize: none; >.container .mail-form h2 < font-size: 30px; font-weight: 600; >.container .mail-form p
How to configure XAMPP to send Mail from Localhost in PHP ?
Hello friends, today in this blog you’ll learn How to configure XAMPP to Send Mail from Localhost in PHP. If you don’t know, XAMPP is an abbreviation for cross-platform, Apache, MySQL, PHP, and Perl. Using XAMPP we can work on our local server and test our local copies or projects of websites using PHP code and MySQL databases.
As a part of the experiment, developers need to send emails and we all know that sending mail from localhost using PHP can be much more painful if we don’t know how to properly configure XAMPP for it.
Before starting this blog, if you want to send an email in node.js applications, then you can see this blog How to Send Email in Node.js using Nodemailer & Gmail.
To send mail from localhost using XAMPP, we’ve to configure XAMPP after installing it. To configure the XAMPP server to send mail from the local host, we have to make some changes in two files one is PHP and another one is Sendmail.
First, go to the XAMPP installation directory and open the XAMPP folder and follow the below steps same: I’ve installed XAMPP in the C directory.
1. Go to the (C:xampp\php) and open the PHP configuration setting file then find the [mail function] by scrolling down or simply press ctrl+f to search it directly then find the following lines and pass these values. Remember, there may be a semicolon ; at the start of each line, simply remove the semicolon from each line which is given below.
[mail function] For Win32 only. http://php.net/smtp SMTP=smtp.gmail.com http://php.net/smtp-port smtp_port=587 sendmail_from = your_email_address_here sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
That’s all for this file, press ctrl+s to save this file and then close it.
2. Now, go the (C:\xampp\sendmail) and open the sendmail configuration setting file then find sendmail by scrolling down or press ctrl+f to search it directly then find the following lines and pass these values. Remember, there may be a semicolon ; at the start of each line, simply remove the semicolon from each line which is given below.
smtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log auth_username=your_email_address_here auth_password=your_password_here force_sender=your_email_address_here (it's optional)
That’s all for this file, press ctrl+s to save this file and then close it. After all changes in the two files, don’t forget to restart your apache server.
Now, you’re done with the required changes in these files. To check the changes you’ve made are correct or not. First, create a PHP file with the .php extension and paste the following codes into your PHP file. After pasting the codes, put your details into the given variables – In the $receiver variable put the receiver email address, in the $subject variable put the email subject and do respectively.
After completing these steps, just open this PHP file on your browser. If your mail is sent successfully then there is appears a success message “Email sent successfully to …..” and in the case your mail not sent then there is appears “Sorry, failed while sending mail!”.
If mail is sent then check whether the receiver has got your email or not. If yes, then great you did all changes perfectly. If not, check all the changes that you have done earlier are correct or not.
Note: Google removed the “Less secure apps” feature on May 30, 2022. So you need to do some extra steps and make changes in the files to send mail from localhost using Gmail.
Instead of using your Google account password as auth_password in sendmail configuration file, you need to use an App password which is 16 characters long.
To create an app password, go to manage your google account > Click Security > Under “Signing in to Google,” select App passwords > Select App & Device, and then you’ll get a 16-character code and this is your password. For more details about creating App Passwords, read the Official Google Article.
How to Configure XAMPP to Send Email from Localhost with PHP
It is often necessary to configure the XAMPP server for sending email from the localhost using PHP. To meet that goal, you need to make changes to two files such as sendmail.ini and php.ini. Here, we will represent to you what steps you need to take to achieve that.
Steps to Send an Email with the help of XAMPP
Below, you can find the steps to configure XAMPP for sending emails from the local host with PHP.
Step 1: XAMPP Installation Directory
The first step is opening the XAMPP Installation Directory.
Step 2: Go and Open php.ini
The second step is going to C:\xampp\php and opening the php.ini file.
Step 3: Find [mail function]
In the framework of the third step, you should press ctrl + f for finding the [mail function] .
Step 4: Pass Values
Now, it is necessary to detect and pass the values, demonstrated below:
SMTP=smtp.gmail.com smtp_port=587 sendmail_from = [email protected] sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
Step 5: Open sendmail.ini
The next step is going to C:\xampp\sendmail and opening the sendmail.ini file.
Step 6: Find [sendmail]
Press ctrl + f for finding [sendmail] .
Step 7: Search and Pass Values
The seventh step considers searching and passing the values, demonstrated below:
mtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log [email protected] auth_password=Your-Gmail-Password [email protected]com(optional)
Script for Sending Email
Finally, let’s see how the script for sending emails looks like:
$to_email = "[email protected]"; $subject = "Simple Email Testing via PHP"; $body = "Hello,nn It is a testing email sent by PHP Script"; $headers = "From: sender\'s email"; if (mail($to_email, $subject, $body, $headers)) < echo "Email successfully sent to $to_email. "; > else < echo "Email sending failed. "; > ?>
After following the steps above, you will manage to send an email. However, if you still face problems in sending an email, then the reason can be among the following:
- A broken port.
- A wrong password.
- The emails are sent late.
- Gmail doesn’t obtain the necessary permissions.
- Wrong email configuration.
What is XAMPP?
XAMPP is considered a free and open-source cross-platform web server solution stack package. It is designed and developed by Apache Friends. It consists of the MariaDB database, Apache HTTP Server, as well as interpreters for scripts written in the PHP and Perl programming languages.
Send Mail From Localhost in PHP Using XAMPP
Send Mail From Localhost in PHP Using XAMPP, Firstly, PHP is an open-source scripting language, which means where you can change your.
Send Mail From Localhost in PHP Using XAMPP, Firstly, PHP is an open-source scripting language, which means where you can change your PHP core files according to your requirements.
PHP also has a SENDMAIL inbuilt package, which is used to send mail from a localhost server like XAMPP.
The below process explained from the XAMPP server.
4+ Steps to Send Mail From Localhost in PHP Using XAMPP
Here are list of all the steps which I explained below completely.
- Create an APP password to setup Gmail SMTP.
- Modify [mail function] on the PHP config file (php.ini) .
- Modify [mail function] on the SendMail config file (sendmail.ini) .
- Restart Apache server.
- Try the example source code.
Create an APP password to setup Gmail SMTP
To access Gmail account which is protected by 2Step Authentication we have to create APP password which helps to send mail using our Gmail account as SMTP account.
- Go to https://security.google.com/settings/security/apppasswords
- Select Mail and the Other like below image,
Then you get the password like “woaablyvvycyznenv” this. Keep it safe for further.
Modify [mail function] on the PHP config file (php.ini)
- Go to this location: C:\XAMPP\php\php.ini
- Open php.ini file on any text editor and search [mail function]
Changes on [mail function] on php.ini file
Remove semicolon (;) first on below lines, and put the given changes,
SMTP=smtp.gmail.com
smtp_port=465
sendmail_from = yourGmailID@gmail.com
sendmail_path = “\”C:\xampp\sendmail\sendmail.exe\” -t”
Modify [mail function] on the SendMail config file (sendmail.ini)
- Now next is the Open Sendmail configuration file named sendmail.ini.
- path:c:\xampp\sendmail\sendmail.ini
search [mail function]
Changes on [mail function] on sendmail.ini file
- smtp_server=smtp.gmail.com
- smtp_port=465
- smtp_ssl=auto
- error_logfile=error.log
- debug_logfile=debug.log
- auth_username=writeyourgmail@gmail.com
- auth_password=PUTthecreatedAppPasswordOnFirstStep
Restart the APACHE Server on XAMPP control panel.
How to configure XAMPP to send Mail from Localhost in PHP ?
That’s all for this file, press ctrl+s to save this file and then close it.
- Now, go the (C:\xampp\sendmail) and open the sendmail configuration setting file then find sendmail by scrolling down or press ctrl+f to search it directly then find the following lines and pass these values. Remember, there may be a semicolon ; at the starting of each line, simply remove the semicolon from each line which is given below.
smtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log auth_username=your_email_address_here auth_password=your_password_here force_sender=your_email_address_here (it's optional)
that’s all for this file, press ctrl+s to save this file and then close it. After all changes in the two files, don’t forget to restart your apache server.
Now, you’re done with the required changes in these files. To check the changes you’ve made are correct or not. First, create a PHP file with the .php extension and paste the following codes into your PHP file. After pasting the codes, put your details to the given variables – In the $receiver variable put the receiver email address, in the $subject variable put the email subject and do respectively.
$receiver = "receiver email address here"; $subject = "Email Test via PHP using Localhost"; $body = "Hi, there. This is a test email send from Localhost."; $sender = "From:sender email address here"; if(mail($receiver, $subject, $body, $sender)) echo "Email sent successfully to $receiver"; >else echo "Sorry, failed while sending mail!"; > ?>
After completing these steps, just open this PHP file on your browser. If your mail is sent successfully then there is appears a success message “Email sent successfully to …..” and in the case your mail not sent then there is appears “Sorry, failed while sending mail!”.
If mail is sent then check the receiver has got your email or not. If yes, then great you did all changes perfectly. If no, check all the changes that you have done earlier are correct or not.