Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services\Email;
- use Exception;
- use App\Services\EmailService;
- use PHPMailer\PHPMailer\Exception as PHPMailerException;
- /**
- * Email Workflows
- *
- * Contains specialized email sending workflows for specific use cases.
- *
- * @package App\Services\Email
- */
- class EmailWorkflows
- {
- /**
- * @var EmailService Core email service
- */
- private EmailService $emailService;
- /**
- * EmailWorkflows constructor
- *
- * @param EmailService|null $emailService Optional email service instance
- * @throws PHPMailerException
- */
- public function __construct(?EmailService $emailService = null)
- {
- $this->emailService = $emailService ?? new EmailService();
- }
- /**
- * Send a template-based email
- *
- * @param string|array $to Recipient(s)
- * @param string $subject Email subject
- * @param string $templateName Template name (without .php extension)
- * @param array $variables Variables to pass to the template
- * @param array $attachments Attachments [path => name]
- * @param array $cc CC recipients [email => name]
- * @param array $bcc BCC recipients [email => name]
- * @return bool True if email was sent successfully
- * @throws Exception If email sending fails
- */
- public function sendTemplate(
- string|array $to,
- string $subject,
- string $templateName,
- array $variables = [],
- array $attachments = [],
- array $cc = [],
- array $bcc = []
- ): bool
- {
- // Get the template content
- $body = $this->emailService->getTemplateRenderer()->render($templateName, $variables);
- // Validate HTML structure
- $body = $this->emailService->getHtmlValidator()->validate($body);
- // Generate a plain text version (improved conversion)
- $plainText = $this->emailService->getTemplateRenderer()->htmlToPlainText($body);
- // Send the email
- return $this->emailService->send($to, $subject, $body, $plainText, $attachments, $cc, $bcc);
- }
- /**
- * Send a contact form email
- *
- * @param string $name Sender's name
- * @param string $email Sender's email
- * @param string $subject Email subject
- * @param string $message Email message
- * @param string $toEmail Recipient email
- * @param string $toName Recipient name
- * @return bool True if email was sent successfully
- * @throws Exception If email sending fails
- */
- public function sendContactForm(
- string $name,
- string $email,
- string $subject,
- string $message,
- string $toEmail = '[email protected]',
- string $toName = 'Support Team'
- ): bool
- {
- // Override reply-to with the sender's email
- $this->emailService->setReplyTo($email, $name);
- // Include original sender information in email
- $emailSubject = "Contact Form: " . $subject;
- // Send the email using the template system
- return $this->sendTemplate(
- [$toEmail => $toName],
- $emailSubject,
- 'contact_form',
- [
- 'name' => $name,
- 'email' => $email,
- 'subject' => $subject,
- 'message' => $message
- ]
- );
- }
- /**
- * Send an auto-reply email to someone who submitted the contact form
- *
- * @param string $name Recipient's name
- * @param string $email Recipient's email
- * @param string $subject Original subject
- * @return bool True if email was sent successfully
- * @throws Exception
- */
- public function sendAutoReply(string $name, string $email, string $subject): bool
- {
- // Reset mailer for a new message
- $this->emailService->resetMailer();
- // Use the template system for the auto-reply
- return $this->sendTemplate(
- $email,
- 'Thank you for contacting ' . APP_NAME,
- 'contact_auto_reply',
- [
- 'name' => $name,
- 'subject' => $subject
- ]
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement