PHP Classes
elePHPant
Icontem

File: src/email.php

Recommend this page to a friend!
  Classes of Scott Arciszewski  >  CMS Airship  >  src/email.php  >  Download  
File: src/email.php
Role: Example script
Content type: text/plain
Description: Example script
Class: CMS Airship
Content management system with security features
Author: By
Last change: Boyscouting src/ (but not any deeper).
Add strict type declaration.
Internationalize exceptions.

Either use trk() or __() on all relevant exception
messages.
Date: 1 year ago
Size: 4,664 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

use
Airship\Engine\State;
use
ParagonIE\GPGMailer\GPGMailer;

/**
 * Set up our email configuration
 */

$email_closure = function() {
   
$state = State::instance();

   
/**
     * If this is defined elsewhere, respect it.
     * Otherwise, just use the default (sendmail):
     */
   
if (empty($state->mailer)) {
        if (empty(
$state->universal['email']['transport'])) {
           
// Sane default
           
$transport = new Zend\Mail\Transport\Sendmail();
        } else {
            switch (
$state->universal['email']['transport']) {
                case
'SMTP':
                   
$transport = new \Zend\Mail\Transport\Smtp();
                   
$transportConfig = [
                       
'name' =>
                           
$state->universal['email']['smtp']['name'],
                       
'host' =>
                           
$state->universal['email']['smtp']['host'],
                       
'connection_class' =>
                           
$state->universal['email']['smtp']['connection_class']
                    ];

                    if (
$state->universal['email']['smtp']['connection_class'] !== 'smtp') {
                       
$transportConfig['connection_config'] = [
                           
'username' =>
                               
$state->universal['email']['smtp']['username'],
                           
'password' =>
                               
$state->universal['email']['smtp']['password']
                        ];
                    }
                   
                    if (!empty(
$state->universal['email']['smtp']['disable_tls'])) {
                       
$transportConfig['connection_config']['port'] = !empty($state->universal['email']['smtp']['port'])
                            ?
$state->universal['email']['smtp']['port']
                            :
25;
                    } else {
                       
$transportConfig['connection_config']['ssl'] = 'tls';
                       
$transportConfig['port'] = !empty($state->universal['email']['smtp']['port'])
                            ?
$state->universal['email']['smtp']['port']
                            :
587;
                    }
                   
$transport->setOptions(
                        new \
Zend\Mail\Transport\SmtpOptions($transportConfig)
                    );
                    break;

                case
'File':
                   
$transport = new Zend\Mail\Transport\File();
                   
/** @noinspection PhpUnusedParameterInspection */
                   
$transport->setOptions(
                        new \
Zend\Mail\Transport\FileOptions([
                           
'path' =>
                                !empty(
$state->universal['email']['file']['path'])
                                    ?
$state->universal['email']['file']['path']
                                    :
ROOT . '/files/email',
                           
'callback' =>
                                function (
Zend\Mail\Transport\File $t): string {
                                    return \
implode(
                                       
'_',
                                        [
                                           
'Message',
                                            \
date('YmdHis'),
                                            \
Airship\uniqueId(12) . '.txt'
                                       
]
                                    );
                                }
                        ])
                    );
                    break;

                case
'Sendmail':
                    if (!empty(
$state->universal['email']['sendmail']['parameters'])) {
                       
$transport = new Zend\Mail\Transport\Sendmail(
                           
$state->universal['email']['sendmail']['parameters']
                        );
                    } else {
                       
$transport = new Zend\Mail\Transport\Sendmail();
                    }
                    break;

                default:
                    throw new
Exception(
                        \
trk(
                           
'errors.email.invalid_transport',
                            \
print_r($state->universal['email']['transport'], true)
                        )
                    );
            }
        }
       
$state->mailer = $transport;
    }

   
// Now that our mailer is set up, let's make sure GPGMailer is too.
   
$gpgMailer = new GPGMailer(
       
$state->mailer,
        [
           
'homedir' =>
               
ROOT . '/files'
       
]
    );
   
$state->gpgMailer = $gpgMailer;
};
$email_closure();
unset(
$email_closure);