Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHPMAILER 2

Status
Not open for further replies.

phpcoder

Programmer
Dec 29, 2008
6
EG
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.


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';




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.';  
  }  
  ?>



Any ideas ?!! Let me know if the configuration is right or not, & how may I solve this problem. Thanks.
 
try adding the red
Code:
 var $Host        = '[COLOR=red]ssl://[/color]smtp.gmail.com';
 
I have changed the host address to


class.phpmailer.php
Code:
var $Hostname          = 'ssl://smtp.gmail.com';

var $Host        = 'ssl://smtp.gmail.com';

The php codes I use for sending emails
Code:
$mail->Host     = "ssl://smtp.gmail.com";

php.ini

Code:
SMTP = ssl://smtp.gmail.com

but that generated the following error

"Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\wamp\ 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\ on line 122
Message was not sent.Mailer error: SMTP Error: Could not connect to SMTP host."




class.smtp
Code:
function Connect($host,$port=0,$tval=30) {
    # 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
 [COLOR=red]line 122  [/color]                $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;
    }
 
are you sure that your isp allows outbound connections on this port?
 
Try this, using an alternate port

includes/phpmailer/class.smtp.php -
set var $SMTP_PORT = 2525;

includes/phpmailer/class.phpmailer.php -
set var $Port =2525;

 
jpadie
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\ 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]: ?!!
 
What comes in mind first is:
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.
 
fsockopen is the internal function that opens a remote socket.

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:
<?php
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";};
?>

 
nemini
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\ 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\ 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.
 
Well, I tried something...may be that can help to determine the problem
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.
 
please use precisely the code that I wrote with the right parameters added. do not use $SMTPSecure etc. (but feel free to turn on the debugging). Whilst $SMTPSecure should work (with changes to Host) when debugging it is always a good idea to start from a code base that is unequivocally known to work.

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).

 
Jpadie: Thank you for this. I am trying to write a script to allow information to be sent over an Outlook Exchange server from a Unix server running PHP5 on Apache2.2

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
 
Drat. Forget to check email notification. This really belongs in a FAQ. It's hard information to find.
 
jlockley,
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.
 
I have done that, and not having much luck. Authentication is tls, but adding that doesn't do anything. Do you happen to know if sherweb permits access? You seem to be familiar with it.

I have found some examples on the php mailer site and others.
 
sherweb will not connect, but usine the same script on host gator succeeds if I send it to the address there. If, however, I send it to my standard address or to my associate's address, I get a message SMTP Error: The following recipients failed: address@aol.com and address@myserver.com
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top