Saturday, December 22, 2018

How to send an email from localhost in Xampp

You can’t just log in to our cPanel every single time you want to check your code which sends emails. For simple mailing functions, there is no need for testing because when you write it carefully, there’s no chance for mistake. But when doing something complex with the mail function, you definitely need to test it before making it live in order check if you have written the code correctly. Most back-end web developers do it on localhost and the commonly used tool is ‘XAMPP’ which I personally use.
Everything works smoothly with XAMPP but to use the mail function, you will have to configure it first in order to send emails from your localhost. Here’s what you need to do-

  • Visit your ‘XAMPP’ folder. Most probably C:\XAMPP.
  • Then look for the folder ‘php’ and inside it, there will be a file called ‘php.ini’. Open it with notepad or any of your code editor and then change the followings-
  • Search for sendmail_path and look for the below two lines-
    ; sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
    sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"
  • Remove the semicolon ‘;‘ from the first line and add a semicolon to the second line. After the editing, it should look like the below image-
  • configure-xampp-for-mail-php
  • Now save the ‘php.ini’ file and look for the ‘sendmail.ini’ file inside the ‘sendmail’ folder.
  • Change the followings-
  1. smtp_server=smtp.gmail.com
  2. smtp_port=587
  3. auth_username=youremail@gmail.com
  4. auth_password=yourgmailpassword
Have a look at the below image-
configure-gmail-xampp
The Gmail account which you have added will be used to send emails ‘from’. In case of other email accounts like yahoo, outlook and all, look for their smtp server and smtp port which should be available on the internet. And to find your smtp server and port for your own domain’s email address, log in to your cPanel, then click on ‘Email Accounts’ and then in the right corner of your email address, there should be a button called ‘more’. Click on that and then click on ‘Configure Email Client’. You will get all your details there.
Now run the below piece of PHP code on your localhost and the mail should be sent successfully.
<?php
$to = "someone@example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: from@example.com";
mail($to,$subject,$txt,$headers);
?>
Also, more Read for freshers crack an interview

No comments:

Post a Comment