-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwiftMailer.php
64 lines (52 loc) · 2.04 KB
/
SwiftMailer.php
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
<?php
/**
* Leafpub: Simple, beautiful publishing. (https://leafpub.org)
*
* @link https://github.com/Leafpub/leafpub
* @copyright Copyright (c) 2016 Leafpub Team
* @license https://github.com/Leafpub/leafpub/blob/master/LICENSE.md (GPL License)
*/
namespace Leafpub\Plugins\SwiftMailer;
require __DIR__ . '/lib/swift_required.php';
use Leafpub\Leafpub,
Leafpub\Mailer\Bridge\MailerInterface;
class SwiftMailer implements MailerInterface {
public function send(\Leafpub\Mailer\Mail\Mail $mail){
try {
$message = \Swift_Message::newInstance();
$message->setSubject($mail->subject);
$message->setBody($mail->message);
$message->setTo($mail->to->getEmail());
$message->setFrom($mail->from->getEmail());
$transport = $this->getTransport();
$mailer = \Swift_Mailer::newInstance($transport);
return $mailer->send($message);
} catch (Exception $e){
return false;
}
}
private function getTransport(){
$options = Plugin::getSwiftOptions();
$transport = null;
switch ($options['SwiftMailer.transport']){
case 'smtp':
$transport = \Swift_SmtpTransport::newInstance();
$transport->setHost($options['SwiftMailer.host']);
$transport->setPort($options['SwiftMailer.port']);
$transport->setUser($options['SwiftMailer.user']);
$transport->setPass($options['SwiftMailer.pass']);
if ($options['SwiftMailer.encryption'] !== null){
$transport->setEncryption($options['SwiftMailer.encryption']);
}
break;
case 'sendmail':
$transport = \Swift_SendmailTransport::newInstance();
break;
case 'mail':
$transport = \Swift_MailTransport::newInstance();
break;
}
// Check Settings table for options
return $transport;
}
}