Skip to content

Commit

Permalink
Reformat code in mailers, update readme with replyTo
Browse files Browse the repository at this point in the history
  • Loading branch information
aprokopenko committed Jul 31, 2018
1 parent ca433df commit 56899d2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Desktop.ini

# composer itself is not needed
composer.phar
composer.lock

# phpunit itself is not needed
phpunit.phar
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ Example:
$messageConfig = [
'from' => ['noreply@my-domain.com' => 'My Domain Support'],
'to' => ['admin@my-domain.com' => 'John Doe'],
'replyTo' => ['REPLY.EMAIL@DOMAIN.COM' => 'REPLY NAME'], // OPTIONAL
'cc' => ['cc-email@gmail.com', 'more-cc@gmail.com'], // OPTIONAL
'bcc' => ['bcc-email@gmail.com'], // OPTIONAL
'subject' => 'Contact request from {name}',
Expand Down Expand Up @@ -231,6 +232,7 @@ $mailerConfig = [
$messageConfig = [
'from' => ['FROM.EMAIL@DOMAIN.COM' => 'FROM NAME'], // set correct FROM.
'to' => ['TO.EMAIL@DOMAIN.COM' => 'TO NAME'], // set correct TO.
'replyTo' => ['REPLY.EMAIL@DOMAIN.COM' => 'REPLY NAME'],// set correct REPLY.
'subject' => 'Contact request from {name}',
'bodyTemplate' => __DIR__ . '/template-html.php',
'altBodyTemplate' => __DIR__ . '/template-plain.php',
Expand Down Expand Up @@ -384,6 +386,7 @@ After that you need to specify which files should be uploaded in `$messageConfig
$messageConfig = [
'from' => ['FROM.EMAIL@DOMAIN.COM' => 'FROM NAME'], // set correct FROM.
'to' => ['TO.EMAIL@DOMAIN.COM' => 'TO NAME'], // set correct TO.
'replyTo' => ['REPLY.EMAIL@DOMAIN.COM' => 'REPLY NAME'],// set correct REPLY.
'subject' => 'Contact request from {name}',
'bodyTemplate' => __DIR__ . '/template-html.php',
'altBodyTemplate' => __DIR__ . '/template-plain.php',
Expand Down
7 changes: 3 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 18 additions & 16 deletions src/Mailer/MandrillMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,23 @@ public function send(MailMessage $message)
$mandrill = new Mandrill($this->apiKey);

$mandrillMessage = array(
'html' => $message->getBody(),
'subject' => $message->getSubject(),
'html' => $message->getBody(),
'subject' => $message->getSubject(),
'from_email' => $message->getFrom()->getEmail(),
'from_name' => $message->getFrom()->getName(),
'from_name' => $message->getFrom()->getName(),
);

$replyTo=$message->getReplyTo();
if(!empty($replyTo)) {
$mandrillMessage['headers']=['Reply-To' => $replyTo->getEmail()];

if ($replyTo = $message->getReplyTo()) {
$mandrillMessage['headers'] = [
'Reply-To' => $replyTo->getEmail(),
];
}

// Recipients.
$recipients = [
'to' => $message->getTo(),
'cc' => $message->getCc(),
'bcc' => $message->getBcc()
'to' => $message->getTo(),
'cc' => $message->getCc(),
'bcc' => $message->getBcc(),
];

$to = [];
Expand All @@ -82,8 +83,8 @@ public function send(MailMessage $message)
foreach ($emails as $email) {
$to[] = [
'email' => $email->getEmail(),
'name' => $email->getName(),
'type' => $type
'name' => $email->getName(),
'type' => $type,
];
}
}
Expand All @@ -92,14 +93,14 @@ public function send(MailMessage $message)

// Attachments.
if (0 < $message->getAttachmentsSize() && $message->getAttachmentsSize() < $this->attachmentsSizeLimit
&& $attachments = $message->getAttachments()
&& $attachments = $message->getAttachments()
) {
$attachmentsArray = [];
foreach ($attachments as $attachment) {
$attachmentsArray[] = [
'type' => $attachment->type,
'name' => $attachment->name,
'content' => $attachment->getBase64()
'type' => $attachment->type,
'name' => $attachment->name,
'content' => $attachment->getBase64(),
];
}

Expand All @@ -111,6 +112,7 @@ public function send(MailMessage $message)
return $result;
} catch (Mandrill_Error $e) {
$this->errors[] = 'A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage();

return false;
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Mailer/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ public function send(MailMessage $message)
$mail = new PHPMailerLib(true); // Passing `true` enables exceptions.
try {
// Enable SMTP if host is set.
if (!empty($this->host)) {
if (! empty($this->host)) {
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = $this->host;
$mail->Port = $this->port;
if (!empty($this->user)) {
if (! empty($this->user)) {
$mail->SMTPAuth = true;
$mail->Username = $this->user;
$mail->Password = $this->password;
}
if (!empty($this->protocol)) {
if (! empty($this->protocol)) {
$mail->SMTPSecure = $this->protocol;
}
}
Expand Down Expand Up @@ -138,13 +138,15 @@ public function send(MailMessage $message)
$mail->Body = $message->getBody();
$mail->AltBody = $message->getAltBody();
} else {
$mail->Body = $message->getAltBody();
$mail->Body = $message->getAltBody();
}

$this->errors = array();

return $mail->send();
} catch (PhpMailerException $e) {
$this->errors[] = 'Message could not be sent. Mailer Error: ' . $mail->ErrorInfo;

return false;
}
}
Expand Down

0 comments on commit 56899d2

Please sign in to comment.