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

74 lines
1.9 KiB
PHP
Raw Normal View History

2025-05-23 07:33:29 +00:00
<?php
namespace alveolata\email\implementations\phpmailer;
require_once(DIR_ALVEOLATA . '/email/base.php');
/**
* requires composer package "phpmailer/phpmailer"
*
* @todo set auth type
* @todo attachment mapping
* @author Christian Fraß <frass@greenscale.de>
*/
function send(
\alveolata\email\struct_credentials $credentials,
\alveolata\email\struct_data $data
) : void
{
$phpmailer = new \PHPMailer\PHPMailer\PHPMailer(true);
// auth
$phpmailer->Host = $credentials->host;
$phpmailer->Port = \sprintf('%u', $credentials->port);
$phpmailer->Username = $credentials->username;
$phpmailer->Password = $credentials->password;
$phpmailer->isSMTP();
// $phpmailer->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_SERVER;
$phpmailer->SMTPAuth = ($credentials->negotiation_type !== \alveolata\email\enum_negotiation_type::none);
$phpmailer->SMTPSecure = ([
\alveolata\email\enum_negotiation_type::none => null,
\alveolata\email\enum_negotiation_type::opportunistic => \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS,
\alveolata\email\enum_negotiation_type::forced => \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS,
][$credentials->negotiation_type]);
// data
foreach ($data->to as $address) {
$phpmailer->addAddress($address);
}
$phpmailer->Subject = $data->subject;
if (! empty($data->from)) {
$phpmailer->setFrom($data->from);
}
if (! empty($data->reply_to)) {
$phpmailer->addReplyTo($data->reply_to);
}
foreach ($data->cc as $address) {
$phpmailer->addCC($address);
}
foreach ($data->bcc as $address) {
$phpmailer->addBCC($address);
}
foreach ($data->attachments as $attachment) {
$phpmailer->addAttachment($attachment);
}
// body
if (! empty($data->body_html)) {
$phpmailer->isHTML(true);
$phpmailer->Body = $data->body_html;
if (! empty($data->body_plain)) {
$phpmailer->AltBody = $data->body_plain;
}
}
else {
$phpmailer->Body = $data->body_plain;
}
// send
$phpmailer->send();
}
?>