Advertisement
ArcaniSGK507

Untitled

Mar 27th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services\Email;
  4.  
  5. use Exception;
  6. use App\Services\EmailService;
  7. use PHPMailer\PHPMailer\Exception as PHPMailerException;
  8.  
  9. /**
  10. * Email Workflows
  11. *
  12. * Contains specialized email sending workflows for specific use cases.
  13. *
  14. * @package App\Services\Email
  15. */
  16. class EmailWorkflows
  17. {
  18. /**
  19. * @var EmailService Core email service
  20. */
  21. private EmailService $emailService;
  22.  
  23. /**
  24. * EmailWorkflows constructor
  25. *
  26. * @param EmailService|null $emailService Optional email service instance
  27. * @throws PHPMailerException
  28. */
  29. public function __construct(?EmailService $emailService = null)
  30. {
  31. $this->emailService = $emailService ?? new EmailService();
  32. }
  33.  
  34. /**
  35. * Send a template-based email
  36. *
  37. * @param string|array $to Recipient(s)
  38. * @param string $subject Email subject
  39. * @param string $templateName Template name (without .php extension)
  40. * @param array $variables Variables to pass to the template
  41. * @param array $attachments Attachments [path => name]
  42. * @param array $cc CC recipients [email => name]
  43. * @param array $bcc BCC recipients [email => name]
  44. * @return bool True if email was sent successfully
  45. * @throws Exception If email sending fails
  46. */
  47. public function sendTemplate(
  48. string|array $to,
  49. string $subject,
  50. string $templateName,
  51. array $variables = [],
  52. array $attachments = [],
  53. array $cc = [],
  54. array $bcc = []
  55. ): bool
  56. {
  57. // Get the template content
  58. $body = $this->emailService->getTemplateRenderer()->render($templateName, $variables);
  59.  
  60. // Validate HTML structure
  61. $body = $this->emailService->getHtmlValidator()->validate($body);
  62.  
  63. // Generate a plain text version (improved conversion)
  64. $plainText = $this->emailService->getTemplateRenderer()->htmlToPlainText($body);
  65.  
  66. // Send the email
  67. return $this->emailService->send($to, $subject, $body, $plainText, $attachments, $cc, $bcc);
  68. }
  69.  
  70. /**
  71. * Send a contact form email
  72. *
  73. * @param string $name Sender's name
  74. * @param string $email Sender's email
  75. * @param string $subject Email subject
  76. * @param string $message Email message
  77. * @param string $toEmail Recipient email
  78. * @param string $toName Recipient name
  79. * @return bool True if email was sent successfully
  80. * @throws Exception If email sending fails
  81. */
  82. public function sendContactForm(
  83. string $name,
  84. string $email,
  85. string $subject,
  86. string $message,
  87. string $toEmail = '[email protected]',
  88. string $toName = 'Support Team'
  89. ): bool
  90. {
  91. // Override reply-to with the sender's email
  92. $this->emailService->setReplyTo($email, $name);
  93.  
  94. // Include original sender information in email
  95. $emailSubject = "Contact Form: " . $subject;
  96.  
  97. // Send the email using the template system
  98. return $this->sendTemplate(
  99. [$toEmail => $toName],
  100. $emailSubject,
  101. 'contact_form',
  102. [
  103. 'name' => $name,
  104. 'email' => $email,
  105. 'subject' => $subject,
  106. 'message' => $message
  107. ]
  108. );
  109. }
  110.  
  111. /**
  112. * Send an auto-reply email to someone who submitted the contact form
  113. *
  114. * @param string $name Recipient's name
  115. * @param string $email Recipient's email
  116. * @param string $subject Original subject
  117. * @return bool True if email was sent successfully
  118. * @throws Exception
  119. */
  120. public function sendAutoReply(string $name, string $email, string $subject): bool
  121. {
  122. // Reset mailer for a new message
  123. $this->emailService->resetMailer();
  124.  
  125. // Use the template system for the auto-reply
  126. return $this->sendTemplate(
  127. $email,
  128. 'Thank you for contacting ' . APP_NAME,
  129. 'contact_auto_reply',
  130. [
  131. 'name' => $name,
  132. 'subject' => $subject
  133. ]
  134. );
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement