Issues (3627)

Swiftmailer/Transport/ElasticemailTransport.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2014 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\EmailBundle\Swiftmailer\Transport;
13
14
use Mautic\EmailBundle\Model\TransportCallback;
15
use Mautic\LeadBundle\Entity\DoNotContact;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\Translation\TranslatorInterface;
19
20
/**
21
 * Class ElasticEmailTransport.
22
 */
23
class ElasticemailTransport extends \Swift_SmtpTransport implements CallbackTransportInterface
24
{
25
    /**
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30
    /**
31
     * @var LoggerInterface
32
     */
33
    private $logger;
34
35
    /**
36
     * @var TransportCallback
37
     */
38
    private $transportCallback;
39
40
    /**
41
     * ElasticemailTransport constructor.
42
     */
43
    public function __construct(TranslatorInterface $translator, LoggerInterface $logger, TransportCallback $transportCallback)
44
    {
45
        $this->translator        = $translator;
46
        $this->logger            = $logger;
47
        $this->transportCallback = $transportCallback;
48
49
        parent::__construct('smtp.elasticemail.com', 2525, null);
50
51
        $this->setAuthMode('login');
52
    }
53
54
    /**
55
     * @param null $failedRecipients
56
     *
57
     * @return int|void
58
     *
59
     * @throws \Exception
60
     */
61
    public function send(\Swift_Mime_SimpleMessage $message, &$failedRecipients = null)
62
    {
63
        // IsTransactional header for all non bulk messages
64
        // https://elasticemail.com/support/guides/unsubscribe/
65
        if ('Bulk' != $message->getHeaders()->get('Precedence')) {
0 ignored issues
show
The condition 'Bulk' != $message->getH...rs()->get('Precedence') is always true.
Loading history...
66
            $message->getHeaders()->addTextHeader('IsTransactional', 'True');
67
        }
68
69
        parent::send($message, $failedRecipients);
70
    }
71
72
    /**
73
     * Returns a "transport" string to match the URL path /mailer/{transport}/callback.
74
     *
75
     * @return mixed
76
     */
77
    public function getCallbackPath()
78
    {
79
        return 'elasticemail';
80
    }
81
82
    /**
83
     * Handle bounces & complaints from ElasticEmail.
84
     */
85
    public function processCallbackRequest(Request $request)
86
    {
87
        $this->logger->debug('Receiving webhook from ElasticEmail');
88
89
        $email    = rawurldecode($request->get('to'));
90
        $status   = rawurldecode($request->get('status'));
91
        $category = rawurldecode($request->get('category'));
92
        // https://elasticemail.com/support/delivery/http-web-notification
93
        if (in_array($status, ['AbuseReport', 'Unsubscribed']) || 'Spam' === $category) {
94
            $this->transportCallback->addFailureByAddress($email, $status, DoNotContact::UNSUBSCRIBED);
95
        } elseif (in_array($category, ['NotDelivered', 'NoMailbox', 'AccountProblem', 'DNSProblem', 'Unknown'])) {
96
            // just hard bounces https://elasticemail.com/support/user-interface/activity/bounced-category-filters
97
            $this->transportCallback->addFailureByAddress($email, $category);
98
        } elseif ('Error' == $status) {
99
            $this->transportCallback->addFailureByAddress($email, $this->translator->trans('mautic.email.complaint.reason.unknown'));
100
        }
101
    }
102
}
103