Viewed   135 times

i am trying to use phpMailer to send confirmation messages to users via email. my code is this:

<?php
include("class.phpmailer.php");
include("class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "sender@gmail.com"; // your SMTP username or your gmail username
$mail->Password = "mypasswrord"; // your SMTP password or your gmail password
$from = "webmaster@example.com"; // Reply to this email
$to="receiver@yahoo.com"; // Recipients email ID
$name="Jersey Name"; // Recipient's name
$mail->From = $from;
$mail->FromName = "Webmaster"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Sending Email From Php Using Gmail";
$mail->Body = "This Email Send through phpmailer, 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";
}
?>

i already enabled ssl in php.ini.

PS> sender@gmail.com is a mask email to protect the privacy. but i did put a true email address in that part

 Answers

3

in you php.ini make sure you have uncommented the line with

extension=php_openssl.dll
Friday, November 11, 2022
3

The difference between the prefixing the hostname with ssl:// and without it is whether or not the underlying stream will be wrapped through OpenSSL, or speak in plain text.

When you connect to Gmail on port 465, it expects the client will use TLS encryption. Most likely, the error message you were seeing was a general result of connecting to a service expecting an encrypted connection when it was just trying to write and read data in plain text.

PHP does magic for you when you use the ssl:// wrapper to connect to a service that supports TLS or SSL. It allows you to read and write on the stream (using say fread/fwrite) in the same manner you would on an unencrypted connection and all the handshaking, encryption, and decryption is done in the background having to do no more than prefix the host with the ssl wrapper.

As to the second question, it is most likely security related. In order to relay (send mail to another domain) you need to authenticate over SMTP which should NEVER be done in cleartext while you can connect on port 25 using an unencrypted connection and send mail to a Gmail user without authenticating (this is what most outside mail servers do when one of their users which to send mail to Gmail). But technologically, there's nothing preventing them from allowing you to send mail using an unencrypted connection, or even authenticating with Gmail credentials (this is called an open relay and is usually badly abused by spammers).

You can learn more about your first question by just reading about the SMTP protocol, the STARTTLS command, and TLS encryption in general. STARTTLS allows a client to connect to the mail server over an unencrypted connection and then negotiate (upgrade) the connection to use encryption, where on the other hand, connections to port 465 expect a TLS handshake to occur as soon as the connection is established and before any protocol (SMTP) communication occurs.

Tuesday, August 2, 2022
 
5

It sounds like your web host is blocking outbound connections to smtp.gmail.com:465. Suggestions:

  1. Verify: If you have shell/terminal access to your web hosting server, try a telnet test to verify that they are in fact blocking this. Run telnet smtp.gmail.com 465

  2. Contact: Call or email your hosting provider and find out what SMTP server they provide for outbound relay. Make sure they know you want to use your @gmail.com address as the From/Reply-to address.

  3. Update code: Once your host provides you with a different mail server, update your code and try again.

If your web host doesn't allow outbound relay from its servers at all, then you need to look at switching hosts, if this is a requirement for your application.

Saturday, August 27, 2022
 
4

Gmail re-writes the headers on messages sent through smtp.gmail.com, replacing the FROM address with the gmail address associated with the gmail account that you are using to send the message through. For more info, and a possible workaround, see: http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server

Tuesday, October 18, 2022
2

Is SMTP set up on ? And if so is it configured to listen to smtp..com on port 587? If the server is not set up by yourself it's not uncommon that they listen to mail..com instead. Also, try to connect to port 25 to see if it might be configured to listen to the default smtp port.

The error messages is in any case very clear. The host is not responding to your connection attempt. The reason could be missconfigurations in both the server and in PHP, firewall issues, routing issues, DNS issues etc.

Tuesday, November 15, 2022
 
hmmmmm
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :