Hello readers, Today in this blog you'll learn how to send Mail from Localhost XAMPP using PHP. Simply, XAMPP is an abbreviation for cross-platform, Apache, MySQL, PHP, and Perl. XAMPP lets you work on a local server and test local copies of websites using PHP code and MySQL databases.

How to configure XAMPP to send Mail from Localhost in PHP

As a part of the experiment, developers need to send emails, and as we all know that sending mail in XAMPP through the localhost can be much more painful if we don’t know how to configure XAMPP to send mail through the localhost using Gmail.

To send mail from localhost XAMPP using Gmail, configure XAMPP after installing it. To configure the XAMPP server to send mail from the localhost, we have to make some changes in two files one is php and another one is sendmail.ini.

First, go to the XAMPP installation directory and open the XAMPP folder and follow the below steps same:

1. Go to the (C:\xampp\php) and open the php 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 starting of each line, simply remove the semicolon from each line which is given below.

PHP FILE:

That's all for this file, save the file by pressing ctrl+s and close it.

2. Now, go the (C:\xampp\sendmail) and open the sendmail.ini  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.

SENDMAIL.INI FILE:

That's all for this file, save the file by pressing ctrl+s and 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 send mail using Gmail paste the following codes in your PHP file.

PHP CODE:

Important note: If your mail isn't sent and you got a warning or error. Please configure your google account security as "Less secure apps". To configure it:
- Go to your Google account then click on the Security tab and scroll down, there you can see the Less secure app access panel, Simply turn on that. This panel only shows if you haven't enabled 2-Step Verification. alert-info

If you're getting an error as a password is wrong then simply go to the sendmail.ini file and put the correct password of your Gmail account which you've provided in sendmail_from in sendmail.ini.

Thanks for visiting, Keep visiting.


88 Comments

