Files
opensourcepos/app/Libraries/Email_lib.php
Ollama 8735c0765b refactor: PSR-compliant naming and address objecttothis review comments
- Rename functions to camelCase: checkEncryption, writeEncryptionKeyToEnv, writeEncryptionKeyToWritable, loadEncryptionKeyFromWritable, abortEncryptionConversion, removeBackup, decryptValue, encryptValue
- Update all callers in Config.php, Customers.php, Migrations, Email_lib.php, Sms_lib.php, Mailchimp_lib.php
- Add EncryptionException import in security_helper.php (removed FQN)
- Use camelCase variables: $smtpPass, $emailConfig, $batchSaveData in affected files
- Remove unnecessary inline comments (code is self-documenting)
- Keep necessary docstrings for public API documentation
2026-05-26 20:54:41 +02:00

106 lines
2.9 KiB
PHP

<?php
namespace app\Libraries;
use CodeIgniter\Email\Email;
use Config\OSPOS;
/**
* Email library
*
* Library with utilities to configure and send emails
*/
class Email_lib
{
private Email $email;
private array $config;
public function __construct()
{
$this->email = new Email();
$this->config = config(OSPOS::class)->settings;
$smtpPass = decryptValue($this->config['smtp_pass'] ?? null);
$emailConfig = [
'mailType' => 'html',
'userAgent' => 'OSPOS',
'validate' => true,
'protocol' => $this->config['protocol'],
'mailPath' => $this->config['mailpath'],
'SMTPHost' => $this->config['smtp_host'],
'SMTPUser' => $this->config['smtp_user'],
'SMTPPass' => $smtpPass,
'SMTPPort' => (int)$this->config['smtp_port'],
'SMTPTimeout' => (int)$this->config['smtp_timeout'],
'SMTPCrypto' => $this->config['smtp_crypto'],
];
$this->email->initialize($emailConfig);
}
/**
* Email sending function
* Example of use: $response = sendEmail('john@doe.com', 'Hello', 'This is a message', $filename);
*/
public function sendEmail(string $to, string $subject, string $message, ?string $attachment = null): bool
{
$email = $this->email;
$email->setFrom($this->config['email'], $this->config['company']);
$email->setTo($to);
$email->setSubject($subject);
$email->setMessage($message);
if (!empty($attachment)) {
$email->attach($attachment);
$email->setAttachmentCID($attachment);
}
$result = $email->send();
if (!$result) {
log_message('error', $email->printDebugger());
}
return $result;
}
/**
* Gets the mime type of the company logo file.
*
* @return string Mime type or empty string if logo doesn't exist
*/
public function getLogoMimeType(): string
{
$logo_path = FCPATH . 'uploads/' . $this->config['company_logo'];
if (!empty($this->config['company_logo']) && file_exists($logo_path)) {
$mimeType = mime_content_type($logo_path);
return $mimeType !== false ? $mimeType : '';
}
return '';
}
/**
* Builds an img tag for the company logo to use in email templates.
*
* @return string HTML img tag with base64-encoded logo, or empty string if no logo
*/
public function buildLogoImgTag(): string
{
$mimeType = $this->getLogoMimeType();
if ($mimeType === '') {
return '';
}
$logo_path = FCPATH . 'uploads/' . $this->config['company_logo'];
$logo_data = base64_encode(file_get_contents($logo_path));
return '<img id="image" src="data:' . $mimeType . ';base64,' . $logo_data . '" alt="company_logo">';
}
}