PHPMAILER
PHPMAILER
(OP)
I had some problems sending php emails using servers that require authentication like google or yahoo. I was told that PHPMAILER is the best solution to my problem. Well, I have installed it right (I think so because it I tried the installation test & there were no errors), configured the class.phpgmailer.php & tried to send an email through php codes but I always get this error "Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host."
I tried to use smtp.gmail.com or smtp.mail.yahoo.com but it's useless..I always get the same error. I thought I should show you the class.phpgmailer.php & the php codes I use to send the email, maybe somebody can tell me how to solve such a problem. I included only the important lines in class.phpgmailer.php file.
The php codes I use for sending emails
Any ideas ?!! Let me know if the configuration is right or not, & how may I solve this problem. Thanks.
I tried to use smtp.gmail.com or smtp.mail.yahoo.com but it's useless..I always get the same error. I thought I should show you the class.phpgmailer.php & the php codes I use to send the email, maybe somebody can tell me how to solve such a problem. I included only the important lines in class.phpgmailer.php file.
CODE
<?php
class PHPMailer {
var $Priority = 3;
var $CharSet = 'iso-8859-1';
var $ContentType = 'text/plain';
var $Encoding = '8bit';
var $ErrorInfo = '';
var $From = 'my_user_name@gmail.com';
var $FromName = 'my_user_name@gmail.com';
var $Sender = 'my_user_name@gmail.com';
var $Subject = '';
var $Body = '';
var $AltBody = '';
var $WordWrap = 999999;
var $Mailer = 'smtp';
var $Sendmail = '/usr/sbin/sendmail';
var $PluginDir = '';
var $Version = "2.0.3";
var $ConfirmReadingTo = '';
var $Hostname = 'smtp.gmail.com';
var $MessageID = '';
var $Host = 'smtp.gmail.com';
var $Port = 465;
var $Helo = '';
var $SMTPSecure = "ssl";
var $SMTPAuth = true;
var $Username = 'my_user_name@gmail.com';
var $Password = 'my_password';
class PHPMailer {
var $Priority = 3;
var $CharSet = 'iso-8859-1';
var $ContentType = 'text/plain';
var $Encoding = '8bit';
var $ErrorInfo = '';
var $From = 'my_user_name@gmail.com';
var $FromName = 'my_user_name@gmail.com';
var $Sender = 'my_user_name@gmail.com';
var $Subject = '';
var $Body = '';
var $AltBody = '';
var $WordWrap = 999999;
var $Mailer = 'smtp';
var $Sendmail = '/usr/sbin/sendmail';
var $PluginDir = '';
var $Version = "2.0.3";
var $ConfirmReadingTo = '';
var $Hostname = 'smtp.gmail.com';
var $MessageID = '';
var $Host = 'smtp.gmail.com';
var $Port = 465;
var $Helo = '';
var $SMTPSecure = "ssl";
var $SMTPAuth = true;
var $Username = 'my_user_name@gmail.com';
var $Password = 'my_password';
The php codes I use for sending emails
CODE
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->From = "my_user_name@gmail.com";
$mail->AddAddress("my_user_name@gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->From = "my_user_name@gmail.com";
$mail->AddAddress("my_user_name@gmail.com");
$mail->Subject = "First PHPMailer Message";
$mail->Body = "Hi! \n\n This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
if(!$mail->Send()) {
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent.';
}
?>
Any ideas ?!! Let me know if the configuration is right or not, & how may I solve this problem. Thanks.
RE: PHPMAILER
CODE
RE: PHPMAILER
class.phpmailer.php
CODE
var $Host = 'ssl://smtp.gmail.com';
The php codes I use for sending emails
CODE
php.ini
CODE
but that generated the following error
"Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\wamp\www\a3\class.smtp.php on line 122
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://ssl://smtp.gmail.com:465 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in E:\wamp\www\a3\class.smtp.php on line 122
Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host."
class.smtp
CODE
# set the error val to null so there is no confusion
$this->error = null;
# make sure we are __not__ connected
if($this->connected()) {
# ok we are connected! what should we do?
# for now we will just give an error saying we
# are already connected
$this->error = array("error" => "Already connected to a server");
return false;
}
if(empty($port)) {
$port = $this->SMTP_PORT;
}
#connect to the smtp server
$this->smtp_conn = fsockopen($host, # the host of the server
$port, # the port to use
$errno, # error number if any
$errstr, # error message if any
line 122 $tval); # give up after ? secs
# verify we connected properly
if(empty($this->smtp_conn)) {
$this->error = array("error" => "Failed to connect to server",
"errno" => $errno,
"errstr" => $errstr);
if($this->do_debug >= 1) {
echo "SMTP -> ERROR: " . $this->error["error"] .
": $errstr ($errno)" . $this->CRLF;
}
return false;
}
RE: PHPMAILER
RE: PHPMAILER
includes/phpmailer/class.smtp.php -
set var $SMTP_PORT = 2525;
includes/phpmailer/class.phpmailer.php -
set var $Port =2525;
RE: PHPMAILER
Yeah all are open...my ISP do not block any ports. I can send emails using Outlook Express using port 465.
nemini
tried your solution... I got the that error
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://smtp.gmail.com:2525 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ) in E:\wamp\www\a3\class.smtp.php on line 122
Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.
By the way, when I removed ssl from the php mail codes & php.ini file & kept them just in class.phpmailer.php...the error was reduced to
Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host.
what's fsockopen() [function.fsockopen]: ?!!
RE: PHPMAILER
Is the server setup to allow for outside communications, or does not accept mail from the IP you try to access.
Are you running fedora? if so
add to the Kernel command line selinux=0
BTW is fsockopen enabled on your account?
I did a search and that pretty much sums it.
RE: PHPMAILER
I see that you are running windows, so nemini's suggestions on Fedora are unlikely to be useful in your case (but will help others). Are you running any firewall application? if so you must make sure that the php process has the permissions to get out to port 465. try ruling out the firewall first by turning it off completely.
I wrote this inheriting class which I have tested on my MAMP installation and can confirm that it works perfectly. i have also tested on a windows computer and can also confirm that it works on that platform.
CODE
require("phpmailer/class.phpmailer.php");
class gMailer extends PHPMailer {
/** CHANGE THESE AS REQUIRED **/
public $From = "myAddress@gmail.com"; //PUT YOUR ADDRESS HERE
public $FromName = "MY NAME"; //PUT YOUR NAME HERE
public $Password = ""; //PUT YOUR PASSWORD HERE
/** DO NOT CHANGE THESE **/
public $Host = "ssl://smtp.gmail.com";
public $Mailer = "smtp";
public $WordWrap = 75;
public $Port = "465";
public $SMTPAuth = true;
public function __construct($debug = false){
$this->Sender = $this->From;
$this->Username = $this->From;
$this->SMTPDebug = $debug;
}
}
$mail = new gMailer(false); //change to true if you are having trouble
$mail->AddAddress('email@domain.com', 'addressee name');
$mail->Body = "This is a test message";
$mail->Subject = "Test Message";
if ($mail->send()) {echo "mail sent";};
?>
RE: PHPMAILER
I'm trying it on a localhost machine using gmail server.
I'm not using fedora..I'm using windows xp sp2
I have no idea if fsockopen is enabled or not...how can I check it ?!
jpadie
My firewall doesn't block any ports...I checked the ports...I can send emails using Outlook Express & I turned it off before & nothing changed.
Well, I tried your codes...actualy at the beginng, it gave me an error saying
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\wamp\www\a3\class.smtp.php on line 122
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://ssl://smtp.gmail.com:465 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in E:\wamp\www\a3\class.smtp.php on line 122
SMTP -> ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: No such host is known. (0)
then I changed a line in your code from
public $Host = "ssl://smtp.gmail.com";
to
public $Host = "smtp.gmail.com";
I didn't receive any errors but I didn't receive mail sent too. I checked the email...found nothing....so I changed
$mail = new gMailer(false);
to
$mail = new gMailer(true);
I received the following error
SMTP -> FROM SERVER: SMTP -> FROM SERVER: SMTP -> ERROR: EHLO not accepted from server: SMTP -> FROM SERVER: SMTP -> ERROR: HELO not accepted from server: SMTP -> ERROR: AUTH not accepted from server: SMTP -> FROM SERVER: SMTP -> ERROR: RSET failed:
btw..sorry for the late response.
RE: PHPMAILER
in class.phpmailer.php
I changed var $SMTPSecure = "";
from var $SMTPSecure = "ssl";
to var $SMTPSecure = "";
After I did that change...I received no errors but still didn't get nail sent & didn't receive the mail.
RE: PHPMAILER
assuming this does not work, however we are now left with three alternatives, in my opinion (as we know that fsockopen is working adequately):
1. you have a public IP address that gmail is actively blocking. check the various black lists for your IP address. I do not know whether gmail does this.
2. somewhere along the line the socket is being proxied/firewalled or otherwise blocked.
3. it is all working but your email is either being delayed or caught by a spam trap. send the email to yourself, whitelist your incoming address, check the sent items in gmail (if the outgoing mail is there then you know it has been sent properly and the problem is on the receiving side).
RE: PHPMAILER
RE: PHPMAILER
The above code looks to be exactly what I need. Am I (new at this and nearly cross eyed from reading up on it) correct in assuming that unlike in Apache installation under Windows32 there is no PHP.ini configuration for this, but that all of the information resides in the code itself?
Thank you
RE: PHPMAILER
RE: PHPMAILER
the above code does not use the windows smtp stack, so no php.ini changes are required.
although the above code is designed for gmail, not exchange you should be able to get it working with any smtp server.
check out the phpmailer site for more examples.
RE: PHPMAILER
I have found some examples on the php mailer site and others.
RE: PHPMAILER
I am going to try the solution on the phpmailer site (essentially the first form I posted above) for the second server. We have only used this as a backup so far, but it there is a way to make it work, we will probably just move everything over there.