Setting up PHPMailer


Apr '15

Apr '15

1

2418

2

So when i first started developing with Phalcon i struggled to make PHPMailer to work properly, but i have gone better and made it work , so i want to share how i did it . ( Keep in mind that i'm using Phalcon's Multi Module )

Step 1 : PHPMailer Files

Since my application is a multi module ( frontend and backend ) , i downloaded the PHPMailer files manually ( Just the PHPMailer.php & SMTP.php ) and put in a folder ouside my application called libraries . Structure :

| apps
| config
| libraries
    | PHPMailer
        | phpmailer.php
        | smtp.php
| public

Step 2 : Namespace PHPMailer Files

Namespace both files under Libraries\PHPMailer , or what makes sense in your application .

<?php
// phpmailer.php
namespace Libraries\PHPMailer;

class PHPMailer
{
<?php
// smtp.php
namespace Libraries\PHPMailer;

class SMTP
{

Step 3 ( optional ) : Config

In my application under the config directory , i have a config.ini for my configurations insted of a php file . I will be setting up all my database and phpmailer information here to be accessed easily . ( the email & name variables are the emails will be sent to)

[database]
adapter  = Mysql
host     = 127.0.0.1
username = root
password = 
dbname   = sandbox
charset  = utf8

[mail]                                
host         = smtp.mandrillapp.com                           
username     = sandbox@yourdomain.com              
password     = ihBxzyEihA4LlDxhshaPyQ                           
security     = tls                            
port         = 587 
charset      = UTF-8
email        = sendermail@yourdomain.com
name         = Hudson Nicoletti

Step 4 : Setting up a service

Also in my config directory i have services.php that will set up all the services in my application that i can access in all my applications ( frontend & backend ).

Since i use a INI configuration file , i will need to add the line :

    use Phalcon\Config\Adapter\Ini

Then the PHPMailer namespaces :

    use Libraries\PHPMailer\smtp,
        Libraries\PHPMailer\PHPMailer as PHPMailer;

Then i will set up the service of Mail , where the variable $cf is my ini configs and the $di is Phalcon's config :

    $di['mail'] = function () use ($cf) {
    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->isHTML(true);

    $mail->CharSet      = $cf->mail->charset;                                
    $mail->Host         = $cf->mail->host;
    $mail->SMTPAuth     = true;                               
    $mail->Username     = $cf->mail->username;               
    $mail->Password     = $cf->mail->password;                           
    $mail->SMTPSecure   = $cf->mail->security;    
    $mail->Port         = $cf->mail->port;

    $mail->addAddress($cf->mail->email, $cf->mail->name);

    return $mail;
    };

So my services.php looks like this :

<?php

use Phalcon\Mvc\Router,
    Phalcon\Mvc\Url as UrlResolver,
    Phalcon\DI\FactoryDefault,
    Phalcon\Session\Adapter\Files as SessionAdapter,
    Phalcon\Mvc\Dispatcher as PhDispatcher,
    Phalcon\Config\Adapter\Ini,
    Libraries\PHPMailer\smtp,
    Libraries\PHPMailer\PHPMailer as PHPMailer,
    Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;

$di = new FactoryDefault();
$cf = new Ini("../config/config.ini");

#   Database connection 
$di['db'] = function() use ($cf) {
    return new DbAdapter([
        "host"      => $cf->database->host,
        "username"  => $cf->database->username,
        "password"  => $cf->database->password,
        "dbname"    => $cf->database->dbname,
        "charset"   => $cf->database->charset
    ]);
};


# use phpmailer globaly
$di['mail'] = function () use ($cf) {
    $mail = new PHPMailer;

    $mail->isSMTP();
    $mail->isHTML(true);

    $mail->CharSet      = $cf->mail->charset;                                
    $mail->Host         = $cf->mail->host;
    $mail->SMTPAuth     = true;                               
    $mail->Username     = $cf->mail->username;               
    $mail->Password     = $cf->mail->password;                           
    $mail->SMTPSecure   = $cf->mail->security;    
    $mail->Port         = $cf->mail->port;

    $mail->addAddress($cf->mail->email, $cf->mail->name);

    return $mail;
};

Step 5 : Register Namespaces

I will be registering the libraries namespace in my frontend application , so the file looks like this :

    $loader->registerNamespaces([
            'Frontend\Controllers'  => __DIR__ . '/controllers/',
            'Frontend\Models'       => __DIR__ . '/models/',
            'Libraries'             => '../libraries/',
        ]);

Step 6 : TEST IT !

Now in a test controller i will send a email like so :

Obs : $this->mail is pointing to the mail service that we created , and all methods used is all the PHPMailer functions that we returned .

    public function SendmailAction()
    {   
    $this->mail->From  = "usertest@yourdomain.com";
    $this->mail->FromName   =  "user";

    $this->mail->addReplyTo("usertest@yourdomain.com", "user");

    $this->mail->Subject = "email test !";

    $this->mail->Body    = "success!";
    $this->mail->send();

            $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);

    }

Thien
8.6k

Good article :D