How to configure XAMPP to send mail from localhost?
Asked Answered
C

11

289

I am trying to send mail from localhost. but i am unable to send the mail from localhost so can anybody tell me that how to reconfigure my xampp to send mail from localhost

Cathedral answered 12/4, 2013 at 7:20 Comment(3)
Can we see your code?Ergener
Impossible to help if you don't give us the current configuration.Ragnar
Is this still relevant with Mercury included (ApacheFriends XAMPP 1.8.2)? I would think yes, but if there are people preferring Mercury, please indicate below. :) Thanks!Koto
R
409

You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.

for example you can configure C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.

in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.

in php.ini file find [mail function] and change

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=my-gmail-password
[email protected]

Now you have done!! create php file with mail function and send mail from localhost.

PS: don't forgot to replace my-gmail-id and my-gmail-password in above code. Also, don't forget to remove duplicate keys if you copied settings from above. For example comment following line if there is another sendmail_path : sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe" in the php.ini file

Also remember to restart the server using the XAMMP control panel so the changes take effect.

For gmail please check https://support.google.com/accounts/answer/6010255 to allow access from less secure apps.

To send email on Linux (with sendmail package) through Gmail from localhost please check PHP+Ubuntu Send email using gmail form localhost.

Rub answered 12/8, 2013 at 10:57 Comment(26)
other than the configs above, i needed to update my sendmail glob.com.au/sendmailTemper
I had to upgrade from XAMMP 1.7.7 to 2.8.2.3 -- sendmail is included in the newer XAMPP. I tried using a newer sendmail with 1.7.7 a few months ago and couldn't get it to work -- finally bit the bullet and used the new XAMPP and it works as described.Garage
@Garage thumbs up !Rub
$to = '[email protected]'; $subject = 'Testing sendmail.exe'; $message = 'Hi, you just received an email using sendmail!'; $headers = 'From: [email protected]' . "\r\n" . 'Reply-To: [email protected]' . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); if(mail($to, $subject, $message, $headers)) echo "Email sent"; else echo "Email sending failed"; i tried with this code, but doesn't get success while I got "Email sent" massage on my screenMesdemoiselles
I am successful by using this method.Some times I colud send message. But Always the message is not send, Why? Can you tell me? Please. @VikasDwivediCoccyx
@MuhammadAshikuzzaman sometimes it didn't send mail because of slow internet speed, every time you try to send mail it establish connection to gmail smtp server..you need to check debug/error logs C:\xampp\sendmail\debug.log for details.Rub
Yes, I have understand the problemCoccyx
I believe the location C:\xampp\php\php.ini refers to windows environments. For mac look in xamppfiles/etc for your php.ini file.Marienbad
@Vikas answer was best, but please note that using the default ssl on smtp_port might not work in certain condition, using 465 as Thirumalai murugan answer has stated actually fixed it for mePrompter
Hello @VikasDwivedi can you tell me how to send it from local server? Message are going to SpamCoccyx
@VikasDwivedi I did everything as said. But my emails are getting failed. In debug file it goes till authentication like 14/12/03 18:21:45 ** Authenticating as [email protected] 14/12/03 18:21:45 >> STARTTLS<EOL> 14/12/03 18:21:46 << 220 2.0.0 Ready to start TLS<EOL> 14/12/03 18:21:46 >> QUIT<EOL> and then lots of codes in unknown language. then 14/12/03 18:21:47 ** Disconnected. 14/12/03 18:21:47 ** Disconnecting from smtp.gmail.com:25 14/12/03 18:21:47 ** Disconnected. 14/12/03 18:21:47 ** Disconnected. 14/12/03 18:21:47 ** Connection Closed Gracefully. I dont know whats wrong.Sloatman
Its not working. Read thisSubgroup
I followed the exact same steps but I get 15/07/16 00:15:30 : Socket Error # 10060<EOL>Connection timed out. in my C:\xampp\sendmail\error.log file , any thoughts there ? @MuhammadAshikuzzamanNippon
followed all the above steps but my code is failed to send a mail--> mail($to,$subject,$message,$headers)Rubicund
@Rubicund For gmail please check [support.google.com/accounts/answer/6010255][1] to allow access from less secure apps.Rub
provide the access for less secure app but still not receiving any mail. Error message: "16/02/05 11:00:06 : Connect timed out. Debug message: 16/02/05 11:04:56 ** Connecting to smtp.gmail.com:587 16/02/05 11:05:06 ** Disconnected."Rubicund
@VikasDwivedi how to do this in linux using xampp. are there alternative to sendmail.exe for linux, then what is it?Irreformable
@KasunSiyambalapitiya please check phpcubes.com/… for configuration in UbuntuRub
Any idea how to do this in Mac?Riesman
It's Working perfectly with XAMPP version 7.0.15 Just don't forget to remove the Semicolon from it and also login to the same Gmail id that you have configured then go to the below link myaccount.google.com/lesssecureapps?pli=1 and Allow less secure apps: ON Now you can send the Mail directly from the XAMPP localhostClayborn
Gmail nowadays does not permit plain login passwords, but requires application passwords, support.google.com/accounts/answer/185833Germinant
Well its great, not receiving any error. But somehow I am not getting the email after submitting the form.Does anyone know what may be the problem?Sennit
scratched my head on the Windows XAMPP. The sendmail.ini file changes did the trick. Thank you very much. Though the php.ini file says that the sendmail_path change is only for Unix environment, I had followed exactly what you had mentioned and it worked like a charm. Of course, I did allow the 'less secure apps' settings to allow the apps to connect to my gmail account.Justify
I have one out of context question. How can I get to learn stuff like this about configuring what files, modifying which code, etc.?Whimwham
As of May 2022 Google has locked down the "Less secure access" that is required to make this work. (It is only available on google workspace accounts) - does anyone know if there is still a way to do this? (or another recommended service combination?)Getraer
The only thing different for this to work in 2023 is to enable 2 factor authentication in gmail and create an App Password, so you can use it in your app. For more details on this, refer to this link: aruljohn.com/blog/php-send-emailAldenalder
M
39

