How to configure PHP to send e-mail? - php

I need to send mail to the users of my website using php script. I have tried using mail function in php.
My code is as follows:
$to = "myweb#gmail.com";
$subject = "Test mail";
$message = "My message";
$from = "webp#gmail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
When I try running the program this is what I get:
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().
Please tell me what address to include in the $from variable. Do I need a smtp server for this? How do I send mails using a localhost? Please tell me what exactly to edit in the php.ini file
I am new to all this.. Please help me..

Use PHPMailer instead: https://github.com/PHPMailer/PHPMailer
How to use it:
require('./PHPMailer/class.phpmailer.php');
$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';
$body = 'This is the message';
$mail->IsSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->Username = 'me.sender#gmail.com';
$mail->Password = '123!##';
$mail->SetFrom('me.sender#gmail.com', $name);
$mail->AddReplyTo('no-reply#mycomp.com','no-reply');
$mail->Subject = 'subject';
$mail->MsgHTML($body);
$mail->AddAddress('abc1#gmail.com', 'title1');
$mail->AddAddress('abc2#gmail.com', 'title2'); /* ... */
$mail->AddAttachment($fileName);
$mail->send();

You need to have a smtp service setup in your local machine in order to send emails. There are many available freely just search on google.
If you own a server or VPS upload the script and it will work fine.

You won't be able to send a message through other people mail servers. Check with your host provider how to send emails. Try to send an email from your server without PHP, you can use any email client like Outook. Just after it works, try to configure PHP.ini with your email client SMTP (sending e-mail) configuration.

Here's the link that gives me the answer and we use gmail:
Install the "fake sendmail for windows". If you are not using XAMPP you can download it here: http://glob.com.au/sendmail/sendmail.zip
Modify the php.ini file to use it (commented out the other lines):
mail function
For Win32 only.
SMTP = smtp.gmail.com
smtp_port = 25
For Win32 only.
sendmail_from = <e-mail username>#gmail.com
For Unix only.
You may supply arguments as well (default: sendmail -t -i).
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
(ignore the "Unix only" bit, since we actually are using sendmail)
You then have to configure the "sendmail.ini" file in the directory where sendmail was installed:
sendmail
smtp_server=smtp.gmail.com
smtp_port=25
error_logfile=error.log
debug_logfile=debug.log
auth_username=<username>
auth_password=<password>
force_sender=<e-mail username>#gmail.com

configure your php.ini like this
SMTP = smtp.gmail.com
[mail function]
; XAMPP: Comment out this if you want to work with an SMTP Server like Mercury
; SMTP = smtp.gmail.com
; smtp_port = 465
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster#localhost

