/vendor/laravel/framework/src/Illuminate/Mail/Message.php

https://gitlab.com/ealexis.t/trends · PHP · 298 lines · 113 code · 38 blank · 147 comment · 5 complexity · 398ea836d5bc61087acaa71fc6a827fa MD5 · raw file

  1. <?php
  2. namespace Illuminate\Mail;
  3. use Swift_Image;
  4. use Swift_Attachment;
  5. class Message
  6. {
  7. /**
  8. * The Swift Message instance.
  9. *
  10. * @var \Swift_Message
  11. */
  12. protected $swift;
  13. /**
  14. * Create a new message instance.
  15. *
  16. * @param \Swift_Message $swift
  17. * @return void
  18. */
  19. public function __construct($swift)
  20. {
  21. $this->swift = $swift;
  22. }
  23. /**
  24. * Add a "from" address to the message.
  25. *
  26. * @param string|array $address
  27. * @param string|null $name
  28. * @return $this
  29. */
  30. public function from($address, $name = null)
  31. {
  32. $this->swift->setFrom($address, $name);
  33. return $this;
  34. }
  35. /**
  36. * Set the "sender" of the message.
  37. *
  38. * @param string|array $address
  39. * @param string|null $name
  40. * @return $this
  41. */
  42. public function sender($address, $name = null)
  43. {
  44. $this->swift->setSender($address, $name);
  45. return $this;
  46. }
  47. /**
  48. * Set the "return path" of the message.
  49. *
  50. * @param string $address
  51. * @return $this
  52. */
  53. public function returnPath($address)
  54. {
  55. $this->swift->setReturnPath($address);
  56. return $this;
  57. }
  58. /**
  59. * Add a recipient to the message.
  60. *
  61. * @param string|array $address
  62. * @param string|null $name
  63. * @param bool $override
  64. * @return $this
  65. */
  66. public function to($address, $name = null, $override = false)
  67. {
  68. if ($override) {
  69. $this->swift->setTo($address, $name);
  70. return $this;
  71. }
  72. return $this->addAddresses($address, $name, 'To');
  73. }
  74. /**
  75. * Add a carbon copy to the message.
  76. *
  77. * @param string|array $address
  78. * @param string|null $name
  79. * @return $this
  80. */
  81. public function cc($address, $name = null)
  82. {
  83. return $this->addAddresses($address, $name, 'Cc');
  84. }
  85. /**
  86. * Add a blind carbon copy to the message.
  87. *
  88. * @param string|array $address
  89. * @param string|null $name
  90. * @return $this
  91. */
  92. public function bcc($address, $name = null)
  93. {
  94. return $this->addAddresses($address, $name, 'Bcc');
  95. }
  96. /**
  97. * Add a reply to address to the message.
  98. *
  99. * @param string|array $address
  100. * @param string|null $name
  101. * @return $this
  102. */
  103. public function replyTo($address, $name = null)
  104. {
  105. return $this->addAddresses($address, $name, 'ReplyTo');
  106. }
  107. /**
  108. * Add a recipient to the message.
  109. *
  110. * @param string|array $address
  111. * @param string $name
  112. * @param string $type
  113. * @return $this
  114. */
  115. protected function addAddresses($address, $name, $type)
  116. {
  117. if (is_array($address)) {
  118. $this->swift->{"set{$type}"}($address, $name);
  119. } else {
  120. $this->swift->{"add{$type}"}($address, $name);
  121. }
  122. return $this;
  123. }
  124. /**
  125. * Set the subject of the message.
  126. *
  127. * @param string $subject
  128. * @return $this
  129. */
  130. public function subject($subject)
  131. {
  132. $this->swift->setSubject($subject);
  133. return $this;
  134. }
  135. /**
  136. * Set the message priority level.
  137. *
  138. * @param int $level
  139. * @return $this
  140. */
  141. public function priority($level)
  142. {
  143. $this->swift->setPriority($level);
  144. return $this;
  145. }
  146. /**
  147. * Attach a file to the message.
  148. *
  149. * @param string $file
  150. * @param array $options
  151. * @return $this
  152. */
  153. public function attach($file, array $options = [])
  154. {
  155. $attachment = $this->createAttachmentFromPath($file);
  156. return $this->prepAttachment($attachment, $options);
  157. }
  158. /**
  159. * Create a Swift Attachment instance.
  160. *
  161. * @param string $file
  162. * @return \Swift_Attachment
  163. */
  164. protected function createAttachmentFromPath($file)
  165. {
  166. return Swift_Attachment::fromPath($file);
  167. }
  168. /**
  169. * Attach in-memory data as an attachment.
  170. *
  171. * @param string $data
  172. * @param string $name
  173. * @param array $options
  174. * @return $this
  175. */
  176. public function attachData($data, $name, array $options = [])
  177. {
  178. $attachment = $this->createAttachmentFromData($data, $name);
  179. return $this->prepAttachment($attachment, $options);
  180. }
  181. /**
  182. * Create a Swift Attachment instance from data.
  183. *
  184. * @param string $data
  185. * @param string $name
  186. * @return \Swift_Attachment
  187. */
  188. protected function createAttachmentFromData($data, $name)
  189. {
  190. return Swift_Attachment::newInstance($data, $name);
  191. }
  192. /**
  193. * Embed a file in the message and get the CID.
  194. *
  195. * @param string $file
  196. * @return string
  197. */
  198. public function embed($file)
  199. {
  200. return $this->swift->embed(Swift_Image::fromPath($file));
  201. }
  202. /**
  203. * Embed in-memory data in the message and get the CID.
  204. *
  205. * @param string $data
  206. * @param string $name
  207. * @param string|null $contentType
  208. * @return string
  209. */
  210. public function embedData($data, $name, $contentType = null)
  211. {
  212. $image = Swift_Image::newInstance($data, $name, $contentType);
  213. return $this->swift->embed($image);
  214. }
  215. /**
  216. * Prepare and attach the given attachment.
  217. *
  218. * @param \Swift_Attachment $attachment
  219. * @param array $options
  220. * @return $this
  221. */
  222. protected function prepAttachment($attachment, $options = [])
  223. {
  224. // First we will check for a MIME type on the message, which instructs the
  225. // mail client on what type of attachment the file is so that it may be
  226. // downloaded correctly by the user. The MIME option is not required.
  227. if (isset($options['mime'])) {
  228. $attachment->setContentType($options['mime']);
  229. }
  230. // If an alternative name was given as an option, we will set that on this
  231. // attachment so that it will be downloaded with the desired names from
  232. // the developer, otherwise the default file names will get assigned.
  233. if (isset($options['as'])) {
  234. $attachment->setFilename($options['as']);
  235. }
  236. $this->swift->attach($attachment);
  237. return $this;
  238. }
  239. /**
  240. * Get the underlying Swift Message instance.
  241. *
  242. * @return \Swift_Message
  243. */
  244. public function getSwiftMessage()
  245. {
  246. return $this->swift;
  247. }
  248. /**
  249. * Dynamically pass missing methods to the Swift instance.
  250. *
  251. * @param string $method
  252. * @param array $parameters
  253. * @return mixed
  254. */
  255. public function __call($method, $parameters)
  256. {
  257. $callable = [$this->swift, $method];
  258. return call_user_func_array($callable, $parameters);
  259. }
  260. }