This article mainly introduces the implementation of PHP using QQ mailbox to send mail, has a certain reference value, now share to everyone, have the need for friends can refer to
In PHP application development, often need to verify the user mailbox, send message notification, and using the PHP built-in mail () function, you need the support of the mail system.
If you are familiar with the IMAP/SMTP protocol, you can write a mail-sending program with the Socket function, but it is not easy to develop such a program.
Fortunately, the Phpmailer package is strong enough to use it to send mail more easily, sparing us a lot of extra trouble.
Phpmailer
Phpmailer is a packaged PHP mail sending class, support for sending HTML content of e-mail, and can add attachments to send, not like the php itself mail () function requires server environment support, you only need to set up the mail server with relevant information to achieve the mail delivery function.
Phpmailer Project Address :Https://github.com/PHPMailer/PHPMailer
PHP extension Support
Phpmailer need PHP sockets extension support, and login QQ mailbox SMTP server must be encrypted by SSL, so PHP has to include OpenSSL support.
↑ Use the Phpinfo () function to view the socket and OpenSSL extension information (WAMP server has enabled the extension by default).
Phpmailer Core File
↑ only class.phpmailer.php and phpmailer/class.smtp.php are required in this document.
QQ Mailbox Settings
All major mailboxes support the SMTP protocol, but not all mailboxes are turned on by default, and you can open them manually in the settings of your mailbox.
Third-party services after providing the account and password, you can log in to the SMTP server, through which to control the way the mail transfer.
Turn on the SMTP service
↑ Select the IMAP/SMTP service and click on the service to open.
Verify the Secret Warranty
↑ Send SMS "Configure mail Client" to 1069-0700-69.
Get Authorization Code
↑SMTP Server Authentication password, need to be properly kept (PS: password directly without space).
php Send mail
Basic code
The following code demonstrates how Phpmailer is used, and note the configuration process for the Phpmailer instance.
Introduction of Phpmailer's core document require_once ("phpmailer/class.phpmailer.php"); Require_once ("phpmailer/class.smtp.php");// Instantiate the Phpmailer core class $mail = new Phpmailer ();//Whether to enable debug for SMTP debugging development environment It is recommended to turn on the production environment comments off can be turned off by default Debug debug mode $mail->smtpdebug = 1;// Use SMTP authentication to send mail $mail->issmtp ();//SMTP requires authentication this must be True$mail->smtpauth = true;//link QQ domain Mailbox server address $mail->host = ' Smtp.qq.com ';//set to use SSL encryption to login authentication $mail->smtpsecure = ' SSL ';//Set SSL to connect the SMTP server to the remote server port number $mail->port = 465;// Set the encoding of the sent message $mail->charset = ' UTF-8 ';//Set the sender's nickname to display the sender's name before the sender's email address of the recipient message $mail->fromname = ' sender nickname ';//SMTP login account QQ mailbox can $mail->username = ' 12345678@qq.com ';//SMTP login password using the generated authorization code $mail->password = ' ********** ';//Set the sender's email address with the login account $mail->from = ' 12345678@qq.com ';//whether the message body is HTML-encoded note here is a method $mail->ishtml (true);//Set the recipient's mailbox address $mail-> AddAddress (' 87654321@qq.com ');//Add multiple recipients by calling the method multiple times $mail->addaddress (' 87654321@163.com ');//Add the subject of the message $mail-> Subject = ' mail subject ';//Add message body $mail->body = '
Encapsulation method
If you want to send mail directly using Phpmailer, you'll need to do a tedious configuration to do so much less efficiently.
To simplify the call process, I've made two footprints on top of it, and only need to configure the account, password, and nickname to customize your own Qqmailer class.
<?phprequire_once ' phpmailer/class.phpmailer.php '; require_once ' phpmailer/class.smtp.php '; class QQMailer{Publi c Static $HOST = ' smtp.qq.com '; The server address of the QQ mailbox is static $PORT = 465; The remote server port number for the SMTP server is public static $SMTP = ' SSL '; Use SSL encryption to log on to public static $CHARSET = ' UTF-8 '; Sets the encoding of the sent message private static $USERNAME = ' 123456789@qq.com '; Authorized login account private static $PASSWORD = ' **************** '; Authorized login Password private static $NICKNAME = ' Woider '; Sender's Nickname/** * Qqmailer constructor. * @param bool $debug [debug mode] */Public function __construct ($debug = False) {$this->mailer = new P Hpmailer (); $this->mailer->smtpdebug = $debug? 1:0; $this->mailer->issmtp (); Use SMTP to send mail}/** * @return Phpmailer */Public Function Getmailer () {return $t his->mailer; } Private Function Loadconfig () {/* Server Settings */$this->mailer->smtpauth = true; Turn on SMTP authentication $this->mailer->host = self:: $HOST; SMTP server address $this->mailer->port = self:: $PORT; Remote server port number $this->mailer->smtpsecure = self:: $SMTP; Login authentication Method/* Account Settings */$this->mailer->username = self:: $USERNAME; SMTP login account $this->mailer->password = self:: $PASSWORD; SMTP login password $this->mailer->from = self:: $USERNAME; Sender email address $this->mailer->fromname = self:: $NICKNAME; Sender nickname (any content)/* Content Setting */$this->mailer->ishtml (TRUE); Whether the message body is HTML $this->mailer->charset = self:: $CHARSET; The encoding of the sent message}/** * ADD attachment * @param $path [Attachment Path] */Public Function AddFile ($path) { $this->mailer->addattachment ($path); }/** * Send Email * @param $email [to] * @param $title [subject] * @param $content [body] * @return BOOL [Send status] */public FUnction Send ($email, $title, $content) {$this->loadconfig (); $this->mailer->addaddress ($email); Recipient mailbox $this->mailer->subject = $title; Message subject $this->mailer->body = $content; Mail message return (BOOL) $this->mailer->send (); Send Message}}
qqmailer.php
Require_once ' qqmailer.php ';//Instantiate Qqmailer$mailer = new Qqmailer (true);//Add Attachment $mailer->addfile (' 20130vl.jpg ');// Message title $title = ' Wish a heart, never to be absent. ';//e-mail content $content = <<< eof<p align= "center" > ai such as snow on the mountain, jiao if clouds between the moon. <br> Shu Jun has two meanings, so it is to refuse. <br> today's bucket, Ming Dan Ditch head. <br> Myeongdong Station asasfor on the ditch, ditch water and things flow. <br> desolately desolately, marrying no need to cry. <br> is willing to have a heart, never to be absent. <br> Bamboo Pole What curl, fish tail He Hing! <br> man is a heavy-blooded, what money knife for! </p>eof;//Send QQ Mail $mailer->send (' 123456789@qq.com ', $title, $content);
Test results
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!