To fix this, you must review your PHP.INI, and the mail services setup you have in your server.
But my best advice for you is to forget about the mail() function. It depends on PHP.INI settings, it's configuration is different depending on the platform (Linux or Windows), and it can't handle SMTP authentication, which is a big trouble in current days. Too much headache.
Use "PHP Mailer" instead (https://github.com/PHPMailer/PHPMailer), it's a PHP class available for free, and it can handle almost any SMTP server, internal or external, with or without authentication, it works exactly the same way on Linux and Windows, and it won't depend on PHP.INI settings. It comes with many examples, it's very powerful and easy to use.

This will not work on a local host, but uploaded on a server, this code should do the trick. Just make sure to enter your own email address for the $to line.
<?php
if (isset($_POST['name']) && isset($_POST['email'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$to = 'your.email#address.com';
$subject = "New Message on YourWebsite.com";
$body = '<html>
<body>
<h2>Title</h2>
<br>
<p>Name:<br>'.$name.'</p>
<p>Email:<br>'.$email.'</p>
</body>
</html>';
//headers
$headers = "From: ".$name." <".$email.">\r\n";
$headers = "Reply-To: ".$email."\r\n";
$headers = "MIME-Version: 1.0\r\n";
$headers = "Content-type: text/html; charset=utf-8";
//send
$send = mail($to, $subject, $body, $headers);
if ($send) {
echo '<br>';
echo "Success. Thanks for Your Message.";
} else {
echo 'Error.';
}
}
?>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Your Name"><br>
<input type="text" name="email" placeholder="Your Email"><br>
<button type="submit">Subscribe</button>
</form>
</body>
</html>

Usually a good place to start when you run into problems is the manual. The page on configuring email explains that there's a big difference between the PHP mail command running on MSWindows and on every other operating system; it's a good idea when posting a question to provide relevant information on how that part of your system is configured and what operating system it is running on.
Your PHP is configured to talk to an SMTP server - the default for an MSWindows machine, but either you have no MTA installed or it's blocking connections. While for a commercial website running your own MTA robably comes quite high on the list of things to do, it is not a trivial exercise - you really need to know what you're doing to get one configured and running securely. It would make a lot more sense in your case to use a service configured and managed by someone else.
Since you'll be connecting to a remote MTA using a gmail address, then you should probably use Gmail's server; you will need SMTP authenticaton and probably SSL support - neither of which are supported by the mail() function in PHP. There's a simple example here using swiftmailer with gmail or here's an example using phpmailer

Related

Warning: mail(): Failed to connect to mailserver at "localhost" port 25 [duplicate]

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
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 = my-gmail-id#gmail.com
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
auth_username=my-gmail-id#gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id#gmail.com
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.
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
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 = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>
and you will see this:
I hope you will have a good day.
you can find me on Youtube for more tutorial Piseth Sok
Cheer!
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
Find [mail function] and remove semi colon which is before ;smtp = localhost
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.
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 = "your-gmail-account#gmail.com"; // 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.
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
auth_username=address#gmail.com
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.
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 = your#gmail.com
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
auth_username=yourmail#gmail.com
auth_password=your-gmail-password
force_sender=yourmail#gmail.com
Please write the belove code in your PHP file to send email
<?php
$to = "tomail#gmail.com";
$subject = "Test Mail";
$headers = "From: from_mail#gmail.com\r\n";
$headers .= "Reply-To: replytomail#gmail.com\r\n";
$headers .= "CC: theassassin.edu#gmail.com\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";
}
?>
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:
go to https://accounts.google.com/ in security tab
turn two-step-verification on, using any available option
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.
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.
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!!
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('testtoo#testto.com', 'Mailer');
$mail->addAddress('testtoo#webbamail.com', '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

Testing php mail() locally [duplicate]

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
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 = my-gmail-id#gmail.com
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
auth_username=my-gmail-id#gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id#gmail.com
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.
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
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 = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>
and you will see this:
I hope you will have a good day.
you can find me on Youtube for more tutorial Piseth Sok
Cheer!
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
Find [mail function] and remove semi colon which is before ;smtp = localhost
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.
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 = "your-gmail-account#gmail.com"; // 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.
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
auth_username=address#gmail.com
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.
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 = your#gmail.com
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
auth_username=yourmail#gmail.com
auth_password=your-gmail-password
force_sender=yourmail#gmail.com
Please write the belove code in your PHP file to send email
<?php
$to = "tomail#gmail.com";
$subject = "Test Mail";
$headers = "From: from_mail#gmail.com\r\n";
$headers .= "Reply-To: replytomail#gmail.com\r\n";
$headers .= "CC: theassassin.edu#gmail.com\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";
}
?>
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:
go to https://accounts.google.com/ in security tab
turn two-step-verification on, using any available option
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.
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.
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!!
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('testtoo#testto.com', 'Mailer');
$mail->addAddress('testtoo#webbamail.com', '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

Sending Mail Successfully using PHP mail() but not receiving it [duplicate]

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
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 = my-gmail-id#gmail.com
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
auth_username=my-gmail-id#gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id#gmail.com
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.
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
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 = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>
and you will see this:
I hope you will have a good day.
you can find me on Youtube for more tutorial Piseth Sok
Cheer!
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
Find [mail function] and remove semi colon which is before ;smtp = localhost
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.
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 = "your-gmail-account#gmail.com"; // 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.
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
auth_username=address#gmail.com
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.
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 = your#gmail.com
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
auth_username=yourmail#gmail.com
auth_password=your-gmail-password
force_sender=yourmail#gmail.com
Please write the belove code in your PHP file to send email
<?php
$to = "tomail#gmail.com";
$subject = "Test Mail";
$headers = "From: from_mail#gmail.com\r\n";
$headers .= "Reply-To: replytomail#gmail.com\r\n";
$headers .= "CC: theassassin.edu#gmail.com\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";
}
?>
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:
go to https://accounts.google.com/ in security tab
turn two-step-verification on, using any available option
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.
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.
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!!
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('testtoo#testto.com', 'Mailer');
$mail->addAddress('testtoo#webbamail.com', '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

How to configure XAMPP to send mail from localhost?

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
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 = my-gmail-id#gmail.com
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
auth_username=my-gmail-id#gmail.com
auth_password=my-gmail-password
force_sender=my-gmail-id#gmail.com
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.
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
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 = "somebody#example.com";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: webmaster#example.com" . "\r\n" .
"CC: somebodyelse#example.com";
mail($to,$subject,$txt,$headers);
?>
and you will see this:
I hope you will have a good day.
you can find me on Youtube for more tutorial Piseth Sok
Cheer!
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
Find [mail function] and remove semi colon which is before ;smtp = localhost
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.
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 = "your-gmail-account#gmail.com"; // 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.
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
auth_username=address#gmail.com
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.
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 = your#gmail.com
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
auth_username=yourmail#gmail.com
auth_password=your-gmail-password
force_sender=yourmail#gmail.com
Please write the belove code in your PHP file to send email
<?php
$to = "tomail#gmail.com";
$subject = "Test Mail";
$headers = "From: from_mail#gmail.com\r\n";
$headers .= "Reply-To: replytomail#gmail.com\r\n";
$headers .= "CC: theassassin.edu#gmail.com\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";
}
?>
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:
go to https://accounts.google.com/ in security tab
turn two-step-verification on, using any available option
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.
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.
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!!
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('testtoo#testto.com', 'Mailer');
$mail->addAddress('testtoo#webbamail.com', '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

PHP : send mail in localhost

I would like to send email through php code hosted locally.
<?php
$email = "myemail#local.com";
$titre = "My subject";
$message = "Text message !";
mail($email, $titre, $message);
?>
When I run this code, I get the following error :
Warning: mail() [<a href='function.mail'>function.mail</a>]: 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:\...
I went into the php.ini file and it seems to be already well configured.
[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
How can I fix this ?
Thank you
It is configured to use localhost:25 for the mail server.
The error message says that it can't connect to localhost:25.
Therefore you have two options:
Install / Properly configure an SMTP server on localhost port 25
Change the configuration to point to some other SMTP server that you can connect to
I spent hours on this. I used to not get errors but mails were never sent. Finally I found a solution and I would like to share it.
<?php
include 'nav.php';
/*
Download PhpMailer from the following link:
https://github.com/Synchro/PHPMailer (CLick on Download zip on the right side)
Extract the PHPMailer-master folder into your xampp->htdocs folder
Make changes in the following code and its done :-)
You will receive the mail with the name Root User.
To change the name, go to class.phpmailer.php file in your PHPMailer-master folder,
And change the name here:
public $FromName = 'Root User';
*/
require("PHPMailer-master/PHPMailerAutoload.php"); //or select the proper destination for this file if your page is in some //other folder
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465"); //No further need to edit your configuration files.
$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPSecure = "ssl";
$mail->Username = "trials.php#gmail.com"; //account with which you want to send mail. Or use this account. i dont care :-P
$mail->Password = "trials.php.php"; //this account's password.
$mail->Port = "465";
$mail->isSMTP(); // telling the class to use SMTP
$rec1="trials.php#gmail.com"; //receiver. email addresses to which u want to send the mail.
$mail->AddAddress($rec1);
$mail->Subject = "Eventbook";
$mail->Body = "Hello hi, testing";
$mail->WordWrap = 200;
if(!$mail->Send()) {
echo 'Message was not sent!.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo //Fill in the document.location thing
'<script type="text/javascript">
if(confirm("Your mail has been sent"))
document.location = "/";
</script>';
}
?>
You will need to install a local mailserver in order to do this.
If you want to send it to external e-mail addresses, it might end up in unwanted e-mails or it may not arrive at all.
A good mailserver which I use (I use it on Linux, but it's also available for Windows) is Axigen:
http://www.axigen.com/mail-server/download/
You might need some experience with mailservers to install it, but once it works, you can do anything you want with it.
try this
ini_set("SMTP","aspmx.l.google.com");
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: test#gmail.com" . "\r\n";
mail("email#domain.com","test subject","test body",$headers);
It is possible to send Emails without using any heavy libraries I have included my example here.
lightweight SMTP Email sender for PHP
https://github.com/Nerdtrix/EZMAIL
Tested in both environments production and development.
and most importantly emails will not go to spam unless your IP is blacklisted by the server.
cheers.

Categories