php mail function Failed to connect to mail server at - php

i will send mail using php mail function but it can display some error..
Warning: mail(): Failed to connect to mail server at
"localhost" port 25, verify your "SMTP" and
"smtp_port" setting in php.ini or use ini_set() in
mail.php
<?PHP
$sender = 'sender123#gmail.com';
$recipient = 'resever123#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
?>

If you are testing on your localhost you most likely dont have an SMTP setup. You have to setup an smtp connection for php to send the message.
I would suggest using something like phpmailer which makes it easier when working on local testing servers.

Related

PHP: Why my Mail() Functions are not working?

Hi I am trying to test my PHP mail function but it is not working. I have shared hosting all of my website mail function are not working
<?PHP
$sender = 'xx#gmail.com';
$recipient = 'xx#gmail.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
?>
I have also checked my php.ini if mail function is disabled but it is not disabled
disable_functions =
I also tried using Easy WP SMTP Plugin. But it is not working. I am sure that my settings are correct.
But this is what i got
SMTP ERROR: Failed to connect to server: Connection timed out (110)SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Thank you

issues while sending email with xampp on localhost

I am working on a project in which I need to send email. My send mail function is:
ini_set('sendmail_path', "\"C:\xampp\sendmail\sendmail.exe\" -t");
ini_set('smtp_server', 'smtp.gmail.com');
ini_set('smtp_port', 25);
ini_set('smtp_ssl', 'auto');
ini_set('error_logfile', 'error.log');
ini_set('auth_username', 'myemailAddreds#gmail.com');
ini_set('auth_password', 'mygmail_password');
//sendmail_from('myemailAddreds#gmail.com');
$to = 'myemailAddreds#gmail.com';
$subject = 'Hello from XAMPP!';
$message = 'This is a test';
$headers = "From: your#email-address.com\r\n";
if (mail($to, $subject, $message, $headers)) {
echo "SUCCESS";
} else {
echo "ERROR";
}
But I am getting following error
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:\umtab\xampp\htdocs\umtab\email.php on line 23
As you can see from the error: you get Failed to connect to mailserver at "localhost" port 25. It's written localhost, like the server is not properly set.
I think that ini_set('smtp_server', 'smtp.gmail.com'); is not working.
Try to use ini_set('SMTP','smtp.gmail.com'); instead ... that should be the correct way to set the SMTP server!
But anyway, as suggested by #ADyson, it would be better using PHPMailer. You can read the documentation and the usage right here.
Andrea

Send email with PHP/IIS

We're struggling with email submission via PHP. Currently we're using the following code:
<?php
ini_set('display_errors',1);
ini_set("SMTP","smtp.gmail.com");
ini_set("smtp_port","587");
ini_set("auth_username","mymail_address#gmail.com");
ini_set("auth_password","mypassword");
$to = 'mydestination_address#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail_address#gmail.com';
if(mail($to, $subject, $message, $headers))
{
echo "Success!";
}else
{
echo "Failed.";
}
?>
The outcome of the code gives the following message:
Warning: mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS
command first. e22sm16110599edu.35 - gsmtp in
C:\inetpub\production\mailtest.php on line 13 failed.
Please note that our application has been created with PHP using IIS.
I think you can do it with:
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
Please check if your PHP installation has SSL support before use the code above.

Warning: mail(): SMTP server response: 550 No such user here in C:\home\site\wwwroot\db_resp3.php on line 19

I am trying to write a simple php script to send an email. Here is my code:
<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
ini_set('sendmail_from', 'mail#leslie4stateassembly.com');
ini_set('SMTP', 'mail.leslie4stateassembly.com');
ini_set('smtp_port', 25);
$recipient= "to2240#columbia.edu";
$subject= "Example Email";
$email= "Is this message gonna send!?!?";
$headers= "From:mail#leslie4stateassembly.com";
$result = mail($recipient, $subject, $email, $headers);
if ($result == false){
echo "didn't send";
}
else {
echo "sent";
}
echo phpinfo();
?>
I'm using the GearHost.com mail servers. For some reason, I keep getting this response:
Warning: mail(): SMTP server response: 550 No such user here in C:\home\site\wwwroot\db_resp3.php on l
(for the record, I know that the recipient email address is correct.)
Please help!

unable to send mail via php

I am trying to send mail using php.And i am using WampServer.
so i tried the following code
ini_set("SMTP","smtp.gmail.com" );
ini_set("smtp_port","465");
ini_set('sendmail_from', 'person1#gmail.com');
$to = "person2#gmail.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "person1#gmail.com";
$headers = "From:" . $from;
$retval = mail($to,$subject,$message,$headers);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
but it take more time to connect and says could not connect with localhost.
Please help me in solving the problem
try this configuration:
http://blog.techwheels.net/send-email-from-localhost-wamp-server-using-sendmail/
this might help.
somehow, instead of "smtp.gmail.com", for me it works with "ssl:smtp.gmail.com"
This line:
ini_set("SMTP","smtp.gmail.com" );
should be
ini_set("SMTP","ssl:smtp.gmail.com" );
Also, see this response to a similar question: Send email using the GMail SMTP server from a PHP page
You're trying to send mail from your localhost (Your PC) I guess It's not setup to send mail. Move the script to a production server and it will work

Categories