Send mail via phpmailer using gmail account "Turn"

Source: Internet
Author: User
Tags explode php language gmail mail ssl connection
163 Mailbox
POP3 Server:
SMTP Server:
Yahoo Mail
Note: Yahoo in foxmail more than 4.1 of the version set as follows: (Outlook not yo ~)
POP3 Server:
SMTP server:
Sohu Mailbox
POP3 Server:
SMTP server:
Gmail Mail
The first step: To manually configure: POP3 server is
SMTP Server is ssl://
Step Two: Use automatic configuration file Http://
This is OK ~ can send and receive Gmail mail.
QQ Mail
POP3 Server:
SMTP server:
The SMTP server requires authentication. email with Gmail account

Phpmailer (now the version is 1.73) is a very useful tool, you can easily use the PHP language to send mail, support SMTP and authentication, we always use it.

However, because Gmail has an SSL connection to SMTP: Outgoing Mail (SMTP) server-requires (use authentication)
Use Authentication:yes
Use Starttls:yes (some clients call this SSL)
port:465 or 587

Use of Phpmailer can not normally connect Gmail's sending server, and this problem has not been Phpmailer official solution, but in the above the discussion is found a bit of information, using the following method can connect Gmail.

Modify class.smtp.php, line 101th, $this->smtp_conn = Fsockopen ($host, # The host of the server

Change to $this->smtp_conn = Fsockopen (' ssl://'. $host, # The host of the server

This is OK, is to specify the use of SSL protocol to connect the host, of course, PHP OpenSSL module must be open: Extension=php_openssl.dll

Instead of using a non-SSL host, I don't like to put two Phpmailer under the Include directory, so I figured out a better way:

Open class.phpmailer.php, near 543 lines, function Smtpconnect (), find: $this->smtp->do_debug = $this->smtpdebug;
$hosts = Explode (";", $this->host);
$index = 0;
$connection = ($this->smtp->connected ());

Retry while there is no connection
while ($index < count ($hosts) && $connection = = False)
if (Strstr ($hosts [$index], ":"))
List ($host, $port) = Explode (":", $hosts [$index]);
$host = $hosts [$index];
$port = $this->port;

Part of the function of this section is that if you enter the host name with the port number, automatically identified, although the idea is good, but this is the part of the call we can not directly use the SSL format of the host name, because the "ssl://xxx" form will be mistaken for the host: the form of the port number, The other port number is generally more fixed, we manually set up, also do not have to have to assign values with the host, so add after the code above://modify by Fwolf @ 2006-4-14, to enable SSL mail connection
$host = $this->host;
$port = $this->port;

On it, use the normal SMTP host, the usage is unchanged, use Gmail, use ssl:// as host name, the only slightly trouble is that the port needs to manually specify-in fact, not trouble.

Following the configuration changes above, there will also be a warning message when you use the Gmail account to send a message: Warning:fgets (): Ssl:fatal protocol error in H:\PHP_INCLUDES\PHPMAILER_SSL\CLA
ss.smtp.php on line 1024

This warning message in the help of PHP, as if the use of SSL connection, read the file read the file at the end of the problem, the need for manual shielding, or artificial control of the length of reading, we use the simplest way to prohibit the warning message display on it, Find the 1024 lines of class.smtp.php and add a @ to the fgets ($this->smtp_conn,515) function.

Here is a complete function to send mail using Phpmailer: function Send_mail ($to _address, $to _name, $subject, $body, $attach = ")
Send mail using Phpmailer
Require_once ("phpmailer/class.phpmailer.php");
$mail = new Phpmailer ();
$mail->issmtp (); Set Mailer to use SMTP
$mail->charset = ' utf-8′;
$mail->encoding = ' base64′;
$mail->from = ' ';
$mail->fromname = ' Fwolf ';
$mail->sender = ' ';
$mail->confirmreadingto = ' '; Receipt.

$mail->host = ' ssl:// ';
$mail->port = 465; Default is, Gmail is 465 or 587
$mail->smtpauth = true;
$mail->username = "";
$mail->password = "xxx";

$mail->>ddaddress ($to _address, $to _name);
$mail->addreplyto (' ', "Fwolf"); For Gmail is useless, Gmail is in-reply-to:,phpmailer default generated is reply-to:
$mail->wordwrap = 50;
if (!empty ($attach))
$mail->addattachment ($attach);
$mail->ishtml (FALSE);
$mail->subject = $subject;
$mail->body = $body;
$mail->altbody = "This are the body in plain text for non-html mail clients";

if (! $mail->send ())
echo "Mail send failed.\r\n";
echo "Error message:". $mail->errorinfo. "\ r \ n";
return false;
Echo ("Send $attach to $to _name < $to _address> successed.\r\n");
return true;
echo "Message has been sent";
} update @ 2006-11-3

Thank Rainchen for a better solution to the fgets error hint (the location is approximately 1024 lines in class.smtp.php):
Will: Fgets ($this->smtp_conn,515)

Instead:!feof ($this->smtp_conn) && $str = fgets ($this->smtp_conn,515)

Such code is much more standardized and strongly discouraged from using @ to block error messages. supporting the Chinese name of the attachment


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.