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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SMTP send email with Gmail 1

Status
Not open for further replies.

rca63

Programmer
Jun 26, 2011
12
HK
thread434-1521422

May I ask your help, my notebook computer as server with XP window, do I need other software to send email, by setting
the php.ini smtp=local host smtp port = 25. After running pgm,
I have the follwing
SMTP Error: Could not connect to SMTP host. Mailer Error: SMTP Error: Could not connect to SMTP host.

Here is my program . . .
<?php
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->Host = "ssl://smtp.gmail.com::465";
$mail->SMTPSecure = "ssl";
$mail->IsSMTP(); // send via SMTP

//IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "c238@gmail.com"; // SMTP username
$mail->Password = "my_password"; // SMTP password
$webmaster_email = "c238@gmail.com"; //Reply to this email ID
$email="c238@gmail.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>

 
change this
Code:
$mail->Host = "ssl://smtp.gmail.com::465";

to
Code:
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';   //or try 'ssl'

and make sure your phpmailer is up to date
 
Thank you jpadie, I made the changes and see some improvement. I do not see the "SMTP Error: Could not connect to SMTP host.", but
it takes a long time to run (about 90 sec.) I do NOT see the echo
message "mail error" or "message has been sent", no Error info.
I check my Gmail account, no messages.

Can you teach me to put a trace to the script, so as to know how
the execution is going, if there is a loop running. How do I know the phpmailer is up to date? Thanks again
 
I suspect that you have mistyped something that is, in turn, creating a syntax error and causing the script to fail on start up.

try turning on display_errors and setting error_reporting to E_ALL in php.ini. You cannot set these in the script if you want to capture syntax errors.

remember to restart the webserver after making changes to php.ini
 
Thank you and sorry to border you again, I checked my php.ini
the display_errors is on, error_reporting to E_ALL, to restart
the webserver by running Apache Restart. I simplify my script.

<?php
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
$mail->IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "c238@gmail.com"; // SMTP username
$mail->Password = "my_passw"; // SMTP password
$mail->From = "c238@gmail.com";
$mail->FromName = "Webtester";

$mail->AddAddress("p238@yahoo.com", "Mike");
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi, This is the HTML BODY "; //HTML Body
echo " aaa2 test2 ";
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
exit;
if(!$mail->Send()){echo "Mailer Error: " . $mail->ErrorInfo;} else {echo "Message sent";}
?>

Testig result is the same as before, it execute with no parse errors or warning. It takes long time (about 90 sec). I put an exit before the if (...),it take 2 sec to run. No email sent,
no messages, I check my gmail acct, nothing. Thanks . . .
 
could you try this please
Code:
<?php

function logger($message){
	echo '<pre>';	
	if(is_object($message)):
		$message = get_object_vars($message);
	endif;
	if(is_array($message)):
		print_r($message);
	else:
		echo $message;
	endif;
	echo '<pre>';
}

require "phpmailer/class.phpmailer.php";
logger('retrieved mailer class');
$mail = new PHPMailer();
logger('instantiated php mailer');
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
$mail->IsSMTP(); // send via SMTP
logger('set phpmailer to SMTP');
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "c238@gmail.com"; // SMTP username
$mail->Password = "my_passw"; // SMTP password
$mail->From = "c238@gmail.com";
$mail->FromName = "Webtester";
$mail->AddAddress("p238@yahoo.com", "Mike");
logger('added address to mail object');
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi, This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
logger('about to send message');
if(!$mail->Send()):
	logger("Error sending mail \n" . $mail->ErrorInfo);
	logger("trying alternative port");
	$mail->Port = 587;
	if(!$mail->Send()):
		logger("Error sending mail with alternative port \n" . $mail->ErrorInfo);
	else:
		logger('Successfully sent mail via alternative port');
	endif;
else:
	logger('successfully sent email');
endif;
?>
 
