email.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Config\Config;
  4. use Grav\Common\GravTrait;
  5. class Email
  6. {
  7. use GravTrait;
  8. /**
  9. * @var \Swift_Transport
  10. */
  11. protected $mailer;
  12. /**
  13. * Returns true if emails have been enabled in the system.
  14. *
  15. * @return bool
  16. */
  17. public function enabled()
  18. {
  19. return self::getGrav()['config']->get('plugins.email.mailer.engine') != 'none';
  20. }
  21. /**
  22. * Creates an email message.
  23. *
  24. * @param string $subject
  25. * @param string $body
  26. * @param string $contentType
  27. * @param string $charset
  28. * @return \Swift_Message
  29. */
  30. public function message($subject = null, $body = null, $contentType = null, $charset = null)
  31. {
  32. return new \Swift_Message($subject, $body, $contentType, $charset);
  33. }
  34. /**
  35. * Creates an attachment.
  36. *
  37. * @param string $data
  38. * @param string $filename
  39. * @param string $contentType
  40. * @return \Swift_Attachment
  41. */
  42. public function attachment($data = null, $filename = null, $contentType = null)
  43. {
  44. return new \Swift_Attachment($data, $filename, $contentType);
  45. }
  46. /**
  47. * Creates an embedded attachment.
  48. *
  49. * @param string $data
  50. * @param string $filename
  51. * @param string $contentType
  52. * @return \Swift_EmbeddedFile
  53. */
  54. public function embedded($data = null, $filename = null, $contentType = null)
  55. {
  56. return new \Swift_EmbeddedFile($data, $filename, $contentType);
  57. }
  58. /**
  59. * Creates an image attachment.
  60. *
  61. * @param string $data
  62. * @param string $filename
  63. * @param string $contentType
  64. * @return \Swift_Image
  65. */
  66. public function image($data = null, $filename = null, $contentType = null)
  67. {
  68. return new \Swift_Image($data, $filename, $contentType);
  69. }
  70. /**
  71. * Send email.
  72. *
  73. * @param \Swift_Message $message
  74. * @return int
  75. */
  76. public function send($message)
  77. {
  78. $mailer = $this->getMailer();
  79. return $mailer ? $mailer->send($message) : 0;
  80. }
  81. /**
  82. * @internal
  83. * @return null|\Swift_Mailer
  84. */
  85. protected function getMailer()
  86. {
  87. if (!$this->enabled()) {
  88. return null;
  89. }
  90. if (!$this->mailer) {
  91. /** @var Config $config */
  92. $config = self::getGrav()['config'];
  93. $mailer = $config->get('plugins.email.mailer.default');
  94. // Create the Transport and initialize it.
  95. switch ($mailer) {
  96. case 'smtp':
  97. $transport = \Swift_SmtpTransport::newInstance();
  98. $options = $config->get('plugins.email.mailer.smtp');
  99. if (!empty($options['server'])) {
  100. $transport->setHost($options['server']);
  101. }
  102. if (!empty($options['port'])) {
  103. $transport->setPort($options['port']);
  104. }
  105. if (!empty($options['encryption']) && $options['encryption'] != 'none') {
  106. $transport->setEncryption($options['encryption']);
  107. }
  108. if (!empty($options['user'])) {
  109. $transport->setUsername($options['user']);
  110. }
  111. if (!empty($options['password'])) {
  112. $transport->setPassword($options['password']);
  113. }
  114. break;
  115. case 'sendmail':
  116. $options = $config->get('plugins.email.mailer.sendmail');
  117. $bin = !empty($options['bin']) ? $options['bin'] : '/usr/sbin/sendmail';
  118. $transport = \Swift_SendmailTransport::newInstance($bin);
  119. break;
  120. case 'mail':
  121. default:
  122. $transport = \Swift_MailTransport::newInstance();
  123. }
  124. // Create the Mailer using your created Transport
  125. $this->mailer = \Swift_Mailer::newInstance($transport);
  126. }
  127. return $this->mailer;
  128. }
  129. }