10-sending_test.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace cx_newsletter;
  3. use \Exception;
  4. class sending_test_activity
  5. extends activity {
  6. public function show_after_button() : null {
  7. return null;
  8. }
  9. public function inside_buttons() : array {
  10. return [ 'test' ];
  11. }
  12. public function inside_inputs() : array {
  13. return [
  14. 'target' => 'email'
  15. ];
  16. }
  17. public function get_site_template_name() : string {
  18. return 'sending_test';
  19. }
  20. private function validate_result() : ?string {
  21. if ($this->is_received('target') and !$this->is_validated('target')) {
  22. return 'Target email is empty or not valid.';
  23. }
  24. return null;
  25. }
  26. private function prepare_mail() : single_mail {
  27. $mail = new single_mail();
  28. $mail->title = 'Test message from cx-newsletter.';
  29. $mail->content = 'Hello!<br> This is tested message ';
  30. $mail->content .= 'from cx-newsletter. <p style="color: green;">';
  31. $mail->content .= 'If it is green, HTML work.</p>';
  32. $mail->is_html = true;
  33. $mail->to = $this->get_validated('target');
  34. return $mail;
  35. }
  36. public function process() : self {
  37. $validation = $this->validate_result();
  38. if ($validation !== null) {
  39. $this->error_toast($validation);
  40. return $this;
  41. }
  42. if (!$this->is_received('target')) {
  43. return $this;
  44. }
  45. try {
  46. $mailer = new mail_manager($this->get_settings());
  47. $mailer->prepare($this->prepare_mail());
  48. if (!$mailer->send(true)) {
  49. throw new Exception('Can not send email.');
  50. }
  51. } catch (Exception $exception) {
  52. $this->error_toast($exception->getMessage());
  53. return $this;
  54. }
  55. $this->success_toast('Successed sended!');
  56. return $this;
  57. }
  58. }