TP5 sending mail using Phpmailer

Source: Internet
Author: User
Tags naming convention

TP uses phpmailer to send mail

1. request a mailbox for an SMTP service.

I am applying for smtp.163.com Service, note that the SMTP service password is not a login password and needs to be set separately

2. download the Phpmailer class library file http://pan.baidu.com/s/1kUNK9vx

3. Remove the class.phpmailer.php and class.smtp.php files from the Phpmailer class library , and renamed to phpmailer.php and smtp.php that conform to the TP5 naming convention

4. In the Extend folder under the project root directory , set up the phpmailer folder and put the above two renamed files in it.

5. set the smtp.php namespace to namespace Phpmailer; and Will CLASS SMTP instead CLASS Smtp

6. set the phpmailer.php namespace to

namespace Phpmailer;

Use PHPMAILER\SMTP;

and Will Class SMTP changed to class Phpmaiiler;

These two parts are important if you use the original Phpmailer and SMTP class name,TP5 Error Cannot find class file

7. the establishment of the email.php file under Application/extra will be in the service provider to apply for good email information, into which

The code is as follows

<?php
Send mail-related configuration
return [
' Email_smtp ' = ' smtp.163.com ',
' email_address ' = ' [EMAIL protected] ',
' Email_loginname ' = ' [EMAIL protected] ',
' Email_password ' = ' xxxx445864742 ',
' Port ' =>25
];

8. Create a new class library file under the Phpmailer folder email.php

The code is as follows:

<?php
namespacePhpmailer;
UsePhpmailer\phpmailer;
Send Message Class
classemail{
Public static functionSend ($address, $title, $message)
{
$Email =NewPhpmailer ();
Set Phpmailer to send email using an SMTP server
$Email->issmtp ();

Set string encoding
$Email->charset = ' UTF-8 ';

Add a recipient address that you can use multiple times to add more recipients
$Email->addaddress ($address);

Set the message body
$Email->body = $message;

Set the From field of the message header
$Email->from = config (' Email '. Email_address ');

Set Sender Name
$Email->fromname = ' Week ';

Set the message header
$Email->subject = $title;

Setting up an SMTP server
$Email->host = config (' Email '. Email_smtp ');

Set as verification code
$Email->smtpauth =true;

Set User name password
$Email->username = config (' Email '. Email_loginname ');
$Email->password = config (' Email '. Email_password ');

Send mail
return($Email->send ());
}
}

9. Call where you want to use the mail service

Send verification Messages modified according to business logic

$message = ' Hello ';

$title = ' Weekly personal test mail ';

$data [' email '] = [email protected]
\phpmailer\email::send($data [' Email '], $title, $message);

TP5 sending mail using Phpmailer

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.