Phpmailer Send mail (ii)

Source: Internet
Author: User
Tags autoload valid email address

Found that Phpmailer has made a large update, the previous method of sending mail has been less applicable, just to do an experiment, need to build an environment, where the use of Gmail to test, is now recorded.

Delivery address Github:phpmailer

The basic requirements are the same as the previous article: Phpmailer send mail (i)

First, the basic requirements

    • Web Access is normal (Apache can access it normally)
    • PHP is set up correctly (Sockets support, OpenSSL two are set to enable)
    • Gmail settings, allow less secure apps: set to Enabled

Can write a simple sentence to test: info.php

<? PHP Phpinfo ();? >

Second, Phpmailer

The new version of the Phpmailer with the biggest difference is the need to use Composer, Ubuntu under the Composer installation can refer to: Ubuntu installation PHP Composer

After the installation is complete, we need to use composer to download the Phpmailer.

We first switch to the Web and directory, create a phpmailer directory, switch to the directory, and then use the command to download the file: Composer require Phpmailer/phpmailer, this process will take some time.

[Email protected]:/var/www/html$ pwd/var/www/html[email protected]:/var /www/html$ lshello. html  index.html  info.  PHP  phpmailer[email protected]:/var/www/html$ cd phpmailer/[email protected]:/  varrequire phpmailer/phpmailer

After the download is complete, we can see some more files in the directory, we create a new file mailer.php to send the file.

[Email protected]:/var/www/html/phpmailer$ pwd/var/www/html/Phpmailer[email Protected]:/var/www/html/phpmailer$ lscomposer. JSON  composer.lock  Mailer.  php  vendor[email protected]:/var/www/html/phpmailer$ ls vendor/autoload . php  composer  Phpmailer

The contents of mailer.php are as follows:

<?PHP//Import Phpmailer classes into the global namespace//these must is at the top of your script, not inside a function< /c18> UsePhpmailer\phpmailer\phpmailer; UsePhpmailer\phpmailer\Exception;//Load composer ' s autoloaderrequire' Vendor/autoload.php ';$mail=NewPhpmailer (true);//passing ' true ' enables exceptionsTry {    //Server Settings    $mail->smtpdebug = 2;//Enable verbose debug output    $mail-&GT;ISSMTP ();//Set Mailer to use SMTP    $mail->host = ' smtp.gmail.com ';//Specify main and backup SMTP servers    $mail->smtpauth =true;//Enable SMTP Authentication    $mail->username = ' [email protected] ';//SMTP username    $mail->password = ' Your Password ';//SMTP Password    $mail->smtpsecure = ' TLS ';//Enable TLS encryption, ' SSL ' also accepted    $mail->port = 587;//TCP port to connect to//recipients    $mail->setfrom (' [email protected] ', ' starnightcyber '); $mail->addaddress (' [email protected] ');//Name is optional    $mail->addaddress (' [email protected] '); //Attachments//$mail->addattachment ('/var/tmp/file.tar.gz ');    ADD attachments//$mail->addattachment ('/tmp/image.jpg ', ' new.jpg '); Optional name//content    $mail->ishtml (true);//Set Email format to HTML    $mail->subject = ' Test mail. '; $mail->body = ' Hello, this is a test mail using Phpmailer '; $mail->altbody = ' Hello, this is a test mail using Phpmailer '; $mail-Send (); Echo' Message has been sent ';} Catch(Exception $e) {    Echo' Message could not being sent. '; Echo' Mailer Error: '.$mail-errorinfo;}?>

Visit the site:

Wait for a while (it may be slower depending on your network condition), we can see that the message was sent successfully, the following is the log information and related messages sent.

