public function create(MessageInterface $message): Email
{
$email = new Email();
$from = $this->convertStringsToAddresses($message->getFrom());
if ($from !== null) {
$email = $email->from(...$from);
}
$to = $this->convertStringsToAddresses($message->getTo());
if ($to !== null) {
$email = $email->to(...$to);
}
$replyTo = $this->convertStringsToAddresses($message->getReplyTo());
if ($replyTo !== null) {
$email = $email->replyTo(...$replyTo);
}
$cc = $this->convertStringsToAddresses($message->getCc());
if ($cc !== null) {
$email = $email->cc(...$cc);
}
$bcc = $this->convertStringsToAddresses($message->getBcc());
if ($bcc !== null) {
$email = $email->bcc(...$bcc);
}
$subject = $message->getSubject();
if ($subject !== null) {
$email = $email->subject($subject);
}
$priority = $message->getPriority();
if ($priority !== null) {
$email = $email->priority($priority->value);
}
$charset = $message->getCharset();
$textBody = $message->getTextBody();
if ($textBody !== null) {
$email = $email->text($textBody, $charset ?? 'utf-8');
}
$htmlBody = $message->getHtmlBody();
if ($htmlBody !== null) {
$email = $email->html($htmlBody, $charset ?? 'utf-8');
}
$returnPath = $message->getReturnPath();
if ($returnPath !== null) {
$email->returnPath($returnPath);
}
$sender = $message->getSender();
if ($sender !== null) {
$email->sender($sender);
}
$date = $message->getDate();
if ($date !== null) {
$email->date($date);
}
$emailHeaders = $email->getHeaders();
foreach ($message->getHeaders() ?? [] as $name => $values) {
foreach ($values as $value) {
match ($name) {
'Date' => $emailHeaders->addDateHeader($name, new DateTimeImmutable($value)),
'Message-ID' => $emailHeaders->addIdHeader($name, $value),
default => $emailHeaders->addTextHeader($name, $value),
};
}
}
foreach ($message->getAttachments() ?? [] as $file) {
$email->addPart(
$this->createDataPartFromFile($file)
);
}
foreach ($message->getEmbeddings() ?? [] as $file) {
$email->addPart(
$this->createDataPartFromFile($file)->asInline()
);
}
return $email;
}
Signup or Login in order to comment.