Phpmailer class calls the remote SMTP server to send mail implementation method

Source: Internet
Author: User
Tags php email word wrap
This article mainly introduces the phpmailer class call method for sending mails from a remote SMTP server, and analyzes the phpmailer Class Call and mail sending techniques in combination with the instance form, for more information about how to use the php mailer class to call a remote SMTP server to send emails, see the example below. We will share this with you for your reference. The details are as follows:

Php mailer is a very useful php email sending module. it can call local smtp to send emails or remote smtp to send emails, however, you need to pay attention to some things during use. Otherwise, the sending may fail or cannot be called at all. This article describes the problems and solutions I encountered when using this class, briefly describe the usage and precautions of php mailer.

First, download the phpmailer class library file. here, you only need one resource point. : Http://www.bitsCN.com/codes/27188.html

After downloading the file, put class. phpmailer. php in a directory of your project, and write the following content where you need to send an email:

<? Phprequire 'class. phpmailer. php '; try {$ mail = new PHPMailer (true); $ body = file_get_contents('contents.html '); // the content of the mail is written to the contents.html page with $ body = preg_replace ('//', ', $ body); // Strip backslashes $ mail-> IsSMTP (); // tell the class to use SMTP $ mail-> SMTPAuth = true; // enable SMTP authentication $ mail-> Port = 25; // set the SMTP server port $ mail-> Host = "mail.yourdomain.com"; // remote SMTP server $ ma Il-> Username = "yourname@yourdomain.com"; // Username $ mail-> Password = "yourpassword" on the remote SMTP server "; // the password of the user on your remote SMTP server // $ mail-> IsSendmail (); // tell this class to use the Sendmail component, note this out if there is no sendmail build in use, otherwise there will be $ mail-> AddReplyTo ("yourname@yourdomain.com", "First Last "); $ mail-> From = "fromname@yourdomain.com"; $ mail-> FromName = "First Last"; $ to = "toname@domain.com"; $ mail-> AddAddress ($ ); $ mail-> Subject = "Firs T PHPMailer Message "; $ mail-> AltBody =" To view the message, please use an HTML compatible email viewer! "; // Optional, comment out and test $ mail-> WordWrap = 80; // set word wrap $ mail-> MsgHTML ($ body ); $ mail-> IsHTML (true); // send as HTML $ mail-> Send (); echo 'Message has been sent. ';} catch (phpmailerException $ e) {echo $ e-> errorMessage () ;}?>

Note: The above $ mail-> IsSendmail (); needs to be commented out, otherwise, if there is no sendmail component, the prompt "cocould not execute: /var/qmail/bin/sendmail "error!

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.