2017-11-08 03:37:47 SERVER, client:220 smtp.gmail.com ESMTP d74sm5796226pfe.167-gsmtp2017-11-08 03:37:47 CLIENT-& Gt Server:ehlo 192.168.0.82017-11-08 03:37:47 SERVER, client:250-smtp.gmail.com at your service, [210.45.123.80]250-s IZE 35882577250-8bitmime250-starttls250-enhancedstatuscodes250-pipelining250-chunking250 SMTPUTF82017-11-08 03:37:47 CLIENT, server:starttls2017-11-08 03:37:47 SERVER, client:220 2.0.0 ready to start tls2017-11-08 03:3 7:47 CLIENT, Server:ehlo 192.168.0.82017-11-08 03:37:48 SERVER, client:250-smtp.gmail.com at your service, [21 0.45.123.80]250-size 35882577250-8bitmime250-auth LOGIN PLAIN XOAUTH2 plain-clienttoken oauthbearer xoauth250-enhancedstatuscodes250-pipelining250-chunking250 smtputf82017-11-08 03:37:48 CLIENT--Server:auth login2017-11-08 03:37:48 SERVER, client:334 vxnlcm5hbwu62017-11-08 03:37:48 CLIENT, Server:c3rhcm5pz2h0y3lizx jaz21hawwuy29t2017-11-08 03:37:48 SERVER, client:334 UGFZC3DVcmq62017-11-08 03:37:48 CLIENT, server:mtizqhdhew5ljmx6otmj2017-11-08 03:37:49 SERVER, client:235 2.7.0 Accept ed2017-11-08 03:37:49 CLIENT, server:mail from:<[email protected]>2017-11-08 03:37:49 SERVER client:250 2.1.0 OK d74sm5796226pfe.167-gsmtp2017-11-08 03:37:49 CLIENT--SERVER:RCPT to:<[email protected ]>2017-11-08 03:37:49 SERVER, client:250 2.1.5 OK d74sm5796226pfe.167-gsmtp2017-11-08 03:37:49 CLIENT SER VER:RCPT to:<[email protected]>2017-11-08 03:37:49 SERVER, client:250 2.1.5 OK d74sm5796226pfe.167-gsm  tp2017-11-08 03:37:49 CLIENT, server:data2017-11-08 03:37:50 SERVER, client:354 Go ahead d74sm5796226pfe.167- gsmtp2017-11-08 03:37:50 Client, SERVER:Date:Wed, 8 11:37:42 +08002017-11-08 03:37:50 Client-SERVER : To: [email protected], [email protected]2017-11-08 03:37:50 CLIENT--SERVER:From:starnightcyber <[ email protected]>2017-11-03:37:50 client, SERVER:Subject:Test mail.2017-11-08 03:37:50 client, Server:message-id: <[email p rotected]>2017-11-08 03:37:50 CLIENT, Server:x-mailer:phpmailer 6.0.1 (https://github.com/PHPMailer/ Phpmailer) 2017-11-08 03:37:50 Client--server:mime-version:1.02017-11-08 03:37:50 client--Server:content-type : multipart/alternative;2017-11-08 03:37:50 CLIENT, server:boundary= "b1_ Pn05ke4mc7wgzpd3bwrxrwz35nsih2r1vkcmlvli "2017-11-08 03:37:50 CLIENT--server:content-transfer-encoding:  8bit2017-11-08 03:37:50 Client--server:2017-11-08 03:37:50 Client--Server:this is a multi-part message in MIME format.2017-11-08 03:37:50 CLIENT, SERVER:--b1_pn05ke4mc7wgzpd3bwrxrwz35nsih2r1vkcmlvli2017-11-08 03:37:50 CLIENT--Server:content-type:text/plain; charset=us-ascii2017-11-08 03:37:50 Client, server:2017-11-08 03:37:50 client, Server:hello, this is a test MA Il using phpmailer2017-11-08 03:37:50 CLIENT--SERVer:2017-11-08 03:37:50 Client, SERVER:--b1_pn05ke4mc7wgzpd3bwrxrwz35nsih2r1vkcmlvli2017-11-08 03:37:50 Client-& Gt server:content-type:text/html; charset=us-ascii2017-11-08 03:37:50 Client, server:2017-11-08 03:37:50 client, Server:hello, this is a test MA Il using phpmailer2017-11-08 03:37:50 client--server:2017-11-08 03:37:50 Client--server:2017-11-08 03:37:50 CL Ient, SERVER:--b1_pn05ke4mc7wgzpd3bwrxrwz35nsih2r1vkcmlvli--2017-11-08 03:37:50 CLIENT--server:2017-11-08 03:37:50 CLIENT, server:. 2017-11-08 03:37:52 Server, client:250 2.0.0 OK 1510112272 d74sm5796226pfe.167-gsmt p2017-11-08 03:37:52 CLIENT, server:quit2017-11-08 03:37:52 SERVER, client:221 2.0.0 closing connection D74SM5 796226pfe.167-gsmtpmessage has been sent
phpmailer Send mail log

You can see that the message was sent successfully.

In addition, the above code to give the mailbox is a valid email address, no code, just so that you can see the effect more clearly, please change to their own mailbox, or they can not see whether the sending success, don't blind a few send me e-mail ^_^.

Phpmailer Send mail (ii)

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: info-contact@alibabacloud.com 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.