PHP uses phpmailer to send mails and phpmailer to send mails

Source: Internet
Author: User
Tags word wrap

PHP uses phpmailer to send mails and phpmailer to send mails

If there is no sending email on the local server, it is a good choice to use the existing SMTP server to send mail. The tool used here is phpmailer (Version 5.2.0), and the SMTP server selects gmail and 163.

1. Use scripts sent by gmail

Include ("class. phpmailer. php "); include (" class. smtp. php "); // get the content of an external file $ mail = new PHPMailer (); $ body = file_get_contents('contents.html '); $ body = eregi_replace (" [\] ", '', $ body); // set the smtp parameter $ mail-> IsSMTP (); $ mail-> SMTPAuth = true; $ mail-> SMTPKeepAlive = true; $ mail-> SMTPSecure = "ssl"; $ mail-> Host = "smtp.gmail.com"; $ mail-> Port = 465; // enter your gmail account and Password $ mail-> Username = "yourname@gmail.com"; $ mail-> Password =" Password "; // set the sender, it is best not to forge the address $ mail-> From =" yourname@gmail.com "; $ mail-> FromName =" Webmaster "; $ mail-> Subject = "This is the subject"; $ mail-> AltBody = $ body; $ mail-> WordWrap = 50; // set word wrap $ mail-> MsgHTML ($ body); // set the reply address $ mail-> AddReplyTo ("yourname@gmail.com", "Webmaster"); // Add an attachment, the attachment and script are located in the same directory. // otherwise, enter the complete path $ mail-> AddAttachment ("attachment.jpg"); $ mail-> AddAttachment ("attachment.zip "); // set the email address and name of the email recipient $ ma Il-> AddAddress ("toname@gmail.com", "FirstName LastName"); // send an email in HTML format $ mail-> IsHTML (true ); // Send an email using the Send method // process the email Based on the sending result if (! $ Mail-> Send () {echo "Mailer Error:". $ mail-> ErrorInfo;} else {echo "Message has been sent ";}

2. Use the 163 email sending script

You only need to change the SMTP configuration and account password. The SMTP configuration is as follows:

// Set smtp parameters // note that ssl protocol $ mail-> IsSMTP (); $ mail-> SMTPAuth = true; $ mail-> SMTPKeepAlive = true; $ mail-> Host = "smtp.163.com"; $ mail-> Port = 25;

If the test passes in the local wampserver environment, you must enable the php_openssl extension.

The above is all the content of this article. I hope you will like it.

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.