1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Ninja/Mailers/Mailer.php

235 lines
8.4 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-03-16 22:45:25 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Ninja\Mailers;
use App\Models\Invoice;
2015-07-02 22:21:29 +02:00
use Exception;
2015-03-16 22:45:25 +01:00
use Mail;
2017-01-30 20:40:43 +01:00
use Utils;
2018-02-22 23:02:44 +01:00
use Postmark\PostmarkClient;
use Postmark\Models\PostmarkException;
use Postmark\Models\PostmarkAttachment;
2015-03-16 22:45:25 +01:00
/**
2017-01-30 20:40:43 +01:00
* Class Mailer.
*/
2015-03-16 22:45:25 +01:00
class Mailer
{
/**
* @param $toEmail
* @param $fromEmail
* @param $fromName
* @param $subject
* @param $view
* @param array $data
2017-01-30 20:40:43 +01:00
*
* @return bool|string
*/
2015-03-16 22:45:25 +01:00
public function sendTo($toEmail, $fromEmail, $fromName, $subject, $view, $data = [])
{
2016-01-11 12:07:41 +01:00
// don't send emails to dummy addresses
2015-10-20 19:12:34 +02:00
if (stristr($toEmail, '@example.com')) {
return true;
}
2016-11-06 14:21:09 +01:00
2017-07-04 22:57:16 +02:00
$views = [
'emails.'.$view.'_html',
'emails.'.$view.'_text',
];
2015-03-16 22:45:25 +01:00
2018-02-22 23:02:44 +01:00
$toEmail = strtolower($toEmail);
$replyEmail = $fromEmail;
$fromEmail = CONTACT_EMAIL;
2018-04-09 21:50:50 +02:00
if (Utils::isSelfHost() && config('app.debug')) {
\Log::info("Sending email - To: {$toEmail} | Reply: {$replyEmail} | From: $fromEmail");
}
2018-02-22 23:02:44 +01:00
// Optionally send for alternate domain
if (! empty($data['fromEmail'])) {
$fromEmail = $data['fromEmail'];
}
if (config('services.postmark')) {
return $this->sendPostmarkMail($toEmail, $fromEmail, $fromName, $replyEmail, $subject, $views, $data);
} else {
return $this->sendLaravelMail($toEmail, $fromEmail, $fromName, $replyEmail, $subject, $views, $data);
}
}
private function sendLaravelMail($toEmail, $fromEmail, $fromName, $replyEmail, $subject, $views, $data = [])
{
if (Utils::isSelfHost()) {
if (isset($data['account'])) {
$account = $data['account'];
if (env($account->id . '_MAIL_FROM_ADDRESS')) {
$fields = [
'driver',
'host',
'port',
'from.address',
'from.name',
'encryption',
'username',
'password',
];
foreach ($fields as $field) {
$envKey = strtoupper(str_replace('.', '_', $field));
if ($value = env($account->id . '_MAIL_' . $envKey)) {
config(['mail.' . $field => $value]);
}
}
2018-05-08 09:56:05 +02:00
2018-05-08 10:59:18 +02:00
$fromEmail = config('mail.from.address');
2018-05-08 09:56:05 +02:00
$app = \App::getInstance();
$app->singleton('swift.transport', function ($app) {
return new \Illuminate\Mail\TransportManager($app);
});
$mailer = new \Swift_Mailer($app['swift.transport']->driver());
Mail::setSwiftMailer($mailer);
}
}
}
2015-07-02 22:21:29 +02:00
try {
2018-02-22 23:02:44 +01:00
$response = Mail::send($views, $data, function ($message) use ($toEmail, $fromEmail, $fromName, $replyEmail, $subject, $data) {
2015-09-17 21:01:06 +02:00
$message->to($toEmail)
->from($fromEmail, $fromName)
->replyTo($replyEmail, $fromName)
->subject($subject);
2017-01-06 14:24:53 +01:00
// Optionally BCC the email
2017-01-30 20:40:43 +01:00
if (! empty($data['bccEmail'])) {
$message->bcc($data['bccEmail']);
2017-01-06 14:24:53 +01:00
}
2018-01-15 07:05:07 +01:00
// Handle invoice attachments
2017-01-30 20:40:43 +01:00
if (! empty($data['pdfString']) && ! empty($data['pdfFileName'])) {
2015-10-13 09:11:44 +02:00
$message->attachData($data['pdfString'], $data['pdfFileName']);
}
2018-01-15 07:05:07 +01:00
if (! empty($data['ublString']) && ! empty($data['ublFileName'])) {
$message->attachData($data['ublString'], $data['ublFileName']);
}
2017-01-30 20:40:43 +01:00
if (! empty($data['documents'])) {
2017-01-30 17:05:31 +01:00
foreach ($data['documents'] as $document) {
$message->attachData($document['data'], $document['name']);
}
}
2015-07-02 22:21:29 +02:00
});
2015-10-11 16:41:09 +02:00
2018-02-22 23:02:44 +01:00
return $this->handleSuccess($data);
} catch (Exception $exception) {
return $this->handleFailure($data, $exception->getMessage());
}
}
private function sendPostmarkMail($toEmail, $fromEmail, $fromName, $replyEmail, $subject, $views, $data = [])
{
$htmlBody = view($views[0], $data)->render();
$textBody = view($views[1], $data)->render();
$attachments = [];
if (isset($data['account'])) {
$account = $data['account'];
$logoName = $account->getLogoName();
2018-02-26 09:31:12 +01:00
if (strpos($htmlBody, 'cid:' . $logoName) !== false && $account->hasLogo()) {
$attachments[] = PostmarkAttachment::fromFile($account->getLogoPath(), $logoName, null, 'cid:' . $logoName);
}
}
if (strpos($htmlBody, 'cid:invoiceninja-logo.png') !== false) {
$attachments[] = PostmarkAttachment::fromFile(public_path('images/invoiceninja-logo.png'), 'invoiceninja-logo.png', null, 'cid:invoiceninja-logo.png');
$attachments[] = PostmarkAttachment::fromFile(public_path('images/emails/icon-facebook.png'), 'icon-facebook.png', null, 'cid:icon-facebook.png');
$attachments[] = PostmarkAttachment::fromFile(public_path('images/emails/icon-twitter.png'), 'icon-twitter.png', null, 'cid:icon-twitter.png');
$attachments[] = PostmarkAttachment::fromFile(public_path('images/emails/icon-github.png'), 'icon-github.png', null, 'cid:icon-github.png');
2018-02-22 23:02:44 +01:00
}
// Handle invoice attachments
if (! empty($data['pdfString']) && ! empty($data['pdfFileName'])) {
2018-02-25 22:03:29 +01:00
$attachments[] = PostmarkAttachment::fromRawData($data['pdfString'], $data['pdfFileName']);
2018-02-22 23:02:44 +01:00
}
if (! empty($data['ublString']) && ! empty($data['ublFileName'])) {
2018-02-25 22:03:29 +01:00
$attachments[] = PostmarkAttachment::fromRawData($data['ublString'], $data['ublFileName']);
2018-02-22 23:02:44 +01:00
}
if (! empty($data['documents'])) {
foreach ($data['documents'] as $document) {
$attachments[] = PostmarkAttachment::fromRawData($document['data'], $document['name']);
}
}
try {
$client = new PostmarkClient(config('services.postmark'));
$message = [
'To' => $toEmail,
2018-04-25 14:50:32 +02:00
'From' => sprintf('"%s" <%s>', addslashes($fromName), $fromEmail),
2018-02-22 23:02:44 +01:00
'ReplyTo' => $replyEmail,
'Subject' => $subject,
'TextBody' => $textBody,
'HtmlBody' => $htmlBody,
'Attachments' => $attachments,
];
if (! empty($data['bccEmail'])) {
$message['Bcc'] = $data['bccEmail'];
}
2018-03-29 21:20:29 +02:00
if (! empty($data['tag'])) {
$message['Tag'] = $data['tag'];
2018-02-25 09:47:15 +01:00
}
2018-02-22 23:02:44 +01:00
$response = $client->sendEmailBatch([$message]);
if ($messageId = $response[0]->messageid) {
return $this->handleSuccess($data, $messageId);
} else {
return $this->handleFailure($data, $response[0]->message);
}
} catch (PostmarkException $exception) {
return $this->handleFailure($data, $exception->getMessage());
2015-07-12 21:43:45 +02:00
} catch (Exception $exception) {
2018-02-22 23:02:44 +01:00
Utils::logError(Utils::getErrorString($exception));
throw $exception;
2015-10-11 16:41:09 +02:00
}
}
/**
* @param $response
* @param $data
2017-01-30 20:40:43 +01:00
*
* @return bool
*/
2018-02-22 23:02:44 +01:00
private function handleSuccess($data, $messageId = false)
2015-10-11 16:41:09 +02:00
{
if (isset($data['invitation'])) {
$invitation = $data['invitation'];
2015-10-29 15:42:05 +01:00
$invoice = $invitation->invoice;
2017-01-30 20:40:43 +01:00
$notes = isset($data['notes']) ? $data['notes'] : false;
2018-02-12 10:23:16 +01:00
2018-02-13 20:27:00 +01:00
if (! empty($data['proposal'])) {
2018-02-12 10:23:16 +01:00
$invitation->markSent($messageId);
} else {
$invoice->markInvitationSent($invitation, $messageId, true, $notes);
}
2015-07-02 22:21:29 +02:00
}
2016-11-06 14:21:09 +01:00
2015-10-11 16:41:09 +02:00
return true;
}
/**
* @param $exception
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2018-02-22 23:02:44 +01:00
private function handleFailure($data, $emailError)
2015-10-11 16:41:09 +02:00
{
if (isset($data['invitation'])) {
$invitation = $data['invitation'];
$invitation->email_error = $emailError;
$invitation->save();
2017-01-30 17:05:31 +01:00
} elseif (! Utils::isNinjaProd()) {
2018-02-22 23:02:44 +01:00
Utils::logError($emailError);
2015-10-11 16:41:09 +02:00
}
return $emailError;
2015-03-16 22:45:25 +01:00
}
}