In the helper of codeigniter, use phpmailer to send emails.

Source: Internet
Author: User

Then write the following functions into the helper file:

[Php]
Function send_mail ($ to, $ title, $ body)
{
$ Ci = & get_instance ();
Require_once ('phpmailer/class. phpmailer. php ');
Require_once ('phpmailer/class. smtp. php ');

$ Mail = new PHPMailer (true); // the true param means it will throw throtions on errors, which we need to catch
 
$ Mail-> IsSMTP (); // telling the class to use SMTP
 
Try {
$ Mail-> Host = $ ci-> config-> item ('mail _ smtp '); // SMTP server
$ Mail-> SMTPDebug = false; // enables SMTP debug information (for testing)
$ Mail-> SMTPAuth = true; // enable SMTP authentication
$ Mail-> Port = $ ci-> config-> item ('mail _ port'); // set the SMTP port for the GMAIL server

$ Mail-> Username = $ ci-> config-> item ('mail _ address'); // SMTP account username
$ Mail-> Password = $ ci-> config-> item ('mail _ passwd'); // SMTP account password

$ Mail-> AddAddress (www.2cto.com ,'');
$ Mail-> SetFrom ($ ci-> config-> item ('mail _ address'), $ ci-> config-> item ('mail _ name '));

$ Mail-> Subject = $ title;
$ Mail-> MsgHTML ($ body );

$ Mail-> Send ();
Return true;
} Catch (phpmailerException $ e ){
// Echo $ e-> errorMessage (); // Pretty error messages from PHPMailer
Return false;
} Catch (Exception $ e ){
// Echo $ e-> getMessage (); // Boring error messages from anything else!
Return false;
}

}
Function send_mail ($ to, $ title, $ body)
{
$ Ci = & get_instance ();
Require_once ('phpmailer/class. phpmailer. php ');
Require_once ('phpmailer/class. smtp. php ');
 
$ Mail = new PHPMailer (true); // the true param means it will throw throtions on errors, which we need to catch

$ Mail-> IsSMTP (); // telling the class to use SMTP

Try {
$ Mail-> Host = $ ci-> config-> item ('mail _ smtp '); // SMTP server
$ Mail-> SMTPDebug = false; // enables SMTP debug information (for testing)
$ Mail-> SMTPAuth = true; // enable SMTP authentication
$ Mail-> Port = $ ci-> config-> item ('mail _ port'); // set the SMTP port for the GMAIL server

$ Mail-> Username = $ ci-> config-> item ('mail _ address'); // SMTP account username
$ Mail-> Password = $ ci-> config-> item ('mail _ passwd'); // SMTP account password

$ Mail-> AddAddress (www.2cto.com ,'');
$ Mail-> SetFrom ($ ci-> config-> item ('mail _ address'), $ ci-> config-> item ('mail _ name '));
 
$ Mail-> Subject = $ title;
$ Mail-> MsgHTML ($ body );

$ Mail-> Send ();
Return true;
} Catch (phpmailerException $ e ){
// Echo $ e-> errorMessage (); // Pretty error messages from PHPMailer
Return false;
} Catch (Exception $ e ){
// Echo $ e-> getMessage (); // Boring error messages from anything else!
Return false;
}

}
In addition, write your mailbox configuration items in config

[Php]
$ Config ['mail _ name'] = 'jiao changyun ';
$ Config ['mail _ passwd'] = 'Password ';
$ Config ['mail _ address'] = 'xxxx @ 21cn.com ';
$ Config ['mail _ smtp '] = 'smtp .21cn.com ';
$ Config ['mail _ smtp_port '] = 25;
$ Config ['mail _ name'] = 'jiao changyun ';
$ Config ['mail _ passwd'] = 'Password ';
$ Config ['mail _ address'] = 'xxxx @ 21cn.com ';
$ Config ['mail _ smtp '] = 'smtp .21cn.com ';
$ Config ['mail _ smtp_port '] = 25;

Author: jiaochangyun
 

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.