Analytics.php
3 years ago
AutomatedLatestContent.php
2 months ago
AutomaticEmails.php
3 years ago
Captcha.php
1 year ago
Coupons.php
2 years ago
CustomFields.php
2 months ago
DynamicProducts.php
1 year ago
DynamicSegments.php
2 months ago
FeatureFlags.php
3 years ago
Forms.php
2 months ago
Help.php
1 year ago
ImportExport.php
2 months ago
Mailer.php
1 year ago
NewsletterLinks.php
2 months ago
NewsletterTemplates.php
2 months ago
Newsletters.php
2 months ago
Premium.php
10 months ago
RedirectResponse.php
1 year ago
Segments.php
2 months ago
SendingQueue.php
2 months ago
Services.php
6 months ago
Settings.php
2 months ago
Setup.php
1 year ago
StatisticsExport.php
3 months ago
SubscriberStats.php
1 month ago
Subscribers.php
2 months ago
Tags.php
3 years ago
UserFlags.php
2 years ago
WoocommerceProductVariations.php
2 months ago
WoocommerceSettings.php
3 years ago
index.php
3 years ago
Mailer.php
101 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\API\JSON\v1; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\Endpoint as APIEndpoint; |
| 9 | use MailPoet\API\JSON\Error as APIError; |
| 10 | use MailPoet\Config\AccessControl; |
| 11 | use MailPoet\Mailer\MailerFactory; |
| 12 | use MailPoet\Mailer\MailerLog; |
| 13 | use MailPoet\Mailer\MetaInfo; |
| 14 | use MailPoet\Services\AuthorizedEmailsController; |
| 15 | use MailPoet\Services\AuthorizedSenderDomainController; |
| 16 | use MailPoet\Settings\SettingsController; |
| 17 | |
| 18 | class Mailer extends APIEndpoint { |
| 19 | |
| 20 | /** @var AuthorizedEmailsController */ |
| 21 | private $authorizedEmailsController; |
| 22 | |
| 23 | /** @var SettingsController */ |
| 24 | private $settings; |
| 25 | |
| 26 | /** @var MetaInfo */ |
| 27 | private $mailerMetaInfo; |
| 28 | |
| 29 | /** @var MailerFactory */ |
| 30 | private $mailerFactory; |
| 31 | |
| 32 | /** @var AuthorizedSenderDomainController */ |
| 33 | private $senderDomainController; |
| 34 | |
| 35 | public $permissions = [ |
| 36 | 'global' => AccessControl::PERMISSION_MANAGE_EMAILS, |
| 37 | ]; |
| 38 | |
| 39 | public function __construct( |
| 40 | AuthorizedEmailsController $authorizedEmailsController, |
| 41 | SettingsController $settings, |
| 42 | MailerFactory $mailerFactory, |
| 43 | MetaInfo $mailerMetaInfo, |
| 44 | AuthorizedSenderDomainController $senderDomainController |
| 45 | ) { |
| 46 | $this->authorizedEmailsController = $authorizedEmailsController; |
| 47 | $this->settings = $settings; |
| 48 | $this->mailerFactory = $mailerFactory; |
| 49 | $this->mailerMetaInfo = $mailerMetaInfo; |
| 50 | $this->senderDomainController = $senderDomainController; |
| 51 | } |
| 52 | |
| 53 | public function send($data = []) { |
| 54 | try { |
| 55 | $mailer = $this->mailerFactory->buildMailer( |
| 56 | $data['mailer'] ?? null, |
| 57 | $data['sender'] ?? null, |
| 58 | $data['reply_to'] ?? null |
| 59 | ); |
| 60 | // report this as 'sending_test' in metadata since this endpoint is only used to test sending methods for now |
| 61 | $extraParams = [ |
| 62 | 'meta' => $this->mailerMetaInfo->getSendingTestMetaInfo(), |
| 63 | ]; |
| 64 | $result = $mailer->send($data['newsletter'], $data['subscriber'], $extraParams); |
| 65 | } catch (\Exception $e) { |
| 66 | return $this->errorResponse([ |
| 67 | $e->getCode() => $e->getMessage(), |
| 68 | ]); |
| 69 | } |
| 70 | |
| 71 | if ($result['response'] === false) { |
| 72 | $error = sprintf( |
| 73 | // translators: %s is the error message. |
| 74 | __('The email could not be sent: %s', 'mailpoet'), |
| 75 | $result['error']->getMessage() |
| 76 | ); |
| 77 | return $this->errorResponse([APIError::BAD_REQUEST => $error]); |
| 78 | } else { |
| 79 | return $this->successResponse(null); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public function resumeSending() { |
| 84 | if ($this->settings->get(AuthorizedEmailsController::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING)) { |
| 85 | $this->authorizedEmailsController->checkAuthorizedEmailAddresses(); |
| 86 | } |
| 87 | MailerLog::resumeSending(); |
| 88 | return $this->successResponse(null); |
| 89 | } |
| 90 | |
| 91 | public function getAuthorizedEmailAddresses() { |
| 92 | $authorizedEmails = $this->authorizedEmailsController->getAuthorizedEmailAddresses(); |
| 93 | return $this->successResponse($authorizedEmails); |
| 94 | } |
| 95 | |
| 96 | public function getVerifiedSenderDomains() { |
| 97 | $verifiedDomains = $this->senderDomainController->getVerifiedSenderDomains(); |
| 98 | return $this->successResponse($verifiedDomains); |
| 99 | } |
| 100 | } |
| 101 |