Install and use the PHP Dependency management tool with the Phpmailer example: Composer

Source: Internet
Author: User
Tags autoload php email string format server port ssl connection
Preface:

This blog is mainly to introduce composer installation and use, and load Phpmailer code library as an example for everyone to do a demo.

Environment: Centos7 + LAMP

Because of the example involved in PHP email, so before starting this blog, we suggest that you first read my previous blog: "PHP use Phpmailer with QQ mailbox implementation of email ," A, prepare to work:

Now we create a new testmail directory in the WWW directory, create a new index.php file as a portal file in the Testmail folder, and create a new function.php file as a common function library file. Ii. installation and use of composer:

Composer is a PHP dependency management tool that allows you to declare the code base in your project, and it will install them for you in your project. Here, our phpmailer will be introduced into our small framework as a code base, and this tool is composer. 1, installation composer:

Install Composer, you only need to download the Composer.phar executable file in the project root directory.

Cd/home/www/testmail
curl-ss Https://getcomposer.org/installer | php

To check if Composer is working, just use PHP to perform PHAR:

PHP Composer.phar

This will return you to a list of executable commands. 2. Project installation: Composer.json

To start using Composer in your project, you only need a Composer.json file. This file contains the dependencies of the project and some other metadata.

About require Key

First thing (and often just do it), you need to specify the value of the Require key in the Composer.json file. All you have to do is simply tell Composer which packages your project depends on.

JSON format:

{
    "require": {"Monolog/monolog":
        "1.0.*"
    }
}

As you can see, require needs a package name (for example, Monolog/monolog) to map to the package version (for example, 1.0.*)

What we need to use here is Phpmailer:

Vim Composer.json

#在composer. json file to add the following
{
    "require": {"
        Phpmailer/phpmailer": "*"
    }
}

#保存退出

The code above indicates that we want to obtain a project named Phpmailer that is supplied by the Phpmailer supplier, and "*" means any version (should be getting the latest version) 3, installation dependency pack

To get a defined dependency on your local project, just call Composer.phar to run the install command.

PHP Composer.phar Install

Run the above command (this process will be a bit slow, slightly wait ~ ~ ~ ~):

After running the above command, we will find that we have added some files to our Testmail directory:

After running the above code, this will find the latest version of Phpmailer/phpmailer and download it to the vendor directory. It's a convention to place third party code in a specified directory vendor. If it is Phpmailer, the Vendor/phpmailer/phpmailer directory will be created. The install command will also create a composer.lock file into the root directory of your project. 4, Necessary analysis: Composer.lock

After installing dependencies, Composer writes the exact version number list of the installation to the Composer.lock file. This will lock the specific version of the project.

The install command checks to see if the lock file exists and, if it exists, downloads the specified version (ignoring the definition in the Composer.json file)

If no Composer.lock file exists, composer reads the Composer.json and creates a lock file.

This means that if your dependency updates a new version, you will not get any updates. To update your dependent version at this time, use the update command. This will get the latest matching version (according to your Composer.json file) and update the new version into the lock file

PHP Composer.phar Update

If you only want to install or update a dependency, you can whitelist them. For example, you are now adding a lot of dependencies, but just want to update Phpmailer:

PHP Composer.phar Update Phpmailer/phpmailer
5. Automatic loading: Using Phpmailer dependency Pack

Auto Load: Composer generates a vendor/autoload.php file for automatic load information on the library. You can simply introduce this file and you will get a free automatic load support.

Require ' vendor/autoload.php ';

With automatic loading, we can easily use third party code.

OK, now we add the following code to the index.php file:

#index. php file

<?php
//Add common function library file
require "function.php";

Add auto Load support
require "vendor/autoload.php";

? >

Add the SendMail () function to function.php (in the last blog, a classmate asked me how to achieve mass, here by the way to achieve it):

