77 lines
4.1 KiB
PHP
77 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* Mail Dispatch Helper using PHPMailer
|
|
* TPU Survey System
|
|
*/
|
|
|
|
use PHPMailer\PHPMailer\PHPMailer;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
require_once __DIR__ . '/../config/mail.php';
|
|
|
|
/**
|
|
* Sends an email with the certificate attached
|
|
*
|
|
* @param string $toEmail Recipient email address
|
|
* @param string $toName Recipient name
|
|
* @param string $pdfPath Path to the generated PDF certificate
|
|
* @param string $surveyTitle Survey title for context
|
|
* @return bool True on success
|
|
* @throws Exception on mail sending failure
|
|
*/
|
|
function sendCertificateEmail($toEmail, $toName, $pdfPath, $surveyTitle = '') {
|
|
$mail = new PHPMailer(true);
|
|
|
|
try {
|
|
// Server settings
|
|
$mail->isSMTP();
|
|
$mail->Host = SMTP_HOST;
|
|
$mail->SMTPAuth = true;
|
|
$mail->Username = SMTP_USER;
|
|
$mail->Password = SMTP_PASS;
|
|
$mail->SMTPSecure = SMTP_SECURE; // 'tls' or 'ssl'
|
|
$mail->Port = SMTP_PORT;
|
|
|
|
// UTF-8 encoding for Thai support
|
|
$mail->CharSet = 'UTF-8';
|
|
|
|
// Recipients
|
|
$mail->setFrom(MAIL_FROM_EMAIL, MAIL_FROM_NAME);
|
|
$mail->addAddress($toEmail, $toName);
|
|
|
|
// Attachments
|
|
if (!file_exists($pdfPath)) {
|
|
throw new Exception("ไม่พบไฟล์เกียรติบัตรที่จะแนบส่ง: " . $pdfPath);
|
|
}
|
|
|
|
$attachmentName = 'เกียรติบัตร_' . str_replace(' ', '_', $toName) . '.pdf';
|
|
$mail->addAttachment($pdfPath, $attachmentName);
|
|
|
|
// Content
|
|
$mail->isHTML(true);
|
|
$mail->Subject = 'เกียรติบัตรผ่านการอบรม/แบบประเมิน - ' . $surveyTitle;
|
|
|
|
$bodyHtml = "
|
|
<div style='font-family: \"Sarabun\", sans-serif; line-height: 1.6; color: #333;'>
|
|
<h2 style='color: #2563eb;'>สวัสดีคุณ {$toName},</h2>
|
|
<p>ขอขอบคุณสำหรับความคิดเห็นและความร่วมมือในการตอบแบบประเมินโครงการ/หลักสูตร <strong>\"{$surveyTitle}\"</strong></p>
|
|
<p>ทางผู้จัดงานขอมอบเกียรติบัตรผู้ผ่านการอบรมให้แก่ท่าน โดยท่านสามารถดาวน์โหลดไฟล์เกียรติบัตรที่แนบมากับอีเมลฉบับนี้ได้เลยครับ</p>
|
|
<br>
|
|
<p>ขอแสดงความนับถือ,</p>
|
|
<p><strong>ฝ่ายจัดโครงการ มหาวิทยาลัยตาปี</strong></p>
|
|
<hr style='border: 0; border-top: 1px solid #eee; margin: 20px 0;'>
|
|
<p style='font-size: 0.8rem; color: #999;'>นี่เป็นอีเมลอัตโนมัติจากระบบ กรุณาอย่าตอบกลับอีเมลฉบับนี้</p>
|
|
</div>
|
|
";
|
|
|
|
$mail->Body = $bodyHtml;
|
|
$mail->AltBody = "สวัสดีคุณ {$toName},\n\nขอขอบคุณสำหรับการตอบแบบประเมินโครงการ \"{$surveyTitle}\"\n\nทางผู้จัดงานขอมอบเกียรติบัตรผู้ผ่านการอบรมให้แก่ท่าน โดยไฟล์เกียรติบัตรถูกแนบมาพร้อมกับอีเมลนี้เรียบร้อยแล้ว\n\nขอแสดงความนับถือ,\nฝ่ายจัดโครงการ มหาวิทยาลัยตาปี";
|
|
|
|
$mail->send();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
throw new Exception("ระบบส่งอีเมลขัดข้อง: " . $mail->ErrorInfo);
|
|
}
|
|
}
|