Use Composer to perfect your PHP framework (ii)

Source: Internet
Author: User
Tags php framework

Review

In the previous article, we built a simple view loader by hand and introduced the error handling package to make our MFFC framework "usable" in M, V, C three. View is a pluggable component, and in this article we will create another pluggable component-the mail sending module.

Body

We use the ' nette/mail ' package as our mail-sending base module, which encapsulates a ' mail ' class on its basis, exposing a concise API to the controller, which we formally begin with.

Introduce ' Nette/mail ' package, modify ' Composer.json ':

"Require": {  "Codingbean/macaw": "Dev-master",  "illuminate/database": "*",  "Filp/whoops": "*",  " Nette/mail ":" * "},

Run ' composer Update ' and wait for the installation to complete. The ' Nette/mail ' document is located at: http://doc.nette.org/en/2.2/mailing let's read it and then design the Mail class:

Create a new ' services/mail.php ' file with the following content:

<?phpuse nette\mail\message;/*** \mail*/class Mail extends message{public $config;  [String] e-mail protected $from;  [Array] e-mail list protected $to;  protected $title;  protected $body; function __construct ($to) {$this->config = require base_path. '    /config/mail.php ';    $this->setfrom ($this->config[' username ');      if (Is_array ($to)) {foreach ($to as $email) {$this->addto ($email);    }} else {$this->addto ($to); }} public function from ($from =null) {if (! $from) {throw new InvalidArgumentException ("Mail send address cannot be empty!    ");    } $this->setfrom ($from);  return $this; public static function to ($to =null) {if (! $to) {throw new InvalidArgumentException ("Mail receive address cannot be empty!    ");  } return new Mail ($to); Public Function title ($title =null) {if (! $title) {throw new InvalidArgumentException ("The message header cannot be empty!    ");    } $this->setsubject ($title);  return $this; The Public function content ($conTent=null) {if (! $content) {throw new InvalidArgumentException ("The message content cannot be empty!    ");    } $this->sethtmlbody ($content);  return $this; }}

The Mail class and the View class work in a way that is basically the same:

$this->mail = mail::to ([' [email protected] ', ' [email protected] ')                    ->from (' Motherfucker <[email Protected]> ')                    ->title (' Fuck me! ')                    ->content (' 

The above code is in HomeController, ' view::make () ' below the line of code.

New ' mffc/config/mail.php ', please replace your e-mail address and password yourself:

<?phpreturn [  ' host ' = ' smtp.163.com ',  ' username ' = ' [email protected] ',  ' password ' = ' Password ',  ' secure ' = ';

Mail and View are also handled in the Basecontroller destructor __destruct () function, and now this function looks like this:

Public Function __destruct () {  $view = $this->view;  if ($view instanceof view) {    extract ($view->data);    Require $view->view;  }  $mail = $this->mail;  if ($mail instanceof Mail) {    $mailer = new Nette\mail\smtpmailer ($mail->config);    $mailer->send ($mail);  }}

OK, ready for almost, run ' composer Dump-autoload ' to add the Mail class to automatically load, refresh the page!

If you see the above page, congratulations! The mail was sent successfully!

Go check the Inbox, there's Wood and mail! :-D This page load may be slightly slower because the messages are sent synchronously. Asynchronous queue System We'll talk about it later.

Analysis

The overall process of mail sending must have been pro, and now mainly describe the Mail class design process:

    1. The core parameter of the message is ' Destination address ', which is the e-mail address to which the message is sent, so we design mail::to (' [email protected] ') as the ' trigger API ' for sending.
    2. Currently we use the simplest ' SMTP ' way to send mail, documents here. The configuration file is placed in ' mffc/config/mail.php ' and still returns an array.
    3. The Mail class inherits the ' Nette\mail\message ' class. ' Mail::to () ' Creates an instance (object) of the Mail class and returns it, when the code in the destructor in ' Basecontroller ' is already triggered and processed by the object. The default sender is the ' username ' read from the configuration file.
    4. ' Mail::to () ' supports strings or arrays as parameters and can send one or more messages at a time.
    5. The ' from () ', ' title () ' and ' content () ' methods are used to enrich the contents of the message. The ' content () ' method can pass HTML code directly.
    6. The ' from () ' configuration does not necessarily succeed, and some mail service providers do not support modifying the sender's address.
    7. After this variable is fully assembled, it is assigned to the controller's ' $mail ' member variable, which is then processed by the destructor, the message is sent, and after the successful page code is sent back to the client, the process ends.

Original address: https://lvwenhan.com/php/412.html

Use Composer to perfect your PHP framework (ii)

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.