Brief introduction to ThinkPhp email sending class

Source: Internet
Author: User
Tags form post word wrap
In thinkphp learning, there are often many problems. mailbox sending problems will be used and involved continuously during the development of large websites. below we will talk about the two email sending mechanisms.

Thinkphp often encounters many problems, which are used and involved continuously during large-scale website development;

Let's talk about the two email sending mechanisms below:

1. intra-site email sending mechanism: it is not hard to understand that email sending is performed on the same site, that is, the email communication function of registered members on a website, this is very common in the development of leisure time to discuss more, his mechanism is simple and indeed a bit complex, but compared to the following relatively simple:

Communication between two users in the station, of course, you must first establish the mail data used by the user to send emails. to put it simply: Since communication is between users, you must have a user name or a user nickname. this is required. even if you do not want the recipient to see who the sender is, you must create an email subject, the content of the email and the content of the email attachment (this can be left blank for the time being)

You must use these two or three fields to store data. Also, you need an independent field to check whether new emails are unread, this field is saved in 1, 2 (for example). when a new email is sent, Data 1 is filled in this field by default. it is convenient to update the field to 2 after the user has watched it.

When a user sends an email to another user, the fields in the table of another user are filled with the title, content, and fields to determine whether or not the fields are read.

Update data fields when another user reads data.

This is the first form of intra-site text message sending, which is troublesome and easy to write code.

Email sending

Type 2: Send Mail pop3 stmp = through the mail protocol

The source code is attached to Tom here,

Class MessageAction extends Action {
Public function mail () {// mail method definition
$ Response = trim ($ _ POST ['comment']); // the comment string sent by the user through form POST.
If ($ response = ")
{$ This-> redirect ('index', 'test', ", APP_NAME," 1); // when the message is empty, it is redirected to the message page.
}
Vendor ("PHPMailer. class # phpmailer"); // class of the function package imported. phpmailer. php
// The following defines a function for sending emails, which has passed the test.
// $ Sendto_email: email sending address
// $ Subject: email subject
// $ Body: body of the email
// $ Sendto_name indicates the sender's name. It is generally saved.
Function smtp_mail ($ sendto_email, $ subject = null, $ body = null, $ sendto_name = null ){
$ Mail = new PHPMailer (); // creates a mail sending class object.
$ Mail-> IsSMTP ();

// Send via SMTP
$ Mail-> Host = "202.38.64.8"; // SMTP mail server address, which must be replaced with the mail server address where the email is sent
$ Mail-> SMTPAuth = true; // turn on SMTP authentication
$ Mail-> Username = "sdaping@mail.ustc.edu.cn"; // The user name of this mailbox on the SMTP server, some only need to @ the front part, and some need the full name. Replace it with the correct email user name
$ Mail-> Password = "123456"; // Password of the mailbox on the SMTP server. replace it with the correct Password.
$ Mail-> From = "sdaping@mail.ustc.edu.cn"; // The mailbox that sent this email on the SMTP server, replace it with the correct one, and the value of $ mail-> Username corresponds.
$ Mail-> FromName = "website user"; // enter the real sender's name and other information as needed.
$ Mail-> CharSet = "UTF-8"; // The character set is specified here!
$ Mail-> Encoding = "base64 ″;
$ Mail-> AddAddress ($ sendto_email, $ sendto_name); // recipient's email address and name
$ Mail-> AddReplyTo ('sdaping @ mail.ustc.edu.cn ', "administrator"); // This item is set as needed
// $ Mail-> WordWrap = 50; // set word wrap
// $ Mail-> AddAttachment ("/var/tmp/file.tar.gz"); // process attachments
// $ Mail-> AddAttachment ("/tmp/image.jpg", “new.jpg ");
$ Mail-> IsHTML (true); // send as HTML
// Email subject
$ Mail-> Subject = $ subject;
// Email content
$ Mail-> Body ='



'. $ Body .'';
$ Mail-> AltBody = "text/html ";
If (! $ Mail-> Send ())
{
Echo"


";
Echo "email sending error ";
Echo "email error message:". $ mail-> ErrorInfo ."";
Echo"";
Exit;
}
Else {
Echo"


";
Echo "email sent successfully !";
Echo"";
}
}
// Parameter description (email address, email subject, email content, and recipient's name)
$ Sendto_mail = 'admistrator @ sites.com '; // enter the email address to be sent to the Administrator. change it to the correct address.
$ Subject = 'website recommend'; // email subject
$ Body = $ response; // comments
Mail ($ sendto_mail, $ subject, $ body); // send an email
}
}
?>

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