#function. php file <?php/** * Send mail method * @param $to: Receiver array $title: Title $content: Message content/function SendMail (array $to, $TITL   E, $content) {//configuration (strongly recommended to write to profile, here I am only for convenience) $config = Array (//configure mail sending server ' Mail_debug ' => 0, Do you want to enable debug for SMTP to debug ' mail_host ' => ' smtp.qq.com ',//SMTP server address ' mail_hostname ' => ' http ://lsgozj.cn ',//Set sender's host domain ' Mail_port ' => 465,//Set the remote server port number for SSL connection SMTP Server optional 465 or 587 ' MAIL_SMTP  AUTH ' => TRUE,/Enable SMTP authentication ' mail_username ' => ' 123456789@qq.com ',//username ' Mail_from ' => ' 123456789@qq.com ',//email address ' mail_fromname ' => ' lsgo laboratory ',//Sender name ' Mail_password ' => ' Yacyti  Iryfzsbbif ',//SMTP login password uses generated authorization code ' mail_charset ' => ' UTF-8 ',//Character set ' mail_ishtml ' => TRUE, Whether the HTML format mail ' mail_replyto ' => ' 987654321@qq.com ',//the user replies to the message when the receiving mailbox, can be separated from the original mailbox//CC is you write this email in addition to the delivery , and will also send you the email address in the CC column, andRecipient > Know that you sent this email to the person who sent it to him and to the e-mail address you typed in the CC column. In addition to sending the message to the recipient, it will also be sent to you in the dark Send a column of the email address, but the recipient > did not know you sent this email to the dark send a column of the e-mail address entered the person

    ' MAIL_CC ' => ',//CC ' MAIL_BCC ' => ',//densely sent
    Instantiate Phpmailer Core class//here because the index.php file already include "vendor/autoload.php", there is no need to introduce $mail = new Phpmailer;
    Send mail using SMTP authentication $mail->issmtp ();
    Link QQ domain name Mailbox server address $mail->host = $config [' Mail_host '];
    SMTP requires authentication this must be true $mail->smtpauth = $config [' Mail_smtpauth '];
    SMTP login account here fill in the string format QQ number can $mail->username = $config [' Mail_username '];
    The password for the SMTP login uses the generated authorization code $mail->password = $config [' Mail_password '];
    Set logon authentication using SSL encryption $mail->smtpsecure = ' SSL ';
    Set the remote server port number for SSL connection SMTP server optional 465 or 587 $mail->port = $config [' Mail_port '];
    Set the encoding of the sent message optional GB2312 I like utf-8. It is said that UTF8 in some clients will be garbled $mail->charset = $config [' Mail_charset '];
    $mail->setfrom ($config [' Mail_from '], $config [' mail_fromname ']); Set up CollectionPerson's mailbox address the method has two parameters the first parameter is the recipient mailbox address the second parameter is the nickname that is set to the address, and the mailbox system will automatically handle the change. The second parameter is not meaningful here//Add multiple recipients then call the method multiple times//$mail->addad
    Dress (' xxx@163.com ', ' jingjing online user ');
    foreach ($to as $val) {$mail->addaddress ($val);
    //Set $mail->addreplyto ($config [' Mail_replyto ']) of the user replying to the mailbox;

    Set the mailbox $mail->addreplyto ($config [' Mail_replyto ']) of the user's reply;
    Set cc person $mail->addcc ($config [' mail_cc ']);

The Mail header does not display the secret send information $mail->addbcc ($config [' mail_bcc ']);         $mail->addattachment ('/var/tmp/file.tar.gz ');    Add Accessories//$mail->addattachment ('/tmp/image.jpg ', ' new.jpg ');

    Optional name//message body is HTML encoding Note that this is a method that is no longer a property true or False $mail->ishtml ($config [' mail_ishtml ']);
    Add the subject of this message $mail->subject = $title;
    Add ishtml to True at the top of the message body, you can complete an HTML string such as: Use the file_get_contents function > read the local HTML file $mail->body = $content;

    Call $mail->altbody = Strip_tags ($content) when the ishtml is set to false above the body of the message being added. if (! $mail->send (){throw new \exception (' Send mail failed. Please check the related configuration.
    '); }}?>

OK, now we can use SendMail () to send an email in index.php.

#index. php file

//Add Public Function library file
require "function.php";

Add automatic load Support
require "vendor/autoload.php";

try{
    $users = Array (' 11111111@qq.com ', ' 22222222@163.com ');
    $title = ' Test title ';
    $content = ' Test mailbox contents. ';
    SendMail ($users, $title, $content);
catch (Exception $e) {
    var_dump ($e->getmessage ());
}

By this, our entire blog will be over. If you fail to send an email, the first is to check the configuration. The second is to review my last blog, "PHP use Phpmailer with QQ mailbox to achieve email", which mentioned the notice.

For a more detailed description of the installation and use of composer, you can visit its Chinese official website composer Chinese web

For more information on the Phpmailer package, please visit Phpmailer/phpmailer, and I have a considerable portion of the code copied above.

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.