PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/swift/EasySwift.php

https://bitbucket.org/enurkov/prestashop
PHP | 949 lines | 719 code | 8 blank | 222 comment | 28 complexity | 83f1d7c5c83a3f787eb752dca42849c8 MD5 | raw file
  1. <?php
  2. /**
  3. * EasySwift: Swift Mailer Facade
  4. * Please read the LICENSE file
  5. * @author Chris Corbyn <chris@w3style.co.uk>
  6. * @package EasySwift
  7. * @version 1.0.3
  8. * @license GNU Lesser General Public License
  9. */
  10. require_once dirname(__FILE__) . "/Swift/ClassLoader.php";
  11. Swift_ClassLoader::load("Swift");
  12. Swift_ClassLoader::load("Swift_Connection_SMTP");
  13. Swift_ClassLoader::load("Swift_Connection_Sendmail");
  14. //Some constants for backwards compatibility with v2 code
  15. if (!defined("SWIFT_TLS")) define("SWIFT_TLS", Swift_Connection_SMTP::ENC_TLS);
  16. if (!defined("SWIFT_SSL")) define("SWIFT_SSL", Swift_Connection_SMTP::ENC_SSL);
  17. if (!defined("SWIFT_OPEN")) define("SWIFT_OPEN", Swift_Connection_SMTP::ENC_OFF);
  18. if (!defined("SWIFT_SECURE_PORT")) define("SWIFT_SECURE_PORT", Swift_Connection_SMTP::PORT_SECURE);
  19. if (!defined("SWIFT_DEFAULT_PORT")) define("SWIFT_DEFAULT_PORT", Swift_Connection_SMTP::PORT_DEFAULT);
  20. /**
  21. * EasySwift: Facade for Swift Mailer Version 3.
  22. * Provides (most of) the API from older versions of Swift, wrapped around the new version 3 API.
  23. * Due to the popularity of the new API, EasySwift will not be around indefinitely.
  24. * @package EasySwift
  25. * @author Chris Corbyn <chris@w3style.co.uk>
  26. * @deprecated
  27. */
  28. class EasySwift
  29. {
  30. /**
  31. * The instance of Swift this class wrappers
  32. * @var Swift
  33. */
  34. public $swift = null;
  35. /**
  36. * This value becomes set to true when Swift fails
  37. * @var boolean
  38. */
  39. public $failed = false;
  40. /**
  41. * The number of loaded plugins
  42. * @var int
  43. */
  44. protected $pluginCount = 0;
  45. /**
  46. * An instance of Swift_Message
  47. * @var Swift_Message
  48. */
  49. public $message = null;
  50. /**
  51. * An address list to send to (Cc, Bcc, To..)
  52. * @var Swift_RecipientList
  53. */
  54. public $recipients = null;
  55. /**
  56. * If all recipients should get the same copy of the message, including headers
  57. * This is already implied if any Cc or Bcc recipients are set
  58. * @var boolean
  59. */
  60. protected $exactCopy = false;
  61. /**
  62. * If EasySwift should get rid of the message and recipients once it's done sending
  63. * @var boolean
  64. */
  65. protected $autoFlush = true;
  66. /**
  67. * A list of the IDs of all parts added to the message
  68. * @var array
  69. */
  70. protected $partIds = array();
  71. /**
  72. * A list of all the IDs of the attachments add to the message
  73. * @var array
  74. */
  75. protected $attachmentIds = array();
  76. /**
  77. * The last response received from the server
  78. * @var string
  79. */
  80. public $lastResponse = "";
  81. /**
  82. * The 3 digit code in the last response received from the server
  83. * @var int
  84. */
  85. public $responseCode = 0;
  86. /**
  87. * The list of errors handled at runtime
  88. * @var array
  89. */
  90. public $errors = array();
  91. /**
  92. * The last error received
  93. * @var string
  94. */
  95. public $lastError = null;
  96. /**
  97. * Constructor
  98. * @param Swift_Connection The connection to use
  99. * @param string The domain name of this server (not the SMTP server)
  100. */
  101. public function __construct(Swift_Connection $connection, $domain=null)
  102. {
  103. try {
  104. $this->swift = new Swift($connection, $domain, Swift::ENABLE_LOGGING);
  105. Swift_ClassLoader::load("Swift_Plugin_EasySwiftResponseTracker");
  106. $this->swift->attachPlugin(new Swift_Plugin_EasySwiftResponseTracker($this), "_ResponseTracker");
  107. } catch (Swift_ConnectionException $e) {
  108. $this->failed = true;
  109. $this->setError("The connection failed to start. An exception was thrown:<br />" . $e->getMessage());
  110. }
  111. $this->newMessage();
  112. $this->newRecipientList();
  113. }
  114. /**
  115. * Set an error message
  116. * @param string Error message
  117. */
  118. public function setError($msg)
  119. {
  120. $this->errors[] = ($this->lastError = $msg);
  121. }
  122. /**
  123. * Get the full list of errors
  124. * @return array
  125. */
  126. public function getErrors()
  127. {
  128. return $this->errors;
  129. }
  130. /**
  131. * Get the last error that occurred
  132. * @return string
  133. */
  134. public function getLastError()
  135. {
  136. return $this->lastError;
  137. }
  138. /**
  139. * Clear the current list of errors
  140. */
  141. public function flushErrors()
  142. {
  143. $this->errors = null;
  144. $this->errors = array();
  145. }
  146. /**
  147. * Turn automatic flsuhing on or off.
  148. * This in ON by deault. It removes the message and all parts after sending.
  149. * @param boolean
  150. */
  151. public function autoFlush($flush=true)
  152. {
  153. $this->autoFlush = $flush;
  154. }
  155. /**
  156. * Set the maximum size of the log
  157. * @param int
  158. */
  159. public function setMaxLogSize($size)
  160. {
  161. $log = Swift_LogContainer::getLog();
  162. $log->setMaxSize($size);
  163. }
  164. /**
  165. * Turn logging on or off (saves memory)
  166. * @param boolean
  167. */
  168. public function useLogging($use=true)
  169. {
  170. $log = Swift_LogContainer::getLog();
  171. if ($use) $log->setLogLevel(Swift_Log::LOG_NETWORK);
  172. else $log->setLogLevel(Swift_Log::LOG_NOTHING);
  173. }
  174. /**
  175. * Enable line resizing (on 1000 by default)
  176. * @param int The number of characters allowed on a line
  177. */
  178. public function useAutoLineResizing($size=1000)
  179. {
  180. $this->message->setLineWrap($size);
  181. }
  182. /**
  183. * Dump the log contents
  184. * @deprecated
  185. */
  186. public function getTransactions()
  187. {
  188. return $this->dumpLog();
  189. }
  190. /**
  191. * Dump the contents of the log to the browser
  192. * The log contains some &lt; and &gt; characters so you may need to view source
  193. * Note that this method dumps data to the browser, it does NOT return anything.
  194. */
  195. public function dumpLog()
  196. {
  197. $log = Swift_LogContainer::getLog();
  198. $log->dump();
  199. }
  200. /**
  201. * This method should be called if you do not wish to send messages in batch mode (i.e. if all recipients should see each others' addresses)
  202. * @param boolean If this mode should be used
  203. */
  204. public function useExactCopy($bool=true)
  205. {
  206. $this->exactCopy = $bool;
  207. }
  208. /**
  209. * Reset the current message and start a fresh one
  210. */
  211. public function newMessage($msg=false)
  212. {
  213. if (!$msg) $msg = new Swift_Message();
  214. $this->message = $msg;
  215. $this->partIds = array();
  216. $this->attachmentIds = array();
  217. }
  218. /**
  219. * Clear out all message parts
  220. * @return boolean
  221. */
  222. public function flushParts()
  223. {
  224. $success = true;
  225. foreach ($this->partIds as $id)
  226. {
  227. try {
  228. $this->message->detach($id);
  229. } catch (Swift_Message_MimeException $e) {
  230. $success = false;
  231. $this->setError("A MIME part failed to detach due to the error:<br />" . $e->getMessage());
  232. }
  233. }
  234. $this->partIds = array();
  235. return $success;
  236. }
  237. /**
  238. * Clear out all attachments
  239. * @return boolean
  240. */
  241. public function flushAttachments()
  242. {
  243. $success = true;
  244. foreach ($this->attachmentIds as $id)
  245. {
  246. try {
  247. $this->message->detach($id);
  248. } catch (Swift_Message_MimeException $e) {
  249. $success = false;
  250. $this->setError("An attachment failed to detach due to the error:<br />" . $e->getMessage());
  251. }
  252. }
  253. $this->attachmentIds = array();
  254. return $success;
  255. }
  256. /**
  257. * Clear out all message headers
  258. * @deprecated
  259. */
  260. public function flushHeaders()
  261. {
  262. $this->newMessage();
  263. }
  264. /**
  265. * Reset the current list of recipients and start a new one
  266. */
  267. public function newRecipientList($list=false)
  268. {
  269. if (!$list) $list = new Swift_RecipientList();
  270. $this->recipients = $list;
  271. }
  272. /**
  273. * Check if Swift has failed or not
  274. * This facade stops processing if so
  275. * @return boolean
  276. */
  277. public function hasFailed()
  278. {
  279. return $this->failed;
  280. }
  281. /**
  282. * Check if the current connection is open or not
  283. * @return boolean
  284. */
  285. public function isConnected()
  286. {
  287. return (($this->swift !== null) && $this->swift->connection->isAlive());
  288. }
  289. /**
  290. * Connect to the MTA if not already connected
  291. */
  292. public function connect()
  293. {
  294. if (!$this->isConnected())
  295. {
  296. try {
  297. $this->swift->connect();
  298. return true;
  299. } catch (Swift_ConnectionException $e) {
  300. $this->failed = true;
  301. $this->setError("Swift failed to run the connection process:<br />" . $e->getMessage());
  302. }
  303. }
  304. return false;
  305. }
  306. /**
  307. * Perform the SMTP greeting process (don't do this unless you understand why you're doing it)
  308. */
  309. public function handshake()
  310. {
  311. $this->swift->handshake();
  312. }
  313. /**
  314. * Close the connection to the MTA
  315. * @return boolean
  316. */
  317. public function close()
  318. {
  319. if ($this->isConnected())
  320. {
  321. try {
  322. $this->swift->disconnect();
  323. return true;
  324. } catch (Swift_ConnectionException $e) {
  325. $this->setError("Disconnect failed:<br />" . $e->getMessage());
  326. }
  327. }
  328. return false;
  329. }
  330. /**
  331. * Send a command to Swift and get a response
  332. * @param string The command to send (leave of CRLF)
  333. * @return string
  334. */
  335. public function command($command)
  336. {
  337. if (substr($command, -2) == "\r\n") $command = substr($command, 0, -2);
  338. try {
  339. $rs = $this->swift->command($command);
  340. return $rs->getString();
  341. } catch (Swift_ConnectionException $e) {
  342. $this->setError("Command failed:<br />" . $e->getMessage());
  343. return false;
  344. }
  345. }
  346. /**
  347. * Add a new plugin to respond to events
  348. * @param Swift_Events_Listener The plugin to load
  349. * @param string The ID to identify the plugin by if needed
  350. * @return string The ID of the plugin
  351. */
  352. public function loadPlugin(Swift_Events_Listener $plugin, $name=null)
  353. {
  354. $this->pluginCount++;
  355. if (!$name) $name = "p" . $this->pluginCount;
  356. $this->swift->attachPlugin($plugin, $name);
  357. return $name;
  358. }
  359. /**
  360. * Get a reference to the plugin identified by $name
  361. * @param string the ID of the plugin
  362. * @return Swift_Events_Listener
  363. */
  364. public function getPlugin($name)
  365. {
  366. try {
  367. $plugin = $this->swift->getPlugin($name);
  368. return $plugin;
  369. } catch (Exception $e) {
  370. return null;
  371. }
  372. }
  373. /**
  374. * Remove the plugin identified by $name
  375. * @param string The ID of the plugin
  376. * @return boolean
  377. */
  378. public function removePlugin($name)
  379. {
  380. try {
  381. $this->swift->removePlugin($name);
  382. return true;
  383. } catch (Exception $e) {
  384. return false;
  385. }
  386. }
  387. /**
  388. * Load in a new authentication mechanism for SMTP
  389. * This needn't be called since Swift will locate any available in Swift/Authenticator/*.php
  390. * @param Swift_Authenticator The authentication mechanism to load
  391. * @throws Exception If the wrong connection is used
  392. */
  393. public function loadAuthenticator(Swift_Authenticator $auth)
  394. {
  395. if (method_exists($this->swift->connection, "attachAuthenticator"))
  396. {
  397. $this->swift->connection->attachAuthenticator($auth);
  398. }
  399. else throw new Exception("SMTP authentication cannot be used with connection class '" . get_class($this->connection) . "'. Swift_Connection_SMTP is needed");
  400. }
  401. /**
  402. * Authenticate with SMTP authentication
  403. * @param string The SMTP username
  404. * @param string The SMTP password
  405. * @return boolean
  406. * @throws Exception If the wrong connection is used
  407. */
  408. public function authenticate($username, $password)
  409. {
  410. if (method_exists($this->swift->connection, "runAuthenticators"))
  411. {
  412. try {
  413. $this->swift->connection->runAuthenticators($username, $password, $this->swift);
  414. return true;
  415. } catch (Swift_ConnectionException $e) {
  416. $this->setError("Authentication failed:<br />" . $e->getMessage());
  417. return false;
  418. }
  419. }
  420. else throw new Exception("SMTP authentication cannot be used with connection class '" . get_class($this->connection) . "'. Swift_Connection_SMTP is needed");
  421. }
  422. /**
  423. * Turn a string representation of an email address into a Swift_Address object
  424. * @paramm string The email address
  425. * @return Swift_Address
  426. */
  427. public function stringToAddress($string)
  428. {
  429. $name = null;
  430. $address = null;
  431. // Foo Bar <foo@bar>
  432. // or: "Foo Bar" <foo@bar>
  433. // or: <foo@bar>
  434. Swift_ClassLoader::load("Swift_Message_Encoder");
  435. if (preg_match("/^\\s*(\"?)(.*?)\\1 *<(" . Swift_Message_Encoder::CHEAP_ADDRESS_RE . ")>\\s*\$/", $string, $matches))
  436. {
  437. if (!empty($matches[2])) $name = $matches[2];
  438. $address = $matches[3];
  439. }
  440. elseif (preg_match("/^\\s*" . Swift_Message_Encoder::CHEAP_ADDRESS_RE . "\\s*\$/", $string))
  441. {
  442. $address = trim($string);
  443. }
  444. else return false;
  445. $swift_address = new Swift_Address($address, $name);
  446. return $swift_address;
  447. }
  448. /**
  449. * Set the encoding used in the message header
  450. * The encoding can be one of Q (quoted-printable) or B (base64)
  451. * @param string The encoding to use
  452. */
  453. public function setHeaderEncoding($mode="B")
  454. {
  455. switch (strtoupper($mode))
  456. {
  457. case "Q": case "QP": case "QUOTED-PRINTABLE":
  458. $this->message->headers->setEncoding("Q");
  459. break;
  460. default:
  461. $this->message->headers->setEncoding("B");
  462. }
  463. }
  464. /**
  465. * Set the return path address (where bounces go to)
  466. * @param mixed The address as a string or Swift_Address
  467. */
  468. public function setReturnPath($address)
  469. {
  470. return $this->message->setReturnPath($address);
  471. }
  472. /**
  473. * Request for a read recipient to be sent to the reply-to address
  474. * @param boolean
  475. */
  476. public function requestReadReceipt($request=true)
  477. {
  478. //$this->message->requestReadReceipt(true);
  479. }
  480. /**
  481. * Set the message priority
  482. * This is an integer between 1 (high) and 5 (low)
  483. * @param int The level of priority to use
  484. */
  485. public function setPriority($priority)
  486. {
  487. $this->message->setPriority($priority);
  488. }
  489. /**
  490. * Get the return-path address as a string
  491. * @return string
  492. */
  493. public function getReturnPath()
  494. {
  495. try {
  496. return $this->message->getReturnPath();
  497. } catch (Swift_Message_MimeException $e) {
  498. return false;
  499. }
  500. }
  501. /**
  502. * Set the reply-to header
  503. * @param mixed The address replies come to. String, or Swift_Address, or an array of either.
  504. */
  505. public function setReplyTo($address)
  506. {
  507. return $this->message->setReplyTo($address);
  508. }
  509. /**
  510. * Get the reply-to address(es) as an array of strings
  511. * @return array
  512. */
  513. public function getReplyTo()
  514. {
  515. try {
  516. return $this->message->getReplyTo();
  517. } catch (Swift_Message_MimeException $e) {
  518. return false;
  519. }
  520. }
  521. /**
  522. * Add To: recipients to the email
  523. * @param mixed To address(es)
  524. * @return boolean
  525. */
  526. public function addTo($address)
  527. {
  528. return $this->addRecipients($address, "To");
  529. }
  530. /**
  531. * Get an array of To addresses
  532. * This currently returns an array of Swift_Address objects and may be simplified to an array of strings in later versions
  533. * @return array
  534. */
  535. public function getToAddresses()
  536. {
  537. return $this->recipients->getTo();
  538. }
  539. /**
  540. * Clear out all To: recipients
  541. */
  542. public function flushTo()
  543. {
  544. $this->recipients->flushTo();
  545. }
  546. /**
  547. * Add Cc: recipients to the email
  548. * @param mixed Cc address(es)
  549. * @return boolean
  550. */
  551. public function addCc($address)
  552. {
  553. return $this->addRecipients($address, "Cc");
  554. }
  555. /**
  556. * Get an array of Cc addresses
  557. * This currently returns an array of Swift_Address objects and may be simplified to an array of strings in later versions
  558. * @return array
  559. */
  560. public function getCcAddresses()
  561. {
  562. return $this->recipients->getCc();
  563. }
  564. /**
  565. * Clear out all Cc: recipients
  566. */
  567. public function flushCc()
  568. {
  569. $this->recipients->flushCc();
  570. }
  571. /**
  572. * Add Bcc: recipients to the email
  573. * @param mixed Bcc address(es)
  574. * @return boolean
  575. */
  576. public function addBcc($address)
  577. {
  578. return $this->addRecipients($address, "Bcc");
  579. }
  580. /**
  581. * Get an array of Bcc addresses
  582. * This currently returns an array of Swift_Address objects and may be simplified to an array of strings in later versions
  583. * @return array
  584. */
  585. public function getBccAddresses()
  586. {
  587. return $this->recipients->getBcc();
  588. }
  589. /**
  590. * Clear out all Bcc: recipients
  591. */
  592. public function flushBcc()
  593. {
  594. $this->recipients->flushBcc();
  595. }
  596. /**
  597. * Add recipients to the email
  598. * @param mixed Address(es)
  599. * @param string Recipient type (To, Cc, Bcc)
  600. * @return boolean
  601. */
  602. protected function addRecipients($address, $type)
  603. {
  604. if (!in_array($type, array("To", "Cc", "Bcc"))) return false;
  605. $method = "add" . $type;
  606. if ($address instanceof Swift_Address)
  607. {
  608. $this->recipients->$method($address);
  609. return true;
  610. }
  611. else
  612. {
  613. $added = 0;
  614. foreach ((array)$address as $addr)
  615. {
  616. if (is_array($addr))
  617. {
  618. $addr = array_values($addr);
  619. if (count($addr) >= 2)
  620. {
  621. $this->recipients->$method($addr[0], $addr[1]);
  622. $added++;
  623. continue;
  624. }
  625. elseif (count($addr) == 1) $addr = $addr[0];
  626. else continue;
  627. }
  628. if (is_string($addr))
  629. {
  630. $addr = $this->stringToAddress($addr);
  631. $this->recipients->$method($addr);
  632. $added++;
  633. }
  634. }
  635. return ($added > 0);
  636. }
  637. }
  638. /**
  639. * Flush message, recipients and headers
  640. */
  641. public function flush()
  642. {
  643. $this->newMessage();
  644. $this->newRecipientList();
  645. }
  646. /**
  647. * Get a list of any addresses which have failed since instantiation
  648. * @return array
  649. */
  650. public function getFailedRecipients()
  651. {
  652. $log = Swift_LogContainer::getLog();
  653. return $log->getFailedRecipients();
  654. }
  655. /**
  656. * Set the multipart MIME warning message (only seen by old clients)
  657. * @param string The message to show
  658. */
  659. public function setMimeWarning($text)
  660. {
  661. $this->message->setMimeWarning($text);
  662. }
  663. /**
  664. * Get the currently set MIME warning (seen by old clients)
  665. * @return string
  666. */
  667. public function getMimeWarning()
  668. {
  669. return $this->message->getMimeWarning();
  670. }
  671. /**
  672. * Set the charset of the charset to use in the message
  673. * @param string The charset (e.g. utf-8, iso-8859-1 etc)
  674. * @return boolean
  675. */
  676. public function setCharset($charset)
  677. {
  678. try {
  679. $this->message->setCharset($charset);
  680. return true;
  681. } catch (Swift_Message_MimeException $e) {
  682. $this->setError("Unable to set the message charset:<br />" . $e->getMessage());
  683. return false;
  684. }
  685. }
  686. /**
  687. * Get the charset of the charset to use in the message
  688. * @return string
  689. */
  690. public function getCharset()
  691. {
  692. return $this->message->getCharset();
  693. }
  694. /**
  695. * Add a new MIME part to the message
  696. * @param mixed The part to add. If this is a string it's used as the body. If it's an instance of Swift_Message_Part it's used as the entire part
  697. * @param string Content-type, default text/plain
  698. * @param string The encoding used (default is to let Swift decide)
  699. * @param string The charset to use (default is to let swift decide)
  700. */
  701. public function addPart($body, $type="text/plain", $encoding=null, $charset=null)
  702. {
  703. if ($body instanceof Swift_Message_Mime)
  704. {
  705. try {
  706. $this->partIds[] = $this->message->attach($body);
  707. } catch (Swift_Message_MimeException $e) {
  708. $this->setError("A MIME part failed to attach:<br />" . $e->getMessage());
  709. return false;
  710. }
  711. }
  712. else
  713. {
  714. try {
  715. $this->partIds[] = $this->message->attach(new Swift_Message_Part($body, $type, $encoding, $charset));
  716. } catch (Swift_Message_MimeException $e) {
  717. $this->setError("A MIME part failed to attach:<br />" . $e->getMessage());
  718. return false;
  719. }
  720. }
  721. }
  722. /**
  723. * Add a new attachment to the message
  724. * @param mixed The attachment to add. If this is a string it's used as the file contents. If it's an instance of Swift_Message_Attachment it's used as the entire part. If it's an instance of Swift_File it's used as the contents.
  725. * @param string Filename, optional
  726. * @param string Content-type. Default application/octet-stream
  727. * @param string The encoding used (default is base64)
  728. * @return boolean
  729. */
  730. public function addAttachment($data, $filename=null, $type="application/octet-stream", $encoding=null)
  731. {
  732. if ($data instanceof Swift_Message_Mime)
  733. {
  734. try {
  735. $this->attachmentIds[] = $this->message->attach($data);
  736. } catch (Swift_Message_MimeException $e) {
  737. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  738. return false;
  739. }
  740. }
  741. else
  742. {
  743. try {
  744. $this->attachmentIds[] = $this->message->attach(new Swift_Message_Attachment($data, $filename, $type, $encoding));
  745. } catch (Swift_Message_MimeException $e) {
  746. $this->setError("An attachment failed to attach<br />" . $e->getMessage());
  747. return false;
  748. } catch (Swift_FileException $e) {
  749. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  750. return false;
  751. }
  752. }
  753. return true;
  754. }
  755. /**
  756. * Embed an image into the message and get the src attribute for HTML
  757. * Returns FALSE on failure
  758. * @param mixed The path to the image, a Swift_Message_Image object or a Swift_File object
  759. * @return string
  760. */
  761. public function addImage($input)
  762. {
  763. $ret = false;
  764. if ($input instanceof Swift_Message_Image)
  765. {
  766. $ret = $this->message->attach($input);
  767. $this->attachmentIds[] = $ret;
  768. return $ret;
  769. }
  770. elseif ($input instanceof Swift_File)
  771. {
  772. try {
  773. $ret = $this->message->attach(new Swift_Message_Image($input));
  774. $this->attachmentIds[] = $ret;
  775. return $ret;
  776. } catch (Swift_Message_MimeException $e) {
  777. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  778. return false;
  779. } catch (Swift_FileException $e) {
  780. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  781. return false;
  782. }
  783. }
  784. else
  785. {
  786. try {
  787. $ret = $this->message->attach(new Swift_Message_Image(new Swift_File($input)));
  788. $this->attachmentIds[] = $ret;
  789. return $ret;
  790. } catch (Swift_Message_MimeException $e) {
  791. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  792. return false;
  793. } catch (Swift_FileException $e) {
  794. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  795. return false;
  796. }
  797. }
  798. }
  799. /**
  800. * Embed an inline file into the message, such as a Image or MIDI file
  801. * @param mixed The file contents, Swift_File object or Swift_Message_EmbeddedFile object
  802. * @param string The content-type of the file, optional
  803. * @param string The filename to use, optional
  804. * @param string the Content-ID to use, optional
  805. * @return string
  806. */
  807. public function embedFile($data, $type="application/octet-stream", $filename=null, $cid=null)
  808. {
  809. $ret = false;
  810. if ($data instanceof Swift_Message_EmbeddedFile)
  811. {
  812. $ret = $this->message->attach($data);
  813. $this->attachmentIds[] = $ret;
  814. return $ret;
  815. }
  816. elseif ($data instanceof Swift_File)
  817. {
  818. try {
  819. $ret = $this->message->attach(new Swift_Message_EmbeddedFile($data, $filename, $type, $cid));
  820. $this->attachmentIds[] = $ret;
  821. return $ret;
  822. } catch (Swift_Message_MimeException $e) {
  823. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  824. return false;
  825. } catch (Swift_FileException $e) {
  826. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  827. return false;
  828. }
  829. }
  830. else
  831. {
  832. try {
  833. $ret = $this->message->attach(new Swift_Message_EmbeddedFile($data, $filename, $type, $cid));
  834. $this->attachmentIds[] = $ret;
  835. return $ret;
  836. } catch (Swift_Message_MimeException $e) {
  837. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  838. return false;
  839. } catch (Swift_FileException $e) {
  840. $this->setError("An attachment failed to attach:<br />" . $e->getMessage());
  841. return false;
  842. }
  843. }
  844. }
  845. /**
  846. * Add headers to the message
  847. * @param string The message headers to append, separated by CRLF
  848. * @deprecated
  849. */
  850. public function addHeaders($string)
  851. {
  852. //Split at the line ending only if it's not followed by LWSP (as in, a full header)
  853. $headers = preg_split("~\r?\n(?![ \t])~", $string);
  854. foreach ($headers as $header)
  855. {
  856. if (empty($header)) continue;
  857. //Get the bit before the colon
  858. $header_name = substr($header, 0, ($c_pos = strpos($header, ": ")));
  859. // ... and trim it away
  860. $header = substr($header, $c_pos+2);
  861. //Try splitting at "; " for attributes
  862. $attribute_pairs = preg_split("~\\s*;\\s+~", $header);
  863. //The value would always be right after the colon
  864. $header_value = $attribute_pairs[0];
  865. $this->message->headers->set($header_name, $header_value);
  866. unset($attribute_pairs[0]);
  867. foreach ($attribute_pairs as $pair)
  868. {
  869. //Now try finding the attribute name, and it's value (removing quotes)
  870. if (preg_match("~^(.*?)=(\"?)(.*?)\\2\\s*\$~", $pair, $matches))
  871. {
  872. try {
  873. $this->message->headers->setAttribute($header_name, $matches[1], $matches[3]);
  874. } catch (Swift_Message_MimeException $e) {
  875. $this->setError("There was a problem parsing or setting a header attribute:<br />" . $e->getMessage());
  876. //Ignored... it's EasySwift... C'mon ;)
  877. }
  878. }
  879. }
  880. }
  881. }
  882. /**
  883. * Set a header in the message
  884. * @param string The name of the header
  885. * @param string The value of the header (without attributes)
  886. * @see {addHeaderAttribute}
  887. */
  888. public function setHeader($name, $value)
  889. {
  890. $this->message->headers->set($name, $value);
  891. }
  892. /**
  893. * Set an attribute in the message headers
  894. * For example charset in Content-Type: text/html; charset=utf-8 set by $swift->setHeaderAttribute("Content-Type", "charset", "utf-8")
  895. * @param string The name of the header
  896. * @param string The name of the attribute
  897. * @param string The value of the attribute
  898. */
  899. public function setHeaderAttribute($name, $attribute, $value)
  900. {
  901. if ($this->message->headers->has($name))
  902. $this->message->headers->setAttribute($name, $attribute, $value);
  903. }
  904. /**
  905. * Send an email to a number of recipients
  906. * Returns the number of successful recipients, or FALSE on failure
  907. * @param mixed The recipients to send to. One of string, array, 2-dimensional array or Swift_Address
  908. * @param mixed The address to send from. string or Swift_Address
  909. * @param string The message subject
  910. * @param string The message body, optional
  911. * @return int
  912. */
  913. public function send($recipients, $from, $subject, $body=null)
  914. {
  915. $this->addTo($recipients);
  916. $sender = false;
  917. if (is_string($from)) $sender = $this->stringToAddress($from);
  918. elseif ($from instanceof Swift_Address) $sender = $from;
  919. if (!$sender) return false;
  920. $this->message->setSubject($subject);
  921. if ($body) $this->message->setBody($body);
  922. try {
  923. if (!$this->exactCopy && !$this->recipients->getCc() && !$this->recipients->getBcc())
  924. {
  925. $sent = $this->swift->batchSend($this->message, $this->recipients, $sender);
  926. }
  927. else
  928. {
  929. $sent = $this->swift->send($this->message, $this->recipients, $sender);
  930. }
  931. if ($this->autoFlush) $this->flush();
  932. return $sent;
  933. } catch (Swift_ConnectionException $e) {
  934. $this->setError("Sending failed:<br />" . $e->getMessage());
  935. return false;
  936. }
  937. }
  938. }