Emailer/src/Emailer.php
2025-06-21 20:58:04 +02:00

154 lines
4.4 KiB
PHP

<?php
/*
Copyright (c) TPsoft.org 2000-2025
Author: Ing. Igor Mino <mino@tpsoft.org>
License: GNU GPL-3.0 or later
Description: Simple send plain text and HTML e-mails over SMTP server with save messages in IMAP server.
Version: 1.0.0
Milestones:
2025-06-20 22:30 Igor - Forked from Beteha to standalone public library
*/
namespace TPsoft\Emailer;
use PEAR;
use Mail;
use Mail_mime;
use Exception;
class Emailer
{
private $config_smtp = array(
'host' => null,
'port' => null,
'encryption' => null,
'username' => null,
'password' => null,
);
private $config_imap = array(
'host' => null,
'port' => null,
'username' => null,
'password' => null,
'encryption' => null,
'folder' => null
);
public function __construct(array $config = array())
{
$this->config_smtp = array_merge($this->config_smtp, $config['smtp']);
$this->config_imap = array_merge($this->config_imap, $config['imap']);
}
public function send(string $send_to, string $subject, string $message_text, string $message_html): bool
{
$secure = $this->config_smtp['encryption'] ?? 'smtp';
// Odoslanie spravu
$suc = $this->smtpEmail(
$secure . '://' . $this->config_smtp['host'],
$this->config_smtp['port'],
$this->config_smtp['username'],
$this->config_smtp['password'],
$this->config_smtp['username'],
$send_to,
$subject,
$message_text,
$message_html,
null,
$message_mime
);
if ($suc != true) {
return false;
}
// Ulozenie spravy do odoslanych
if ($this->config_imap['host']) {
$suc2 = $this->imapEmailSave(
$this->config_imap['host'],
$this->config_imap['port'],
$this->config_imap['encryption'],
$this->config_imap['folder'],
$this->config_imap['username'],
$this->config_imap['password'],
$message_mime
);
if ($suc2 != true) {
return false;
}
}
return true;
}
public function smtpEmail($smtp_host, $smtp_port = 465, $smtp_username, $smtp_password, $email_from, $email_to, $subject, $message_text = null, $message_html = null, $reply_to = null, &$message_mime = null)
{
if (is_null($reply_to)) $reply_to = $email_from;
$crlf = "\n";
$hdrs = array(
'From' => $email_from,
'To' => $email_to,
'Subject' => '=?UTF-8?B?' . base64_encode($subject) . '?=',
'Reply-To' => $reply_to,
'Content-Type' => 'text/plain; charset=utf-8',
'X-Mailer' => 'TPsoft/Emailer'
);
$mime_params = array(
'eol' => $crlf,
'text_encoding' => '7bit',
'text_charset' => 'UTF-8',
'html_charset' => 'UTF-8',
'head_charset' => 'UTF-8'
);
$mime = new Mail_mime($mime_params);
if (!is_null($message_text)) $mime->setTXTBody($message_text);
if (!is_null($message_html)) $mime->setHTMLBody($message_html);
//$mime->addAttachment($file, 'text/plain');
$body = $mime->get();
$headers = $mime->headers($hdrs);
$message_mime = $mime->getMessage();
$smtp = Mail::factory('smtp', ['host' => $smtp_host, 'port' => $smtp_port, 'auth' => true, 'username' => $smtp_username, 'password' => $smtp_password]);
$mail = $smtp->send($email_to, $headers, $body);
if (PEAR::isError($mail)) {
throw new Exception($mail->getMessage());
}
return true;
}
public function imapEmailSave($imap_host, $imap_port = 993, $imap_secure = 'ssl', $imap_folder = 'Sent', $imap_username, $imap_password, $message_mime)
{
// example $path = "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail";
$path = sprintf('{%s:%s/%s}%s', $imap_host, $imap_port, $imap_secure, $imap_folder);
//Tell your server to open an IMAP connection using the same username and password as you used for SMTP
$imapStream = imap_open($path, $imap_username, $imap_password);
$result = imap_append($imapStream, $path, $message_mime);
imap_close($imapStream);
return $result;
}
public function imapList($imap_host, $imap_port = 993, $imap_secure = 'ssl', $imap_username, $imap_password)
{
$mbox = imap_open('{' . $imap_host . ':' . $imap_port . '/' . $imap_secure . '}', $imap_username, $imap_password, OP_HALFOPEN);
if (!$mbox) {
throw new Exception('Can\'t connect: '.imap_last_error());
}
$list = imap_list($mbox, '{' . $imap_host . ':' . $imap_port . '/' . $imap_secure . '}', "*");
$ret = array();
if (is_array($list)) {
foreach ($list as $val) {
$ret[] = imap_utf7_decode($val);
}
} else {
throw new Exception('imap_list failed: ' . imap_last_error());
}
imap_close($mbox);
return $ret;
}
}