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

100 lines
2.5 KiB
PHP
Raw Normal View History

2025-05-23 07:33:29 +00:00
<?php
namespace alveolata\email\implementations\pear;
require_once(DIR_ALVEOLATA . '/email/base.php');
/**
* untested!
*/
function send(
\alveolata\email\struct_credentials $credentials,
\alveolata\email\struct_data $data
) : void
{
require_once('/usr/share/php/Mail.php');
require_once('/usr/share/php/Mail/mime.php');
$auth_type_map = [
\alveolata\email\enum_auth_type::none => false,
\alveolata\email\enum_auth_type::plain => 'PLAIN',
\alveolata\email\enum_auth_type::login => 'LOGIN',
\alveolata\email\enum_auth_type::gssapi => 'GSSAPI',
\alveolata\email\enum_auth_type::digest_md5 => 'DIGEST-MD5',
\alveolata\email\enum_auth_type::cram_md5 => 'CRAM-MD5',
];
if (! \array_key_exists($credentials->auth_type, $auth_type_map)) {
throw (new \Exception(\sprintf('unsupported auth method "%s"', $credentials->auth_type)));
}
else {
$auth = $auth_type_map[$credentials->auth_type];
/**
* @see https://pear.php.net/manual/en/package.mail.mail.factory.php
*/
$sender = \Mail::factory(
'smtp',
[
'host' => $credentials->host,
'port' => \sprintf('%u', $credentials->port),
'auth' => $auth,
'username' => $credentials->username,
'password' => $credentials->password,
// 'localhost' => null,
// 'timeout' => null,
// 'verp' => null,
// 'debug' => null,
// 'persist' => null,
// 'pipelining' => null,
// 'socket_options' => null,
]
);
// headers & body
$headers_raw = [];
$headers_raw['To'] = \implode(',', $data->to);
$headers_raw['Subject'] = $data->subject;
if (! empty($data->from)) {
$headers_raw['From'] = $data->from;
}
if (! empty($data->reply_to)) {
$headers_raw['Reply-To'] = $data->reply_to;
}
if (empty($data->attachments)) {
$headers = $headers_raw;
$body = $data->body_plain;
}
else {
$mime = new \Mail_mime();
if (! empty($data->body_plain)) {
$mime->setTXTBody($data->body_plain);
}
if (! empty($data->body_html)) {
$mime->setHTMLBody($data->body_html);
}
foreach ($data->attachments as $attachment) {
$mime->addAttachment(
$attachment['path'],
($attachment['mime'] ?? 'application/octet-stream'),
($attachment['name'] ?? \basename($attachment['path']))
);
}
$headers = $mime->headers($headers_raw);
$body = $mime->get();
}
// exec
$mail = $sender->send(
\implode(',', $data->to),
$headers,
$body
);
if (\PEAR::isError($mail)) {
throw (new \Exception(\sprintf('mail_could_not_be_sent: %s', $mail->getMessage())));
}
}
}
?>