-
Notifications
You must be signed in to change notification settings - Fork 10
Mandrill app mailer driver integration #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
0c57e66
8475d28
b0a2805
ee6825f
b530681
ec4cce8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,14 +40,22 @@ | |
] // according to Valitron doc. | ||
]; | ||
|
||
$mailerConfig = [ | ||
'mailer' => MailHandler::USE_PHPMAILER, // (or USE_POSTMARKAPP, USE_MANDRILL) | ||
// PhpMailer config. | ||
/*$mailerConfig = [ | ||
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL) | ||
'host' => 'smtp.gmail.com', | ||
'user' => 'YOUR EMAIL', | ||
'password' => 'YOUR PASSWORD', | ||
'protocol' => 'tls', | ||
'port' => 587, | ||
'attachmentsSizeLimit' => 8000000, // around 8MB. | ||
];*/ | ||
|
||
// Mandrill config. | ||
$mailerConfig = [ | ||
'mailer' => MailHandler::USE_MANDRILL, // (or USE_POSTMARKAPP, USE_MANDRILL) | ||
'password' => '_5mPSvb39BQqnA7G_dOaAA', | ||
'attachmentsSizeLimit' => 8000000, // around 8MB. | ||
]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to have another example for mandrill to keep files simple. Let's rename files as:
|
||
|
||
$fileManager = new FileManager([ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
namespace JustCoded\FormHandler\Mailer; | ||
|
||
use JustCoded\FormHandler\DataObjects\DataObject; | ||
use JustCoded\FormHandler\DataObjects\EmailAttachment; | ||
use JustCoded\FormHandler\DataObjects\MailMessage; | ||
use Mandrill; | ||
use Mandrill_Error; | ||
|
||
/** | ||
* Class MandrillMailer | ||
* | ||
* @package JustCoded\FormHandler\Mailer | ||
*/ | ||
class MandrillMailer extends DataObject implements MailerInterface | ||
{ | ||
/** | ||
* Attachments size limit | ||
* | ||
* @var int | ||
*/ | ||
protected $attachmentsSizeLimit = 8000000; | ||
|
||
/** | ||
* User from user config | ||
* | ||
* @var string | ||
*/ | ||
protected $user; | ||
|
||
/** | ||
* Password from user config | ||
* | ||
* @var string | ||
*/ | ||
protected $password; | ||
|
||
/** | ||
* List of errors | ||
* | ||
* @var array | ||
*/ | ||
protected $errors = array(); | ||
|
||
/** | ||
* Sending form | ||
* | ||
* @param MailMessage $message Mailer message | ||
* | ||
* @return array|bool | ||
*/ | ||
public function send(MailMessage $message) | ||
{ | ||
try { | ||
$mandrill = new Mandrill($this->password); | ||
|
||
$mandrillMessage = array( | ||
'html' => $message->getBody(), | ||
'text' => 'Example text content', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this? |
||
'subject' => $message->getSubject(), | ||
'from_email' => $message->getFrom()->getEmail(), | ||
'from_name' => $message->getFrom()->getName(), | ||
); | ||
|
||
// Recipients. | ||
if ($to = $message->getTo()) { | ||
$toArray = []; | ||
foreach ($to as $address) { | ||
$toArray[] = [ | ||
'email' => $address->getEmail(), | ||
'name' => $address->getName(), | ||
'type' => 'to' | ||
]; | ||
} | ||
|
||
if ($cc = $message->getCc()) { | ||
foreach ($cc as $address) { | ||
$toArray[] = [ | ||
'email' => $address->getEmail(), | ||
'name' => $address->getName(), | ||
'type' => 'cc' | ||
]; | ||
} | ||
} | ||
|
||
if ($bcc = $message->getBcc()) { | ||
foreach ($bcc as $address) { | ||
$toArray[] = [ | ||
'email' => $address->getEmail(), | ||
'name' => $address->getName(), | ||
'type' => 'bcc' | ||
]; | ||
} | ||
} | ||
|
||
$mandrillMessage['to'] = $toArray; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a bit of spaghetti code. $toArray should be defined before if() - otherwise you can get undefined var. and you can write 1 code for all parts like:
|
||
|
||
// Attachments. | ||
if (0 < $message->getAttachmentsSize() && $message->getAttachmentsSize() < $this->attachmentsSizeLimit | ||
&& $attachments = $message->getAttachments() | ||
) { | ||
$attachmentsArray = []; | ||
foreach ($attachments as $attachment) { | ||
$attachmentsArray[] = [ | ||
'type' => $attachment->type, | ||
'name' => $attachment->name, | ||
'content' => $attachment->getBase64() | ||
]; | ||
} | ||
|
||
$mandrillMessage['attachments'] = $attachmentsArray; | ||
} | ||
|
||
$async = false; | ||
$ip_pool = 'Main Pool'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is this? |
||
$send_at = date('Y-m-d h:i:s'); | ||
$result = $mandrill->messages->send($mandrillMessage, $async, $ip_pool, $send_at); | ||
|
||
return $result; | ||
} catch (Mandrill_Error $e) { | ||
$this->errors[] = 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage(); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* Getting list of errors | ||
* | ||
* @return array | ||
*/ | ||
public function getErrors() | ||
{ | ||
return $this->errors; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not password - it's apiKey!