Thank you for your reply, I tried, the result is same as before.
I can see the first 5 logger messages, but not the ones after
the if(!$mail->Send()):
The run time is long, about 70 sec. I put an exit before the if(!$mail->Send(), it takes 2 sec, I checked my gmail and yahoo acct no mail send/received.

Thank you very much.
 
Please add this to the start of the script
Code:
ini_set('display_errors', true); error_reporting(E_ALL);

Run the code and then please paste back the log output.

Please also let us know what os you are running ( and version ).
 
Yes, I put the ini_set('display_errors', true); error_reporting(E_ALL); I run the script, the same thing, exactly the same as before, time about 70 sec, no log output seen. My notebook computer use the XP window. AppServ V. 2.5.8 for window, Apache
web server V.2.2.4, PHP script language V.5.2.1, MYSQL database V.5.0.27

By the way do I need to install the software for openSSL? Thanks
 
You should not need OpenSSL. Appropriate implementations should already be present in your OS. You might need to enable the php extensions though. Check that the relevant lines are uncommented in your php.ini file. Make sure you restart the web server after. Make sure you have no firewall apps installed. Or if you do make sure that you are 100% allowing php out via port 587 465 and 25.

The fact that you are not receiving any error message is troubling. Do check the error console for windoze and also the error logs for php (for location check php.ini)
 
Here is my php.ini, I may not set it right, Thanks
[mail function]
; For Win32 only.
SMTP = smtp.netvigator.com
smtp_port = 25

; For Win32 only.
sendmail_from = c238@gmail.com


;extension=php_mbstring.dll
;extension=php_bz2.dll
;extension=php_curl.dll
;extension=php_dba.dll
extension=php_dbase.dll
;extension=php_exif.dll
;extension=php_fdf.dll
;extension=php_filepro.dll
extension=php_gd2.dll
;extension=php_gettext.dll
;extension=php_ifx.dll
;extension=php_imap.dll
;extension=php_interbase.dll
;extension=php_ldap.dll
extension=php_mbstring.dll
;extension=php_mcrypt.dll
;extension=php_mhash.dll
;extension=php_mime_magic.dll
;extension=php_ming.dll
;extension=php_mssql.dll
;extension=php_msql.dll
extension=php_mysql.dll
;extension=php_oci8.dll
extension=php_openssl.dll
;extension=php_oracle.dll
;extension=php_pgsql.dll
;extension=php_shmop.dll
;extension=php_snmp.dll
extension=php_sockets.dll
;extension=php_sqlite.dll
;extension=php_sybase_ct.dll
;extension=php_tidy.dll
;extension=php_xmlrpc.dll
;extension=php_xsl.dll
;extension=php_pdo.dll
;extension=php_pdo_sqlite.dll
;extension=php_winbinder.dll
 
the php.ini file looks ok.
i assume you have checked that there are no firewall apps installed?
what errors are in the php logs? please copy and paste them here.
can you confirm that you are using the latest copy of phpmailer?
also check with your ISP that they allow outbound smtp connections to anything other than their servers (many do not)
the issue feels like a timeout caused by your isp just failing the packets deliberately. in this circumstance the socket never gets a response so just carries on going. windows does not support socket_set_timeout so no fix there. effectively you are waiting for the webserver to give up.

try this code

Code:
<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

function logger($message){
    echo '<pre>';    
    if(is_object($message)):
        $message = get_object_vars($message);
    endif;
    if(is_array($message)):
        print_r($message);
    else:
        echo $message;
    endif;
    echo '<pre>';
}

require_once "phpmailer/class.phpmailer.php";
logger('retrieved mailer class');

$mail = new PHPMailer();
logger('instantiated php mailer');

$mail->IsSMTP(); // send via SMTP
$mail->SMTPDebug = 5;
logger('set phpmailer to SMTP');

$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';

$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "c238@gmail.com"; // SMTP username
$mail->Password = "my_passw"; // SMTP password

$mail->From = "c238@gmail.com";
$mail->FromName = "Webtester";
$mail->AddAddress("p238@yahoo.com", "Mike");
logger('added address to mail object');

$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi, This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body


logger('about to send message');

try{
	
	$mail->Send();
	logger('mail sent successfully over tls');
	
} catch ( phpmailerException $e ) {
	
    logger("Error sending mail \n" . $e->errormessage());
    logger("trying alternative port");
    
    try{
    	$mail->Port = 465;
    	$mail->SMTPSecure = 'ssl';
    	$mail->send();
    	logger('Mail sent successfully via alternative port');
    
	} catch (phpmailerException $e ){
    	logger("Error sending mail with alternative port \n" . $e->errorMessage());
    } catch (Exception $e) {
		logger( $e->getMessage());
	}
} catch (Exception $e) {	
	logger( $e->getMessage());
}
?>
 
How do I know I am using the latest version of phpmailer? After
I run the above code, I have these . . .

retrieved mailer classinstantiated php mailerset phpmailer to SMTPadded address to mail objectabout to send messageSMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "220 mx.google.com ESMTP x2sm1528025pbn.13
"
SMTP -> get_lines(): $data is "220 mx.google.com ESMTP x2sm1528025pbn.13
"
SMTP -> FROM SERVER:220 mx.google.com ESMTP x2sm1528025pbn.13

SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "250-mx.google.com at your service, [218.102.107.200]
"
SMTP -> get_lines(): $data is "250-mx.google.com at your service, [218.102.107.200]
"
SMTP -> get_lines(): $data was "250-mx.google.com at your service, [218.102.107.200]
"
SMTP -> get_lines(): $str is "250-SIZE 35882577
"
SMTP -> get_lines(): $data is "250-mx.google.com at your service, [218.102.107.200]
250-SIZE 35882577
"
SMTP -> get_lines(): $data was "250-mx.google.com at your service, [218.102.107.200]
250-SIZE 35882577
"
SMTP -> get_lines(): $str is "250-8BITMIME
"
SMTP -> get_lines(): $data is "250-mx.google.com at your service, [218.102.107.200]
250-SIZE 35882577
250-8BITMIME
"
SMTP -> get_lines(): $data was "250-mx.google.com at your service, [218.102.107.200]
250-SIZE 35882577
250-8BITMIME
"
SMTP -> get_lines(): $str is "250-STARTTLS
"
SMTP -> get_lines(): $data is "250-mx.google.com at your service, [218.102.107.200]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
"
SMTP -> get_lines(): $data was "250-mx.google.com at your service, [218.102.107.200]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
"
SMTP -> get_lines(): $str is "250 ENHANCEDSTATUSCODES
"
SMTP -> get_lines(): $data is "250-mx.google.com at your service, [218.102.107.200]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
"
SMTP -> FROM SERVER: 250-mx.google.com at your service, [218.102.107.200]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES

SMTP -> get_lines(): $data was ""
SMTP -> get_lines(): $str is "220 2.0.0 Ready to start TLS
"
SMTP -> get_lines(): $data is "220 2.0.0 Ready to start TLS
"
SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS


Warning: stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in C:\AppServ\ on line 199
 
Looks like you have not correctly installed OpenSSL. Make sure that the library extension is actually present in the right directory.
 
also I don't think you are using the latest version of phpmailer. the line numbers don't match up.

download the latest version from sourceforge.

latest version is 5.1
 
Do I install the OpenSSL correctly? Is it in the right directory?
I do a search on my hard drive, looking for files of "php_openssl.dll, I find 2 locations,
(1)C:\Documents and Settings\Venus\my documents\appserv\Php5\ext
(2)C:\AppServ\Php5\ext

Now about the version of phpmailer, I have it a month ago, I think I got version 5.1, as seen from copy
/*~ class.phpmailer.php
.---------------------------------------------------------------------------.
| Software: PHPMailer - PHP email class |
| Version: 5.1 |
| Contact: via sourceforge.net support pages
 
Well make sure that the dll is accessible to php and windows. Read the manual on OpenSSL for some peculiarities with installation on windows
 
Thank you for your reply, I am afraid to open the Openssl file and make changes, they give a warning. Where can I read the manual on OpenSSL, how to make sure that the dll is accessible to php and windows?

I find 2 locations of php_openssl.dll (as my last message (1)/(2)
in 2 Appserv folders, should I delete (1)?

Any problem with my phpmailer? thanks
 
i have no idea what appserv might be.

the php manual can be found here:
there is no requirement to edit openssl.dll or anything. just follow the instructions
 
May I ask you to take a look, if I am going to the right place.
To install the openssl, I go to fr2.php.net/openssl . .

then I go to

Description Download Size Last change Md5sum
? Complete package, except sources Setup 4658384 4 December 2008 9d9e1c90bb4976a554f604e9e69ac0a0

I just do the above setup using the wiz. step by step, but
? Sources Setup
? Binaries Zip
? Developer files Zip
? Documentation Zip
? Sources Zip
? Original source those 6 items I do not touch. . .

How to make sure that the dll is accessible to php and windows?
After Setup, the openssl file was in C:\program files\Gnuwin32,
just 2 pdf files, no exe files. I need your advice, thanks . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top