In XAMPP v3.2.1 for testing purposes you can see the emails that the XAMPP sends in XAMPP/mailoutput. In my case on Windows 8 this did not require any additional configuration and was a simple solution to testing email

Marienbad answered 13/7, 2015 at 13:41 Comment(6)
And if this isn't working, check your c:\xampp\php\php.ini, look for [mail function], and uncomment the line that mentions mailtodisk.exe. Keep every other line in this section commented out except for the mail.add_x_header one.Fromm
As an addemdum to my comment above, for the more recent versions of XAMPP, you may need to manually add the mailtodisk line. It should look something like this: sendmail_path="C:\xampp\mailtodisk\mailtodisk.exe"Fromm
An additional addendum to comment from @TimMalone. I found that mail would only be written to XAMPP/mailoutput if running XAMPP as adminFootboy
@sohail: You don't have to run XAMPP as admin but you have to give permission for all users to write to the folder XAMPP/mailoutput (right click on folder > security > all users > edit permissions > full access)Regalia
@TimMalone I have applied the both suggestions but nothing in the mailoutput directory even after so many triesDiaconate
Just wanted to send thanks to @TimMalone - by far the easiest way to see the mails generated on WindowsTeaspoon
T
39

You can test send mail in Your PC without Internet

you should use Papercut this simple application to test send mail. and you don't need to configure anything.

Just run it and try test send mail:

test_sendmail.php

<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "\r\n" .
"CC: [email protected]";

mail($to,$subject,$txt,$headers);
?>

and you will see this:

enter image description here

I hope you will have a good day. you can find me on Youtube for more tutorial Piseth Sok

Cheer!

Thoma answered 8/9, 2017 at 3:38 Comment(3)
This is a really great solution, quick easy and I can just get straight into work. Stuffing around with mail servers locally is a hassle when it's only for dev.Corporator
Thanks I used this for testing my project too.Thoma
Wow, this is amazing! No setup or configuration needed. It just works!Stair
P
26

