rosavox/lib/alveolata/email/implementation-swift/functions.php

69 lines
1.7 KiB
PHP
Raw Normal View History

2025-05-23 07:33:29 +00:00
<?php
namespace alveolata\email\implementations\swift;
require_once(DIR_ALVEOLATA . '/email/base.php');
/**
* requires composer package "swiftmailer/swiftmailer"
*
* @todo set auth type
* @todo check for availability at start
* @author Christian Fraß <frass@greenscale.de>
*/
function send(
\alveolata\email\struct_credentials $credentials,
\alveolata\email\struct_data $data
) : void
{
// Create the Transport
$transport = new \Swift_SmtpTransport(
$credentials->host,
$credentials->port,
[
\alveolata\email\enum_negotiation_type::none => null,
\alveolata\email\enum_negotiation_type::opportunistic => 'tls',
\alveolata\email\enum_negotiation_type::forced => 'ssl',
][$credentials->negotiation_type]
);
$transport->setUsername($credentials->username);
$transport->setPassword($credentials->password);
// Create the Mailer using your created Transport
$mailer = new \Swift_Mailer($transport);
// Create a message
$message = new \Swift_Message();
if (! empty($data->from)) {
$message->setFrom([$data->from]);
}
$message->setTo($data->to);
if (! empty($data->cc)) {
$message->setCc($data->cc);
}
if (! empty($data->bcc)) {
$message->setBcc($data->bcc);
}
$message->setSubject($data->subject);
if (! empty($data->body_html)) {
$message->setBody($data->body_html, 'text/html');
if (! empty($data->body_plain)) {
$message->addPart($data->body_plain, 'text/plain');
}
}
else {
$message->setBody($data->body_plain, 'text/plain');
}
foreach ($data->attachments as $attachment) {
$attachment_ = \Swift_Attachment::fromPath($attachment['path'], $attachment['mime']);
$attachment_->setFilename($attachment['name']);
$message->attach($attachment_);
}
// Send the message
$result = $mailer->send($message);
}