Nessuna descrizione

ConfirmationEmailMailer.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace MailPoet\Subscribers;
  3. if (!defined('ABSPATH')) exit;
  4. use Html2Text\Html2Text;
  5. use MailPoet\Mailer\Mailer;
  6. use MailPoet\Mailer\MetaInfo;
  7. use MailPoet\Models\Subscriber;
  8. use MailPoet\Services\AuthorizedEmailsController;
  9. use MailPoet\Services\Bridge;
  10. use MailPoet\Settings\SettingsController;
  11. use MailPoet\Subscription\SubscriptionUrlFactory;
  12. use MailPoet\Util\Helpers;
  13. use MailPoet\WP\Functions as WPFunctions;
  14. class ConfirmationEmailMailer {
  15. const MAX_CONFIRMATION_EMAILS = 3;
  16. /** @var Mailer */
  17. private $mailer;
  18. /** @var WPFunctions */
  19. private $wp;
  20. /** @var SettingsController */
  21. private $settings;
  22. /** @var MetaInfo */
  23. private $mailerMetaInfo;
  24. /** @var SubscribersRepository */
  25. private $subscribersRepository;
  26. /** @var SubscriptionUrlFactory */
  27. private $subscriptionUrlFactory;
  28. /** @var array Cache for confirmation emails sent within a request */
  29. private $sentEmails = [];
  30. public function __construct(
  31. Mailer $mailer,
  32. WPFunctions $wp,
  33. SettingsController $settings,
  34. SubscribersRepository $subscribersRepository,
  35. SubscriptionUrlFactory $subscriptionUrlFactory
  36. ) {
  37. $this->mailer = $mailer;
  38. $this->wp = $wp;
  39. $this->settings = $settings;
  40. $this->mailerMetaInfo = new MetaInfo;
  41. $this->subscriptionUrlFactory = $subscriptionUrlFactory;
  42. $this->subscribersRepository = $subscribersRepository;
  43. }
  44. /**
  45. * Use this method if you want to make sure the confirmation email
  46. * is not sent multiple times within a single request
  47. * e.g. if sending confirmation emails from hooks
  48. */
  49. public function sendConfirmationEmailOnce(Subscriber $subscriber): bool {
  50. if (isset($this->sentEmails[$subscriber->id])) {
  51. return true;
  52. }
  53. return $this->sendConfirmationEmail($subscriber);
  54. }
  55. public function sendConfirmationEmail(Subscriber $subscriber) {
  56. $signupConfirmation = $this->settings->get('signup_confirmation');
  57. if ((bool)$signupConfirmation['enabled'] === false) {
  58. return false;
  59. }
  60. if (!$this->wp->isUserLoggedIn() && $subscriber->countConfirmations >= self::MAX_CONFIRMATION_EMAILS) {
  61. return false;
  62. }
  63. $authorizationEmailsValidation = $this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING);
  64. $unauthorizedSenderEmail = isset($authorizationEmailsValidation['invalid_sender_address']);
  65. if (Bridge::isMPSendingServiceEnabled() && $unauthorizedSenderEmail) {
  66. return false;
  67. }
  68. $segments = $subscriber->segments()->findMany();
  69. $segmentNames = array_map(function($segment) {
  70. return $segment->name;
  71. }, $segments);
  72. $body = nl2br($signupConfirmation['body']);
  73. // replace list of segments shortcode
  74. $body = str_replace(
  75. '[lists_to_confirm]',
  76. '<strong>' . join(', ', $segmentNames) . '</strong>',
  77. $body
  78. );
  79. // replace activation link
  80. $subscriberEntity = $this->subscribersRepository->findOneById($subscriber->id);
  81. $body = Helpers::replaceLinkTags(
  82. $body,
  83. $this->subscriptionUrlFactory->getConfirmationUrl($subscriberEntity),
  84. ['target' => '_blank'],
  85. 'activation_link'
  86. );
  87. //create a text version. @ is important here, Html2Text throws warnings
  88. $text = @Html2Text::convert(
  89. (mb_detect_encoding($body, 'UTF-8', true)) ? $body : utf8_encode($body),
  90. true
  91. );
  92. // build email data
  93. $email = [
  94. 'subject' => $signupConfirmation['subject'],
  95. 'body' => [
  96. 'html' => $body,
  97. 'text' => $text,
  98. ],
  99. ];
  100. // send email
  101. try {
  102. $extraParams = [
  103. 'meta' => $this->mailerMetaInfo->getConfirmationMetaInfo($subscriber),
  104. ];
  105. $result = $this->mailer->send($email, $subscriber, $extraParams);
  106. if ($result['response'] === false) {
  107. $subscriber->setError(__('Something went wrong with your subscription. Please contact the website owner.', 'mailpoet'));
  108. return false;
  109. };
  110. if (!$this->wp->isUserLoggedIn()) {
  111. $subscriber->countConfirmations++;
  112. $subscriber->save();
  113. }
  114. $this->sentEmails[$subscriber->id] = true;
  115. return true;
  116. } catch (\Exception $e) {
  117. $subscriber->setError(__('Something went wrong with your subscription. Please contact the website owner.', 'mailpoet'));
  118. return false;
  119. }
  120. }
  121. }