Its very simple to send emails on localhost or local server

Note: I am using the test mail server software on Windows 7 64bit with Xampp installed

Just download test mail server tool and install according to the instruction given on its website Test Mail Server Tool

Now you need to change only two lines under php.ini file

  1. Find [mail function] and remove semi colon which is before ;smtp = localhost
  2. Put the semi colon before sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

You don't need to change anything else, but if you still not getting emails than check for the SMTP port, the port number must be same.

The above method is for default settings provided by the Xampp software.

Pontificate answered 14/4, 2014 at 13:21 Comment(7)
If you don't trust that site, you might want to use papercut from codeplex instead: papercut.codeplex.com.Laplante
I did not have to do anything to make it work, just install it.Phytohormone
I just had to change the port thoughDermatogen
What is username & password in this instance?Jounce
@Jounce username & password of what?Pontificate
ignore me I was tired. I meant the email address but it was a silly question.Jounce
This did the trick. XAMPP PHP, Thunderbird as client. Easy to get up and running.Lymphoblast
L
20

You have to configure SMTP on your server. You can use G Suite SMTP by Google for free:

<?php

$mail = new PHPMailer(true);

// Send mail using Gmail
if($send_using_gmail){
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth = true; // enable SMTP authentication
    $mail->SMTPSecure = "ssl"; // sets the prefix to the servier
    $mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
    $mail->Port = 465; // set the SMTP port for the GMAIL server
    $mail->Username = "[email protected]"; // GMAIL username
    $mail->Password = "your-gmail-password"; // GMAIL password
}

// Typical mail data
$mail->AddAddress($email, $name);
$mail->SetFrom($email_from, $name_from);
$mail->Subject = "My Subject";
$mail->Body = "Mail contents";

try{
    $mail->Send();
    echo "Success!";
} catch(Exception $e){
    // Something went bad
    echo "Fail :(";
}

?>

Read more about PHPMailer here.

Lowminded answered 12/4, 2013 at 7:25 Comment(3)
@Vikas answer was best, but please note that using the default ssl on smtp_port might not work in certain condition, using 465 as this answer has stated actually fixed it for mePrompter
Iused @Vikas answer but I used a fragment from yours i.e. using 465 as emtp_port. Good work anywayPrompter
Fatal error: Class 'PHPMailer' not found in C:\xampp\htdocs\myweb\email_script.php on line 82Goldeye
F
9

As in my personal experience I found that very similar thing to Vikas Dwivedi answer will work just fine.

Step 1 (php.ini file)

In php.ini file located in xampp\php\php.ini. Change settings to the following:

 extension=php_openssl.dll
 [mail function]
 sendmail_path =":\xampp7\sendmail\sendmail.exe -t"
 mail.add_x_header=On

Turn off other variables under mail funciton by putting ; before them. e.g ;smtp_port=25

Step 2 (sendmail.ini file)

In sendmail.ini located in xampp\sendmail\semdmail.ini change to the following:

 smtp_server=smtp.gmail.com
 smtp_port=465
 smtp_ssl=auto
 [email protected]
 auth_password=YourPassword

Step 3 (code)

Create a php file and use the following:

 <?php
    mail($to, "subject", "body", "From: ".$from);
 ?>

Notice

  • You need to restart apache in order for php.ini to reload.
  • you need to activate Google Less secure app access in https://myaccount.google.com/u/1/security
  • It might help to run Xampp with Admin permission.
Fibrinous answered 2/2, 2019 at 11:17 Comment(0)
D
4

This code is used for the mail from your localhost XAMPP and your Gmail account. This code is very easy and working for me try your self.

Below Change In php.ini File

SMTP=smtp.gmail.com 
smtp_port=587 
sendmail_from = [email protected] 
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t" 
extension=php_openssl.dll 

Below Change In sendmail.ini File

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log 
[email protected] 
auth_password=your-gmail-password 
[email protected]  

Please write the belove code in your PHP file to send email

