Phpmailer implementing the ability to send mail

Source: Internet
Author: User
This article is mainly for everyone to introduce the Phpmailer send mail function, with a certain reference value, interested in small partners can refer to

PHP built-in mail functions are not easy to use, in addition to other languages, bloggers prefer the object-oriented package management mode, so Phpmailer became my first choice to send mail with PHP, here to share to you.

Library Import

Here we use composer for package management, the following is the JSON file:


{  "name": "",  "description": "Test The Mail",  "require": {    "Phpmailer/phpmailer": "*"    }}

Using the sample


<?php//include ("./vendor/autoload.php"); include ("./vendor/phpmailer/ Phpmailer/class.phpmailer.php "); include ("./vendor/phpmailer/phpmailer/class.smtp.php "); function Postmail ($to, $ Subject= "", $body = "") {$mail = new Phpmailer (); #创建PHPMailer实例 $mail->charset = ' utf-8 '; #设置编码形式, the default is Iso-8859-1 $mail ->ISSMTP (); #表示使用SMTP服务 $mail->smtpauth = true; #使用SMTP认证 $mail->host = ' smtp.163.com '; #SMTP主机名, here take 163 for example $mail->username = "xxxx@163.com"; #SMTP服务的用户名 $mail->password = "xxxx"; #SMTP用户的密码 (now more in the form of authorization codes) #下面的部分是用户可见的 $mail->setfrom ("xxxx@163.com", "userName"); #第二个参数, the user sees the sender's name, can be arbitrarily changed $mail->subject = $subject; #邮件主题 $mail->msghtml ($body); #邮件内容, this is $mail->addaddress ($to, ") in HTML form; #地址, the second parameter is the recipient, when the sending object is named ' ', the 163 test result is that the SMTP service will change it to ' I ' $mail->addattachment ('./php.png ');  #添加附件 #发送 if ($mail->send ()) {echo "Mail sent successfully";  }else {echo "failed to send mail"; }}postmail (' xxxx@qq.com ', ' test ', ' How are you ');? 

Test results

Use 163 as the sender, sent to the QQ mailbox, 163 mailboxes are passed the test.

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.