We welcome relevant and respectful comments. Off-topic or spam comments may be removed.

  1. Thanks for this, now if we are using another server online,(after publishing our website online),will the mail sending still work?

    ReplyDelete
  2. Thanks for this, now if we are using another server online, (after uploading our codes online), will the mail sending still work?

    ReplyDelete
    Replies
    1. No you've configure require changes in your server file. This is only works on XAMPP server.

      Delete
  3. can u make something like this again but with rust instead of php. Rust is faster and synatically equal to c++.

    ReplyDelete
  4. which php file should i copy and paste the last code ?, i got an error that says failed while sending code

    ReplyDelete
    Replies
    1. You've to create a PHP file with the extension .php and paste the last codes on that PHP file but before pasting the codes make sure you've configured the XAMPP and turn on less secure app on your google account. You can read this blog carefully.

      Delete
  5. Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\usertable\controllerUserData.php on line 33

    I KEEP GETTING THIS

    ReplyDelete
    Replies
    1. You haven't configured your XAMPP properly...Please read this blog carefully and apply the changes in your files.

      Delete
    2. did you find the solution on this error? need help...

      Delete
  6. Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\usertable\controllerUserData.php on line 33

    what does it mean

    ReplyDelete
    Replies
    1. Please recheck your changes files with the above given codes.

      Delete
  7. I followed all the steps but an error message appeared to me saying
    Warning: mail (): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set () in C: \ xampp \ htdocs \ p1 \ mail.php on line 7
    Sorry, failed while sending mail

    ReplyDelete
  8. Hola buenas tardes, me manda el siguiente error:


    Advertencia:mail(): No se pudo conectar al servidor de correo en el puerto 25 "localhost", verifique su configuración "SMTP" y "smtp_port" en php.ini o utilice ini_set() en C:-xampp-htdocs-Verificacion de email-controllerUserData.php en la línea 33

    ReplyDelete
  9. sir, i configured properly...... but when i try to signup .... it redirect me to otp verification page.... no email comes to the gmail... please help me...

    ReplyDelete
    Replies
    1. Maybe the email address you've entered doesn't exist or You may haven't on Less Secure Apps panel in your google account

      Delete

  10. Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\swda\controllerUserData.php on line 33


    please help sir

    ReplyDelete
    Replies
    1. Please configure carefully. Maybe you did some mistakes.

      Delete
  11. <?php
    $receiver = "receiver_email@gmail.com";
    $subject = "Email Test via PHP using Localhost";
    $body = "Hi, there...This is a test email send from Localhost.";
    $sender = "From:sender_email@gmail.com";

    if(mail($receiver, $subject, $body, $sender)){
    echo "Email sent successfully to $receiver";
    }else{
    echo "Sorry, failed while sending mail!";
    }

    This php file with what name we have to save and where it showing error

    Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. 78sm5550625pfz.211 - gsmtp in C:\xampp\htdocs\Login and Signup Form with Email Verification - PHP\controllerUserData.php on line 33 (Failed while sending code) I configure all xampp and sendmail settings.

    ReplyDelete
    Replies
    1. Create a php file inside the HTDOCS folder with .php extension and paste the following codes in this file then put the receiver email and sender email address in the mentioned fields.

      Delete

  12. Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. x5sm10407906pfr.83 - gsmtp in C:\xampp\htdocs\Login and Signup Form with Email Verification - PHP\controllerUserData.php on line 33

    ReplyDelete
  13. Great 😊 thanks you
    For help 👍

    ReplyDelete
  14. bro please make a video for it i am confused

    ReplyDelete
  15. When run the php file result is email sending failed

    ReplyDelete
    Replies
    1. Did you enbale less secure app panel on your google account?

      Delete
  16. Sir I configured properly but result is email sending failed.pls help

    ReplyDelete
    Replies
    1. Did you enable less secure app panel on your google account?

      Delete
  17. When will you be making this video, I really need help on this for my school project?

    ReplyDelete
    Replies
    1. Can you send me the screenshot of your problems on my Instagram account?

      Delete
  18. No error in the code, but mail is not sending

    ReplyDelete
  19. No error while PHP codes runs ,and it turn to OTP page.but mail didn't send to corresponding ?.pls help me out

    ReplyDelete
  20. Notice: Undefined index: message in C:\xampp\htdocs\mail.php on line 3

    Warning: mysqli::__construct(): (HY000/1049): Unknown database 'contact us' in C:\xampp\htdocs\mail.php on line 6
    Connection Failed : Unknown database 'contact us'

    ReplyDelete
  21. Notice: Undefined index: message in C:\xampp\htdocs\mail.php on line 3

    Warning: mysqli::__construct(): (HY000/1049): Unknown database 'contact us' in C:\xampp\htdocs\mail.php on line 6
    Connection Failed : Unknown database 'contact us'

    ReplyDelete
  22. we are feeling bore with HTML & CSS only. PLease Upload PHP related videos atleast 2 in a week

    ReplyDelete
  23. Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. r17sm13504747pfr.153 - gsmtp in C:\xampp\htdocs\mail.php on line 7
    Sorry, failed while sending mail!

    bro, create a telegram group so that we will share our queires there so that you can respond to all

    ReplyDelete
  24. Sir, when will you make a tutorial about the login and sign up with email otp verification??? huhu

    ReplyDelete
  25. after registration i keep getting error of failed while sending code even after enabling less secure apps what should i do

    ReplyDelete
  26. for all of u that have "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\swda\controllerUserData.php on line 33 error pls restart your apache.

    ReplyDelete
  27. Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp64\www\login\controllerUserData.php on line 118
    what was the mistake?

    ReplyDelete
  28. how do I get a specific password from google?? the error log keeps giving me this msg Application-specific password required. failed while sending email

    ReplyDelete
  29. Hi thanks for this tutorial and instructions but I got the error warning after I signed up.

    The data is already input on my data base. Name, email, password and the code also the verification

    But the email is not.
    Warning said is mail():SMTP server response 530 5.7.0 must issue a starTTLS command first.gjlsm12559445pjb.11 - gsmtp in "distinction folder" on line 33 where located this code-->> if(mail($email, $subject, $message, $sender)){ . . . . ..

    ReplyDelete
    Replies
    1. Please read the blog carefully...I can't do anything except this...All answers are in the blog.

      Delete
    2. hi? did you find any solution for this? It is the same error I'm facing rn help plsss

      Delete
  30. sorry to disturb admin. I have followed the instructions for creating your website and I entered the registration menu. I have filled in all the data then I press the register button. a text appears. FAILED WHEN ENTERING DATA TO DATABASE ??
    Please explain where I went wrong. Thank you

    ReplyDelete
  31. sorry to disturb admin. I have followed the instructions for creating your website and I entered the registration menu. I have filled in all the data then I press the register button. a text appears. FAILED WHEN ENTERING DATA TO DATABASE ??
    Please explain where I went wrong. Thank you

    ReplyDelete
  32. sorry to disturb admin. I have followed the instructions for creating your website and I entered the registration menu. I have filled in all the data then I press the register button. a text appears. FAILED WHEN ENTERING DATA TO DATABASE ??
    Please explain where I went wrong. Thank you

    ReplyDelete
    Replies
    1. This blog is about configuring XAMPP...I think you're facing the error which is not related to this blog. So please read this carefully - https://www.codingnepalweb.com/2020/09/login-signup-form-with-email-verification-php-mysql-bootstrap.html and must be careful while putting the name of database, table, and columns.

      Delete
  33. Login-singup-form-with-email-verification using phpmailer can you make this video

    ReplyDelete
  34. Thanks for the tutorial. I got this error:
    21/01/20 11:13:23 : Username and Password not accepted. Learn more at https://support.google.com/mail/?p=BadCredentials f13sm1134675iog.18 - gsmtp

    To fix this problem, I had to login to my Gmail account, go to security, and then turn on "Less secure app access." This is "not recommended" according to Google, but it wouldn't work without this setting turned on.

    ReplyDelete
  35. Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\email\controllerUserData.php on line 19
    whats happen??

    ReplyDelete
  36. Warning: mail(): Failed to connect to mailserver at "smtp.gmail.com" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\SIS\contact.php on line 41



    its been 2hrs since ive been trying to read the blog
    pleasseeee helpppp

    ReplyDelete
  37. i already got it after 3h THANKK YOU.
    for errors , kindly restart your apache after encoding those codes.
    and some are in comment in php.ini so we should remove the " ; " to enable the lines
    after that dont forget to enable the less app in gmail settings

    ReplyDelete
    Replies
    1. Great now you can send Email to any existing email address from localhost

      Delete
  38. Please why do I receive an error message say "Access denied"while saving the PHP.ini file

    ReplyDelete
    Replies
    1. Maybe you don't have permission to change the C drive files

      Delete
  39. Also there's PHP.in-production and PHP.ini-development

    ReplyDelete
    Replies
    1. The above one is. There is a name with php before php.ini-development. You've to open it

      Delete
  40. I keep getting this error. Please help. I have configured properly as mentioned abovee

    Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

    ReplyDelete
  41. Jai tombé sur cet erreur là: "Failed while sending code!"

    ReplyDelete
  42. It would have been better if there was a video to help beginners like me

    ReplyDelete
    Replies
    1. Yes I think it definitely help to all beginners.

      Delete
  43. Thanks worked .. I was getting multiple issues ..as I open the .ini files in notepad... If you can update the comment .. the ini.. to be opened in PHP ... most error will be sorted.

    ReplyDelete
    Replies
    1. Ok I'll update it soon. When I posted this blog, there is php.ini file for configuration but now it's only php.

      Delete
  44. auth_username=your_email_address_here
    auth_password=your_password_here

    in this we have to pass email address and password or paste it like this only

    ReplyDelete
  45. not able to find mail function in php

    ReplyDelete
  46. its not working please make it on javascript smtp.js

    ReplyDelete
  47. i watched your other tutorial and i have a question sir?
    should i enter the last part of the code PHP CODE: to my controllerUserData.php or separate file?

    ReplyDelete
  48. Heyy the last php code
    So be with which name just .php or like some name send.php???
    PLZZ helpp

    ReplyDelete
  49. 1.when i click on the send mail it shows this page is not working can you please help?
    2.we need to link our php file to index.html or not?

    ReplyDelete
  50. Hi, how can I config wampp server for send mail

    ReplyDelete
  51. I didn't recieve the email, and yes, I turn on the "less secure apps" on Google. The blog was great but I think you should update it, so that you can address the multiple problems we are all having.

    ReplyDelete
  52. Hello sir how to use on infinity hosting or any website i upload this login page but not working

    ReplyDelete
  53. $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!";
    }
    where want to paste this code. pls

    ReplyDelete
  54. hello sir how to configure login and signup in cpanel

    ReplyDelete

Post a Comment

We welcome relevant and respectful comments. Off-topic or spam comments may be removed.

Previous Post Next Post
CodingNepal
Subscribe our YouTube Channel