bbone/application/helpers/mail_helper.php

152 lines
5.2 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use PHPMailer\PHPMailer\PHPMailer;
if (!function_exists('send_emails')) {
function send_emails($to, $subject, $body, $aliasName = '', $cc = null, $bcc = null, $receipt = null)
{
log_message('error', 'Fns send_emails - helper_loaded');
log_message('error', 'Receiptent mail - ' . $to . ' - subject - ' . $subject . ' - body - ' . $body);
log_message('error', 'Sender mail ' . business()->sender_mail);
try {
$mailer = new PHPMailer(true);
log_message('error', 'phpmailer loaded');
$mailer->isSMTP();
$mailer->CharSet = 'utf-8';
$mailer->Host = business()->mail_hostname;
$mailer->SMTPSecure = business()->mail_security;
$mailer->SMTPAuth = true;
$mailer->SMTPDebug = false;
$mailer->Username = business()->mail_username;
$mailer->Password = business()->mail_password;
$mailer->Port = business()->mail_port_no;
$mailer->setFrom(business()->sender_mail, $aliasName);
$mailer->addAddress($to);
// Add CC recipients
// if ($cc !== null) {
// if (is_array($cc)) {
// foreach ($cc as $ccRecipient) {
// $mailer->addCC($ccRecipient);
// }
// } else {
// $mailer->addCC($cc);
// }
// }
// Add BCC recipients
// if ($bcc !== null) {
// if (is_array($bcc)) {
// foreach ($bcc as $bccRecipient) {
// $mailer->addBCC($bccRecipient);
// }
// } else {
// $mailer->addBCC($bcc);
// }
// }
$mailer->Subject = $subject;
$mailer->isHTML(true);
$mailer->Body = $body;
$attachmentPath = '/path/to/your/attachment/' . $receipt;
if (file_exists($attachmentPath)) {
$mailer->addAttachment($attachmentPath);
}
if ($mailer->send()) {
log_message('error', 'Email sent successfully');
} else {
throw new \Exception('Email sending failed.');
}
} catch (\Exception $e) {
log_message('error', 'Error mail noy send: ' . $mailer->ErrorInfo);
log_message('error', 'Email Error = ' . $e->getMessage());
}
return true;
}
}
if (!function_exists('send_emails_forgot_password')) {
function send_emails_forgot_password($to, $subject, $body, $mailCredentials = null, $aliasName = '', $cc = null, $bcc = null, $receipt = null)
{
log_message('error', 'send_mail_helper_loadded');
log_message('error', 'Receiptent mail - ' . $to . ' - subject - ' . $subject . ' - body - ' . $body);
log_message('error', 'Sender mail ' . $mailCredentials->sender_mail);
try {
$mailer = new PHPMailer(true);
log_message('error', 'phpmailer loaded');
$mailer->isSMTP();
$mailer->CharSet = 'utf-8';
$mailer->Host = $mailCredentials->mail_hostname;
$mailer->SMTPSecure = $mailCredentials->mail_security;
$mailer->SMTPAuth = true;
$mailer->SMTPDebug = false;
$mailer->Username = $mailCredentials->mail_username;
$mailer->Password = $mailCredentials->mail_password;
$mailer->Port = $mailCredentials->mail_port_no;
$mailer->setFrom($mailCredentials->sender_mail, $aliasName);
$mailer->addAddress($to);
// Add CC recipients
if ($cc !== null) {
if (is_array($cc)) {
foreach ($cc as $ccRecipient) {
$mailer->addCC($ccRecipient);
}
} else {
$mailer->addCC($cc);
}
}
// Add BCC recipients
if ($bcc !== null) {
if (is_array($bcc)) {
foreach ($bcc as $bccRecipient) {
$mailer->addBCC($bccRecipient);
}
} else {
$mailer->addBCC($bcc);
}
}
$mailer->Subject = $subject;
$mailer->isHTML(true);
$mailer->Body = $body;
$attachmentPath = '/path/to/your/attachment/' . $receipt;
if (file_exists($attachmentPath)) {
$mailer->addAttachment($attachmentPath);
}
if ($mailer->send()) {
log_message('error', 'Email sent successfully');
} else {
throw new \Exception('Email sending failed.');
}
// return true;
// log_message('error', 'success : mail send successfully');
} catch (\Exception $e) {
// return 'Error: ' . $mailer->Errorerror;
log_message('error', 'Error mail noy send: ' . $mailer->ErrorInfo);
log_message('error', 'Email Error = ' . $e->getMessage());
}
return true;
}
}