SMTP and PHP problem - php

I have setup WAMP for Windows 64-bit and am trying to use the mail() function
to send an email from my local host to another mail server (for example, Gmail or Hotmail).
The problem is in PHP and SMTP connection. I get the following error message:
Verify your smtp and port 25.
Can anyone tell me what is causing this error and how I can fix it?

When using mail(), PHP expects to find a mailserver running on localhost. You probably don't have that. You can try using PHPMailer instead.
The configuration would then look something like this:
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = 'smtp.example.com';
$mail->Username = 'user';
$mail->Password = 'pass';
$mail->From = 'john.doe#example.com';
$mail->FromName = "John Doe";
$mail->AddAddress('test#gmail.com');
$mail->Subject = 'test subject';
$mail->Body = 'Works?';
$mail->Send()
?>

Related

PHP - Sending mail from XAMPP using PHPMailer [duplicate]

I have been trying to send a mail using php mailer on xampp and i do get this error saying
Message could not be sent. Mailer Error: The following From address failed: xxxx2#gmail.com : Called Mail() without being connected
please, i need help on how to fix this.
Here is my code;
<?php
require( 'class.phpmailer.php' );
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 25;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxxx";
//Sending the actual email
$mail->setFrom('xxxx2#gmail.com', 'Aaron');
$mail->addAddress('xxxx2#gmail.com', 'Aaron'); // Add a recipient
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Calculation form results from ';
$mail->Body = 'testing...';
if(!$mail->send()) {
echo 'Message could not be sent. ';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
Literally copied from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
I tried with Composer and github, but couldn't get the latest version of phpmailer to work with my test php email through xampp. When I ran the program on localhost, it kept crashing out, as the email code was looking for the php mailer class.
So I went back to google, ignored composer, and downloaded a plain ordinary zip of php mailer 5.2.0, and extracted this zip directly into my 'websiteX' testing folder which is located in htdocs in xampp.
In my 'websiteX' folder I have my testmail.php file along with my phpmailer unzipped folder and it contains the class.phpmailer which actually works in my case.
I have spent a week faffing around, but now I have xampp php emails going to my test gmail account perfectly. I use chrome and notepad++ as well.
Funnily enough I had php emails with the php mail() command working too, although Gmail hard bounced 'em, which isnt great. The first thing I did last week was to get Mercury mail (included in xampp) working. I followed this link https://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows and managed to get xampp communicating with gmail which was great!
Good luck with your coding.

how to send mail using phpmailer on xampp

I have been trying to send a mail using php mailer on xampp and i do get this error saying
Message could not be sent. Mailer Error: The following From address failed: xxxx2#gmail.com : Called Mail() without being connected
please, i need help on how to fix this.
Here is my code;
<?php
require( 'class.phpmailer.php' );
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "tls://smtp.gmail.com";
$mail->Port = 25;
$mail->Username = "xxxx#gmail.com";
$mail->Password = "xxxxx";
//Sending the actual email
$mail->setFrom('xxxx2#gmail.com', 'Aaron');
$mail->addAddress('xxxx2#gmail.com', 'Aaron'); // Add a recipient
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Calculation form results from ';
$mail->Body = 'testing...';
if(!$mail->send()) {
echo 'Message could not be sent. ';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
?>
Literally copied from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username#gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";
//Set who the message is to be sent from
$mail->setFrom('from#example.com', 'First Last');
//Set an alternative reply-to address
$mail->addReplyTo('replyto#example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('whoto#example.com', 'John Doe');
I tried with Composer and github, but couldn't get the latest version of phpmailer to work with my test php email through xampp. When I ran the program on localhost, it kept crashing out, as the email code was looking for the php mailer class.
So I went back to google, ignored composer, and downloaded a plain ordinary zip of php mailer 5.2.0, and extracted this zip directly into my 'websiteX' testing folder which is located in htdocs in xampp.
In my 'websiteX' folder I have my testmail.php file along with my phpmailer unzipped folder and it contains the class.phpmailer which actually works in my case.
I have spent a week faffing around, but now I have xampp php emails going to my test gmail account perfectly. I use chrome and notepad++ as well.
Funnily enough I had php emails with the php mail() command working too, although Gmail hard bounced 'em, which isnt great. The first thing I did last week was to get Mercury mail (included in xampp) working. I followed this link https://www.open-emr.org/wiki/index.php/Mercury_Mail_Configuration_in_Windows and managed to get xampp communicating with gmail which was great!
Good luck with your coding.

Sending Mail using PHP : SMTP ERROR

I am trying to send mail using php. But it gave me error,
" SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
"Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting" "
So that I searched alot to find the problem. There I got a solution, that I need to change $mail->IsMail(); from $mail->IsSMTP();
I did it and Mail was sent...
But when I checked my mail,
I got,
"This message may not have been sent by: sender#gmail.com"
Being a developer I understood that An email should not contain such lines or issues.
I want to know, Is it alright if Receiver is showing such line in Email? and if not that What should I do?
I mean what changes I should make in my code.
Here is my php code:
**
date_default_timezone_set('Etc/UTC');
include 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
// $mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "sender#gmail.com";
$mail->Password = 'senderPassword';
$mail->setFrom("sender#gmail.com", 'sender name');
$mail->addReplyTo('sender#gmail.com', '');
$mail->addAddress($receiver, '');
$mail->Subject = 'Welcome';
$mail->Body = 'body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send())
{
return "Mailer Error: " . $mail->ErrorInfo;
}
else
{
return array('flag' => "1");
}
**
isMail and isSMTP use two different sending mechanisms. isMail submits messages through the PHP mail() function, which delivers mail to a local mail server via a sendmail binary. This local mail server then tries to deliver the message to its ultimate recipient. It's possible that this local server will accept a message which is later rejected, and that will be too late for your script to know about.
With isMail:
script -> local mail server -> gmail
With isSMTP:
script -> gmail
With isMail you don't need to authenticate (localhost is usually allowed to relay), and the message is sent from your server to gmail. With isSMTP your message is sent from gmail to gmail, and it does require authentication.
When sending directly through gmail, you need to authenticate with gmail, and that has its own set of problems (that will be why your script is not working) covered thoroughly in the PHPMailer docs, examples and questions here on SO.
When sending via your server, you're saying that you are sending from a gmail user, but it's being sent by your server, not by a server listed in gmail's SPF record. This is forgery, which is why you are seeing the "This message may not have been sent by..." message. It would not say that if you sent from an address in your own domain.
The solution is to fix your gmail authentication and send directly through gmail. Base your code on the gmail example provided with PHPMailer, not the old, obsolete code you're using, and read the docs.
Here is the code which is use for mailing purpose. Try setting the SMTPDebug mode 3 and check the output.
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxxxxxxxxxxx';
$mail->Password = 'xxxxxxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = 'xxxxxxxxxxxxxx';
$mail->FromName = 'xxxxxxxxxxxxxxxxx';
$mail->addAddress(xxxxxxxxxxxxxx);
$mail->addReplyTo('xxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxx');
$mail->isHTML(true);
$mail->Subject = '';
$mail->Body = "";
$mail->send();

PHPMailer: SMTP connect() failed

When I send a request to this, the request times out. This code has worked on another script in the same directory. I just logged into the gmail via browser to make sure all was good. The logged value of $mail->ErrorInfo is: SMTP connect() failed.
Any idea why this might work back in November but now throws an error when I copy it to another script?
$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->CharSet = 'UTF-8';
$mail->Debugoutput = 'html';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Username = "xxxxxx#gmail.com";
$mail->Password = "xxxxxxxxxx";
$mail->setFrom('xxxxxxxxxxx#gmail.com', 'xxxxxxxx');
$mail->addReplyTo('xxxxx#gmail.com', 'xxxxxx');
$mail->addAddress($email, $name);
$mail->Subject = 'Your License Information';
$mail->Body = $message;
I tried another gmail account and I've tried using the app password Gmail gives you.
Since I had it working before in same directory with similar code, I knew it had to be something weird. I tried ping google.com which didn't work and got me thinking about outbound traffic. Then I did ping 24.156.131.93 which is Google's IP (cutting out the domain resolver) and that worked, so I changed my nameservers in resolv.conf to Google's 8.8.8.8 and it now works. So basically, my host has some issues with their nameservers they provided, and the error wasn't just for SMTP but outgoing traffic is a whole.

PHP Send mail error

Please help me I am New in PHP and since last 5 Hours i am try to semd mail and now really tired. Thanks.
Here is my code. I am using Gmail account.
include("class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
$body = $mail->getFile('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP();
$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 = "hussaintalha#gmail.com"; // GMAIL username
$mail->Password = "xxxxxxxx"; // GMAIL password
$mail->AddReplyTo("hussaintalha#gmail.com","First Last");
$mail->From = "hussaintalha#gmail.com.com";
$mail->FromName = "First Last";
$mail->Subject = "PHPMailer Test Subject via gmail";
//$mail->Body = "Hi,<br>This is the HTML BODY<br>"; //HTML Body
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddAddress("hussaintalha#gmail.com", "John Doe");
$mail->AddAttachment("images/phpmailer.gif"); // attachment
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
When I run my file I get This Error:
Warning: fopen(contents.html)
[function.fopen]: failed to open
stream: No such file or directory in
D:\xampplite\htdocs\WebEng\class.phpmailer.php
on line 1870
Warning: fsockopen()
[function.fsockopen]: unable to
connect to ssl://smtp.gmail.com:465
(Unable to find the socket transport
"ssl" - did you forget to enable it
when you configured PHP?) in
D:\xampplite\htdocs\WebEng\class.smtp.php
on line 122 Mailer Error: SMTP Error:
Could not connect to SMTP host.
First of all when you get error messages then that's great! Because in 90% of the cases you find that others have had them too and therefore you'll find plenty of information on the internet about this error message.
So step 1 when getting an error message you don't know yet is always open google and copy paste it there. But, take out any paths or other things which are uniquely connected to your system.
Then about your errors. Especially xampp light doesn't support SSL. Maybe you try an easier sendmail example first. Like a very small one and then increase it step by step.
That's what I always do, when I don't know why something doesn't work. I start with one line and see what it does, then I add another and so forth.
Let's say you start with this and see if it works:
<?php
include("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "hussaintalha#gmail.com"; // GMAIL username
$mail->Password = "xxxxxxxx"; // GMAIL password
$mail->From = "hussaintalha#gmail.com";
$mail->Subject = "PHPMailer Test Subject via gmail";
$mail->Body = "Hi, this is a test";
$mail->AddAddress("hussaintalha#gmail.com", "Hussain");
$mail->send();
?>
oh, and btw. your mail from has a .com too many!
You PHP installation (XAMPP, by the looks of it) does not support SSL. Ensure that the line
extension=php_openssl.dll
is not commented out in your php.ini, restart Apache, and if that still doesn't work try overwriting (or copying) ssleay32.dll and libeay32.dll from your PHP directory into Apache's binary (.exe) directory then restart Apache.

Categories