How to use Phpmailer to send mail in thinkphp

Source: Internet
Author: User
Tags html page


Phpmailer Send mail function is very powerful, today's real experience, simply say configuration, I was in thinkphp is used

Configuration steps:

1. Background configuration send mail class, location admin/common/common.php

In

The code is as follows Copy Code

function SendMail ($tomail, $title, $content)
{

/* Message Setup Information * *
$email _set = C (' Email_set ');

Vendor (' Phpmailer.class#phpmailer ');
Vendor ("Phpmailer.class#smtp"); Optional, otherwise it will be included in the class.phpmailer.php

$mail = new Phpmailer (true); Instantiate the Phpmailer class, true to throw an exception when an error occurs

$mail->issmtp (); Using SMTP

$mail->charset = "UTF-8";//Set message encoding
$mail->host = $email _set[' Host ']; SMTP Server
$mail->smtpdebug = 1; Enable SMTP Debugging 1 = errors 2 = messages
$mail->smtpauth = true; Server needs authentication
$mail->port = $email _set[' Port ']; Set port
$mail->smtpsecure = "SSL";
/*
$mail->smtpsecure = "SSL";
$mail->host = "smtp.gmail.com";
$mail->port = 465;
*/

$mail->username = $email _set[' Email_user ']; User account for SMTP server
$mail->password = $email _set[' email_pwd ']; User password for SMTP server
$mail->addreplyto ($email _set[' email '), $email _set[' email_name '); Reply to this mailbox when the recipient replies, you can execute the method multiple times
if (Is_array ($tomail)) {
foreach ($tomail as $m) {
$mail->addaddress ($m, ' user ');
}
}else{
$mail->addaddress ($tomail, ' user ');
}

$mail->setfrom ($email _set[' email '), $email _set[' email_name ');
$mail->addattachment ('./img/phpmailer.gif '); Add an attachment and repeat the method if there are multiple attachments
$mail->subject = $title;

The following are the message content related
$mail->body = $content;
$mail->ishtml (TRUE);

$body = file_get_contents (' tpl.html '); Get HTML page Content
$mail->msghtml (Eregi_replace ("[]", ", $body));


return $mail->send ()? True:false;

2: Configuration parameters in the configuration file:

The code is as follows Copy Code

/* Mail Settings * *
' Email_set ' =>array (
' Host ' => ' smtp.163.com ',
' Port ' => ' 25 ',
' Email_user ' => ' liuying ',
' Email_pwd ' => ' 123456 ',
' Email ' => ' liuying@163.com ',
' Email_name ' => ' 86 market network ',
),

3. Test Send code:

  code is as follows copy code

SendMail (' 11234@126.com ', ' hello ', ' I am content ');

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.