Function code for sending mails in phpmail

Source: Internet
Author: User
The day before yesterday, I want to make a small function for the website, that is, to send an email to the administrator when the user leaves a message. We all know that there is a mail function in PHP, but the server must have the mail server function, such as simple SMTP or POP3. However, if the phpmail class is not used on our servers, you don't have to worry about it. This is a class written by a foreigner. we just need to "take it. The following is a function written based on the send () method in this class:
The code is as follows:
Function send_mail ($ title, $ content, $ from, $ to, $ charset = 'gbk', $ attachment = '')
Include '/class/PHPMail. class. php ';
Header ('content-Type: text/html; charset = '. $ charset );
$ Mail = new PHPMailer ();
$ Mail-> CharSet = $ charset; // sets the gb2312 Chinese encoding.
$ Mail-> IsSMTP (); // you can use SMTP to send emails.
$ Mail-> Host = ""; // Set the email server address
$ Mail-> Port = 25; // Set the mail server Port. the default value is 25.
$ Mail-> From = $ from; // you can specify the sender's email address.
$ Mail-> FromName = ""; // you can specify the sender's name.
$ Mail-> SMTPAuth = true; // Set whether password verification is required for SMTP. true indicates that password verification is required.
$ Mail-> Username = $ from; // you can specify the email address for sending an email.
$ Mail-> Password = ""; // Set the mailbox Password
$ Mail-> Subject = $ title; // you can specify the mail title.
$ Mail-> AltBody = "text/html"; // optional, comment out and test
$ Mail-> Body = $ content; // you can specify the email content.
$ Mail-> IsHTML (true); // Sets whether the content is of the html type.
$ Mail-> WordWrap = 50; // you can specify the number of characters in each line.
$ Mail-> AddReplyTo ("address", "name"); // you can specify the address of the recipient.
$ Mail-> AddAddress ($ to, "star mode training"); // you can specify the recipient address.
If ($ attachment! = '') // Set The Attachment
$ Mail-> AddAttachment ($ attachment, $ attachment );
If (! $ Mail-> Send ())
Return false;
} Else {
Return true;

Generally, QQ Mail is used, because it is easy to enable SMTP and POP3 services, and it is free of charge. pay attention to the content format and encoding of the mail.
PHPMail. class. php: Click to download it!
Related Article

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: 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.