|
1
|
|
|
<?php namespace Tequilarapido\Helpers; |
|
2
|
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
|
4
|
|
|
use Swift_MailTransport; |
|
5
|
|
|
use Swift_Mailer; |
|
6
|
|
|
use Swift_Message; |
|
7
|
|
|
use Tequilarapido\Cli\Config\Config; |
|
8
|
|
|
|
|
9
|
|
|
class MailHelper |
|
10
|
|
|
{ |
|
11
|
|
|
protected $config; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(Config $config) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->config = $config; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $commandName |
|
20
|
|
|
*/ |
|
21
|
|
|
public function send($mail, $commandName) |
|
22
|
|
|
{ |
|
23
|
|
|
// to & from |
|
24
|
|
|
$from = $this->config->getNotificationFrom(); |
|
25
|
|
|
$to = $this->config->getNotificationTo(); |
|
26
|
|
|
|
|
27
|
|
|
// Mail content |
|
28
|
|
|
$givenSubject = isset($mail['subject']) ? $mail['subject'] : ''; |
|
29
|
|
|
$subject = '[' . $this->config->getProject() . '][' . $commandName . ']' . ' : ' . $givenSubject; |
|
30
|
|
|
|
|
31
|
|
|
$givenBody = isset($mail['body']) ? $mail['body'] : ''; |
|
32
|
|
|
$body = array(); |
|
33
|
|
|
$body[] = $givenBody; |
|
34
|
|
|
$body[] = 'Command run at : ' . Carbon::now()->format('Y-m-d H:i:s'); |
|
35
|
|
|
$body[] = 'Using file : ' . $this->config->getFile(); |
|
36
|
|
|
$body = implode("\n", $body); |
|
37
|
|
|
|
|
38
|
|
|
// Transport |
|
39
|
|
|
$transport = $this->getMailTransportFromConfiguration(); |
|
40
|
|
|
|
|
41
|
|
|
// Send |
|
42
|
|
|
$this->sendMessageUsingTransport($transport, $from, $to, $subject, $body); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function getMailTransportFromConfiguration() |
|
46
|
|
|
{ |
|
47
|
|
|
$transport = null; |
|
48
|
|
|
$notificationTransport = $this->config->getNotificationTransport(); |
|
49
|
|
|
|
|
50
|
|
|
// Try SMTP if it correctly configured |
|
51
|
|
|
if ( |
|
52
|
|
|
!is_null($notificationTransport) && |
|
53
|
|
|
!empty($notificationTransport->type) && |
|
54
|
|
|
$notificationTransport->type === 'smtp' && |
|
55
|
|
|
!empty($notificationTransport->parameters->host) && |
|
56
|
|
|
!empty($notificationTransport->parameters->port) |
|
57
|
|
|
) { |
|
58
|
|
|
$transport = \Swift_SmtpTransport::newInstance($notificationTransport->parameters->host, $notificationTransport->parameters->port); |
|
59
|
|
|
|
|
60
|
|
|
// Username ? |
|
61
|
|
|
if (!empty($notificationTransport->parameters->username)) { |
|
62
|
|
|
$transport->setUsername($notificationTransport->parameters->username); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Username ? |
|
66
|
|
|
if (!empty($notificationTransport->parameters->password)) { |
|
67
|
|
|
$transport->setPassword($notificationTransport->parameters->password); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Otherwise just try default server transport |
|
72
|
|
|
if (is_null($transport)) { |
|
73
|
|
|
$transport = Swift_MailTransport::newInstance(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $transport; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $subject |
|
81
|
|
|
* @param string $body |
|
82
|
|
|
*/ |
|
83
|
|
|
private function sendMessageUsingTransport($transport, $from, $to, $subject, $body) |
|
84
|
|
|
{ |
|
85
|
|
|
$message = Swift_Message::newInstance(); |
|
86
|
|
|
$message->setFrom($from); |
|
87
|
|
|
$message->setTo($to); |
|
88
|
|
|
$message->setSubject($subject); |
|
89
|
|
|
$message->setBody($body); |
|
90
|
|
|
|
|
91
|
|
|
$mailer = Swift_Mailer::newInstance($transport); |
|
92
|
|
|
$mailer->send($message); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |