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\SendGrid\Mail; |
13
|
|
|
|
14
|
|
|
use Mautic\EmailBundle\Helper\PlainTextMessageHelper; |
15
|
|
|
use SendGrid\Content; |
16
|
|
|
use SendGrid\Email; |
17
|
|
|
use SendGrid\Mail; |
18
|
|
|
|
19
|
|
|
class SendGridMailBase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var PlainTextMessageHelper |
23
|
|
|
*/ |
24
|
|
|
private $plainTextMessageHelper; |
25
|
|
|
|
26
|
|
|
public function __construct(PlainTextMessageHelper $plainTextMessageHelper) |
27
|
|
|
{ |
28
|
|
|
$this->plainTextMessageHelper = $plainTextMessageHelper; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return Mail |
33
|
|
|
*/ |
34
|
|
|
public function getSendGridMail(\Swift_Mime_SimpleMessage $message) |
35
|
|
|
{ |
36
|
|
|
$froms = $message->getFrom(); |
37
|
|
|
$from = new Email(current($froms), key($froms)); |
|
|
|
|
38
|
|
|
$subject = $message->getSubject(); |
39
|
|
|
|
40
|
|
|
$contentMain = new Content($this->getContentType($message), $message->getBody()); |
41
|
|
|
$contentSecond = null; |
42
|
|
|
|
43
|
|
|
// Plain text message must be first if present |
44
|
|
|
if ('text/plain' !== $contentMain->getType()) { |
45
|
|
|
$plainText = $this->plainTextMessageHelper->getPlainTextFromMessageNotStatic($message); |
46
|
|
|
if ($plainText) { |
47
|
|
|
$contentSecond = $contentMain; |
48
|
|
|
$contentMain = new Content('text/plain', $plainText); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// Sendgrid class requires to pass an TO email even if we do not have any general one |
53
|
|
|
// Pass a dummy email and clear it in the next 2 lines |
54
|
|
|
$to = '[email protected]'; |
55
|
|
|
$mail = new Mail($from, $subject, $to, $contentMain); |
56
|
|
|
$mail->personalization = []; |
57
|
|
|
|
58
|
|
|
if ($contentSecond) { |
59
|
|
|
$mail->addContent($contentSecond); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $mail; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
private function getContentType(\Swift_Mime_SimpleMessage $message) |
69
|
|
|
{ |
70
|
|
|
return 'text/plain' === $message->getContentType() ? $message->getContentType() : 'text/html'; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|