×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

SMTP send email with Gmail

SMTP send email with Gmail

SMTP send email with Gmail

(OP)
thread434-1521422: PHPMAILER

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

 

RE: SMTP send email with Gmail

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  

RE: SMTP send email with Gmail

(OP)
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

RE: SMTP send email with Gmail

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

RE: SMTP send email with Gmail

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

RE: SMTP send email with Gmail

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

RE: SMTP send email with Gmail

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

RE: SMTP send email with Gmail

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

RE: SMTP send email with Gmail

(OP)
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

RE: SMTP send email with Gmail

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)

RE: SMTP send email with Gmail

(OP)
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
 

RE: SMTP send email with Gmail

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());
}
?>

RE: SMTP send email with Gmail

(OP)
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\www\phptest2\testing2\phpmailer\class.smtp.php on line 199

 

RE: SMTP send email with Gmail

Looks like you have not correctly installed OpenSSL.  Make sure that the library extension is actually present in the right directory.  

RE: SMTP send email with Gmail

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

RE: SMTP send email with Gmail

(OP)
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      

RE: SMTP send email with Gmail

Well make sure that the dll is accessible to php and windows. Read the manual on OpenSSL for some peculiarities with installation on windows

RE: SMTP send email with Gmail

(OP)
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

RE: SMTP send email with Gmail

i have no idea what appserv might be.  

the php manual can be found here: http://fr2.php.net/openssl

there is no requirement to edit openssl.dll or anything.  just follow the instructions

RE: SMTP send email with Gmail

(OP)
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
http://sourceforge.net/projects/gnuwin32/files/openssl/0.9.8h-1/openssl-0.9.8h-1-setup.exe/download

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    http://www.openssl.org/source/openssl-0.9.8h.tar.gz
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 . .

 

RE: SMTP send email with Gmail

Unless you are signing certificates there is no need to install OpenSSL binaries. I do not think you have read the php manual carefully enough.

You need to work out which php instance you are using.  Whether it is via appserv (whatever that is) or native php. Since I have no idea what appserv is I can boy recommend doing the normal thing and installing apache mysql and php separately and manually (ie not using the automated installers) . I have never had any problems that route.

The only gotcha for windows is ensuring that certain dlls are available in the windows path environment variable. The link for this is http://fr.php.net/manual/en/faq.installation.php#faq.installation.addtopath.  

Essentially you can either edit the path variable or just copy the dll to windows/system32. The former is better from a tidiness perspective.  

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close