<?php 
    $to = "[email protected]";
    $subject = "Test Mail";
    $headers = "From: [email protected]\r\n";
    $headers .= "Reply-To: [email protected]\r\n";
    $headers .= "CC: [email protected]\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
    $message = '<html><body>';
    $message .= '<img src="//css-tricks.com/examples/WebsiteChangeRequestForm/images/wcrf-header.png" alt="Website Change Request" />';
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
    $message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>Details</td></tr>";
    $message .= "<tr><td><strong>Email:</strong> </td><td>Details</td></tr>";
    $message .= "<tr><td><strong>Type of Change:</strong> </td><td>Details</td></tr>";
    $message .= "<tr><td><strong>Urgency:</strong> </td><td>Details</td></tr>";
    $message .= "<tr><td><strong>URL To Change (main):</strong> </td><td>Details</td></tr>";
    $addURLS = 'google.com';
    if (($addURLS) != '') {
        $message .= "<tr><td><strong>URL To Change (additional):</strong> </td><td>" . $addURLS . "</td></tr>";
    }
    $curText = 'dummy text';           
    if (($curText) != '') {
        $message .= "<tr><td><strong>CURRENT Content:</strong> </td><td>" . $curText . "</td></tr>";
    }
    $message .= "<tr><td><strong>NEW Content:</strong> </td><td>New Text</td></tr>";
    $message .= "</table>";
    $message .= "</body></html>";

    if(mail($to,$subject,$message,$headers))
    {
        echo "Mail Send Sucuceed";
    }
    else{
        echo "Mail Send Failed";    
    }
?>
Dogtrot answered 5/3, 2019 at 17:32 Comment(0)
V
4

in addition to all answers please note that in sendmail.ini file:

auth_password=this-is-Not-your-Gmail-password

due to new google security concern, you should follow these steps to make an application password for this purpose:

  1. go to https://accounts.google.com/ in security tab
  2. turn two-step-verification on, using any available option
  3. go back to the security tab (under the same section, where the two-step-verification was originally found) and make an App-password (in select-app drop-down menu, you can choose 'other')

Finally, please note that if you are just using an application, instead of writing the php-code yourself, you may need to set Sendmail as the preferred option.

Running XAMPP as administrator should also solve any file-access issues.

Venusberg answered 22/4, 2020 at 23:43 Comment(1)
Thanks, this was a very crucial partKatricekatrina
D
0

You have to define an SMTP server and a port for this. All except like sending mails from live hosts.

This is a useful link regarding this.

NB: The port should be unused. Please take care that, Some applications like Skype uses the default ports and there by prevents sending mail.

Disabuse answered 12/4, 2013 at 7:25 Comment(1)
You can prevent skype from using port 80. its on connection settings of skypeUgly
A
0

just spent over an hour trying to make this work. for everybody having the same trouble with all the suggestions posted not working: you have to restart Apache in your XAMPP inrerface! just restarting XAMPP wont work!!

Archbishopric answered 2/1, 2014 at 13:46 Comment(1)
I believe this should be posted as a commentPerlite
T
0

I tried many ways to send a mail from XAMPP Localhost, but since XAMPP hasn't SSL Certificate, my email request blocked by Gmail or similar SMTP Service providers.

Then I used MailHog for local smtp server, what you need to do is just run it. localhost:1025 is for smtp server, localhost:8025 is for mail server, where you can check the emails you sent.

here is my code:

    require_once "src/PHPMailer.php";
    require_once "src/SMTP.php";
    require_once "src/Exception.php";

    $mail = new PHPMailer\PHPMailer\PHPMailer();

      //Server settings
    $mail->SMTPDebug = 3;                      // Enable verbose debug output
    $mail->isSMTP();                                            // Send using SMTP
    $mail->Host       = 'localhost';                    // Set the SMTP server to send through
    $mail->Port       = 1025;                                    // TCP port to connect to
    // $mail->Username   = '';                     // SMTP username
    // $mail->Password   = '';                               // SMTP password
    // $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
    // $mail->SMTPSecure = 'tls';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
        echo "Message sent!";
    }

MailHog Github Repository link

Treadway answered 26/2, 2020 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.