php Send mail function--thinkphp3.2.3 code detailed introduction

Source: Internet
Author: User
Tags php send mail
First step: In the online down a Phpmailer plugin, plug-in address "http://www.php.cn/Download decompression, here we only need to use two of them, as shown in:

Place the class.phpmailer.php and class.smtp.php two files respectively into a
thinkphp/library/vendor/phpmailer/class.phpmailer.php (note case)
thinkphp/library/vendor/phpmailer/class.smtp.php

Note: The thinkphp default third-party class library Directory is now placed, if defined in index.php such as define (' Vendor_path ', App_path. ' common/vendor/'); Then the file should be placed in the same path, lest the class ' Phpmailer ' not found the case.

2, create the user-defined function file application/home/common/function.php, put the following functions:

1/** 2 * function: Mail send function 3 * @param string $to target Mailbox 4 * @param string $subject message subject (title) 5 * @param string $to message content 6 * @re turn bool True 7 */8 function SendMail ($to, $subject, $content) {9 Vendor (' PHPMAILER.CLASS#SMTP ');    Phpmailer.class#phpmailer '); Pay attention to the case here, or there will be no class found, Phpmailer is the folder name, Class#phpmailer on behalf of class.phpmailer.php file name $mail = new Phpmailer (); 12//Assembly Mail  Server (C (' mail_smtp ')) {$mail->issmtp (),}16 $mail->host = C (' mail_host ');  The parameters explained here are described in the following configuration information note $mail->smtpauth = C (' Mail_smtpauth '); $mail->username = C (' Mail_username '), $mail->password = C (' Mail_password '); $mail->smtpsecure = C (' mail_secure '); $mail->charset = C (' Mail_charset '); 22//Assembly Header message $mail->from = C (' Mail_username ') ): $mail->addaddress ($to), $mail->fromname = C (' Mail_fromname '); $mail->ishtml (C (' mail_ishtml ') ); 27//Assemble message body information $mail->subject = $subject; 29 $mail->body = $content; 30//Send mail if (! $mail->send ()) {return false;33} else {34 Return true;35}36}


3. In the above function, the C method is used to load some configuration information, so we have to add the following configuration information in the configuration file (default/application/home/conf/config.php):

<?php 2  return Array (3     //Other configuration items omitted ...) 4     //configure mail sending server 5     ' mail_smtp '            +  TRUE, 6     ' mail_host '            = '  smtp.163.com ',          // Mail Send SMTP server 7     ' Mail_smtpauth '   +  TRUE, 8     ' mail_username '   = '  123***@163.com ' ,       //SMTP Server login username 9     ' mail_password '   = '  123456abc ',              //smtp server login password     ' Mail_ SECURE '         = '  tls ', one     ' mail_charset ' + '  utf-8 ',     ' mail_ishtml         '  true,13     ' mail_fromname '  and ' xxx website client ', +  ;

4, start the call, assuming that through the URL/?m=home&c=index&a=send access, then we corresponding in application/home/controller/ Add a method to the IndexController.class.php file as follows:

1 <?php 2  namespace Home\controller; 3 use  Think\controller; 4  class Indexcontroller extends Controller { 5 Public     Function Index () {6          7     } 8 public     function Send () {9         if (SendMail (' vsiryxm@qq.com '), ' Hello! Message header ', ' This is a test message body! ') {Ten             echo ' sent successfully! ';         }12         else{13             echo ' send failed! ';         }15     }16  }

The above is the PHP send mail function--thinkphp3.2.3 code detailed description of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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