summaryrefslogtreecommitdiff
path: root/app/Integration/Smtp.php
blob: ad2f30f84cb18df6aec22667ef9419ee8d30c567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

namespace Integration;

use Swift_Message;
use Swift_Mailer;
use Swift_MailTransport;
use Swift_SendmailTransport;
use Swift_SmtpTransport;
use Swift_TransportException;

/**
 * Smtp
 *
 * @package  integration
 * @author   Frederic Guillot
 */
class Smtp extends \Core\Base
{
    /**
     * Send a HTML email
     *
     * @access public
     * @param  string  $email
     * @param  string  $name
     * @param  string  $subject
     * @param  string  $html
     * @param  string  $author
     */
    public function sendEmail($email, $name, $subject, $html, $author)
    {
        try {

            $message = Swift_Message::newInstance()
                ->setSubject($subject)
                ->setFrom(array(MAIL_FROM => $author))
                ->setBody($html, 'text/html')
                ->setTo(array($email => $name));

            Swift_Mailer::newInstance($this->getTransport())->send($message);
        }
        catch (Swift_TransportException $e) {
            $this->container['logger']->error($e->getMessage());
        }
    }

    /**
     * Get SwiftMailer transport
     *
     * @access private
     * @return \Swift_Transport
     */
    private function getTransport()
    {
        switch (MAIL_TRANSPORT) {
            case 'smtp':
                $transport = Swift_SmtpTransport::newInstance(MAIL_SMTP_HOSTNAME, MAIL_SMTP_PORT);
                $transport->setUsername(MAIL_SMTP_USERNAME);
                $transport->setPassword(MAIL_SMTP_PASSWORD);
                $transport->setEncryption(MAIL_SMTP_ENCRYPTION);
                break;
            case 'sendmail':
                $transport = Swift_SendmailTransport::newInstance(MAIL_SENDMAIL_COMMAND);
                break;
            default:
                $transport = Swift_MailTransport::newInstance();
        }

        return $transport;
    }
}