1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Drakakisgeo\Mailtester; |
5
|
|
|
|
6
|
|
|
use RuntimeException; |
7
|
|
|
use Swift_Message; |
8
|
|
|
use Swift_Mailer; |
9
|
|
|
use Swift_Mime_MimePart; |
10
|
|
|
use Swift_SmtpTransport; |
11
|
|
|
|
12
|
|
|
trait InteractsWithSwiftEmailer |
13
|
|
|
{ |
14
|
|
|
private $emailMessage = null; |
15
|
|
|
|
16
|
|
|
public function sendMail() |
17
|
|
|
{ |
18
|
|
|
if (is_null($this->emailMessage)) { |
19
|
|
|
throw new RuntimeException('You need to create the message first and chain it.'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$transport = Swift_SmtpTransport::newInstance(getenv('MAIL_HOST'), getenv('MAIL_PORT')); |
23
|
|
|
$mailer = Swift_Mailer::newInstance($transport); |
24
|
|
|
|
25
|
|
|
if (!$mailer->send($this->emailMessage)) { |
26
|
|
|
throw new RuntimeException('Can\'t send the Email message'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$this->emailMessage = null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function buildMailMessage(array $option) |
33
|
|
|
{ |
34
|
|
|
// Set defaults |
35
|
|
|
if (!array_key_exists('from', $option)) { |
36
|
|
|
$option['from'] = ['[email protected]' => 'FromTester']; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!array_key_exists('to', $option)) { |
40
|
|
|
$option['to'] = ['[email protected]' => 'ToTester']; |
41
|
|
|
} |
42
|
|
|
if (!array_key_exists('subject', $option)) { |
43
|
|
|
$option['subject'] = 'Testing Email'; |
44
|
|
|
} |
45
|
|
|
if (!array_key_exists('contentType', $option)) { |
46
|
|
|
$option['contentType'] = 'text/html'; |
47
|
|
|
} |
48
|
|
|
if (!array_key_exists('cc', $option)) { |
49
|
|
|
$option['cc'] = []; |
50
|
|
|
} |
51
|
|
|
if (!array_key_exists('bcc', $option)) { |
52
|
|
|
$option['bcc'] = []; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Make sure Body exists |
56
|
|
|
if (!array_key_exists('body', $option)) { |
57
|
|
|
throw new RuntimeException('You really need to set the body'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->emailMessage = Swift_Message::newInstance() |
61
|
|
|
->setSubject($option['subject']) |
62
|
|
|
->setFrom($option['from']) |
63
|
|
|
->setCc($option['cc']) |
64
|
|
|
->setBcc($option['bcc']) |
65
|
|
|
->setTo($option['to']) |
66
|
|
|
->setBody($option['body'], $option['contentType']); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|