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.
XAMPP stands for:
- Cross-Platform -X
- Apache -A
- MySQL -M
- PHP -P
- Perl -P
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.
Example of Send Mail From Localhost in PHP Using XAMPP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php // the message $msg = "First line of text\nSecond line of text"; // use wordwrap() if lines are longer than 70 characters $msg = wordwrap($msg,70); $email = "phpcodertech@gmail.com"; //mail headers are mandatory for sending email $headers = 'From: ' .$email . "\r\n". 'Reply-To: ' . $email. "\r\n" . 'X-Mailer: PHP/' . phpversion(); // send email if (mail("phpcodertech@gmail.com","My subject",$msg,$headers)) { echo "Good to go!"; }else{ echo "Something Wrong"; } ?> |
These are the complete steps for sending mail from localhost in PHP.
Also, Check:
Media Query CSS for Desktop, Tablet, Mobile
Happy Coding..!
it works for me. HAPPY NEW YEAR
[…] Send Mail From Localhost in PHP Using XAMPP […]
[…] And if you want to send mail using the localhost, you can check Send Mail From Localhost in PHP Using XAMPP. […]
[…] Here you can check how we can create a google app password https://codingtasks.net/send-mail-from-localhost-in-php-using-xampp/#Create_an_APP_password_to_setup_G…. […]
Thanks brother , email sending option is working !.
[…] Also Read: Send Mail From Localhost in PHP Using XAMPP […]
[…] Send Mail From Localhost in PHP Using XAMPP […]
[…] Send Mail From Localhost in PHP Using XAMPP […]