Skip to content

Instantly share code, notes, and snippets.

@Zegnat Zegnat/Mail.php
Created Mar 25, 2017

Embed
What would you like to do?
Dirty EmailSenderInterface implementation for Omnimail.
<?php
namespace Zegnat\Omnimail;
use Omnimail\EmailSenderInterface;
use Omnimail\EmailInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
class Mail implements EmailSenderInterface
{
private $additional_parameters = null;
private $logger;
public function __construct($additional_parameters = null, LoggerInterface $logger = null) {
$this->additional_parameters = $additional_parameters;
if ($logger === null) {
$this->logger = new NullLogger();
} else {
$this->logger = $logger;
}
}
public function send(EmailInterface $email) {
$headers = array_filter(
[
'From' => $this->dirtyStringification([$email->getFrom()]),
'Reply-To' => $this->dirtyStringification($email->getReplyTos()),
'Cc' => $this->dirtyStringification($email->getCcs()),
'Bcc' => $this->dirtyStringification($email->getBccs()),
],
function ($value) {
return is_string($value) && strlen($value) > 0;
}
);
$headers = array_map(
function ($header) use ($headers) {
return $header . ': ' . $headers[$header];
},
array_keys($headers)
);
$send = mail(
$this->dirtyStringification($email->getTos()),
$email->getSubject(),
$email->getTextBody(),
implode("\r\n", $headers),
$this->additional_parameters
);
}
private function dirtyStringification(array $email_adresses) {
$collection = [];
foreach ($email_adresses as $email_adress) {
$collection[] = $email_adress['email'];
}
return implode(', ', $collection);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.