PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Mail.php

https://github.com/netplayer/PrestaShop
PHP | 460 lines | 334 code | 61 blank | 65 comment | 114 complexity | 10d11b54abf86edbfbe957edf9e05555 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. include_once(_PS_SWIFT_DIR_.'Swift.php');
  27. include_once(_PS_SWIFT_DIR_.'Swift/Connection/SMTP.php');
  28. include_once(_PS_SWIFT_DIR_.'Swift/Connection/NativeMail.php');
  29. include_once(_PS_SWIFT_DIR_.'Swift/Plugin/Decorator.php');
  30. class MailCore
  31. {
  32. const TYPE_HTML = 1;
  33. const TYPE_TEXT = 2;
  34. const TYPE_BOTH = 3;
  35. /**
  36. * Send Email
  37. *
  38. * @param int $id_lang Language of the email (to translate the template)
  39. * @param string $template Template: the name of template not be a var but a string !
  40. * @param string $subject
  41. * @param string $template_vars
  42. * @param string $to
  43. * @param string $to_name
  44. * @param string $from
  45. * @param string $from_name
  46. * @param array $file_attachment Array with three parameters (content, mime and name). You can use an array of array to attach multiple files
  47. * @param bool $modeSMTP
  48. * @param string $template_path
  49. * @param bool $die
  50. * @param string $bcc Bcc recipient
  51. */
  52. public static function Send($id_lang, $template, $subject, $template_vars, $to,
  53. $to_name = null, $from = null, $from_name = null, $file_attachment = null, $mode_smtp = null,
  54. $template_path = _PS_MAIL_DIR_, $die = false, $id_shop = null, $bcc = null)
  55. {
  56. $configuration = Configuration::getMultiple(array(
  57. 'PS_SHOP_EMAIL',
  58. 'PS_MAIL_METHOD',
  59. 'PS_MAIL_SERVER',
  60. 'PS_MAIL_USER',
  61. 'PS_MAIL_PASSWD',
  62. 'PS_SHOP_NAME',
  63. 'PS_MAIL_SMTP_ENCRYPTION',
  64. 'PS_MAIL_SMTP_PORT',
  65. 'PS_MAIL_TYPE'
  66. ), null, null, $id_shop);
  67. // Returns immediatly if emails are deactivated
  68. if ($configuration['PS_MAIL_METHOD'] == 3)
  69. return true;
  70. $theme_path = _PS_THEME_DIR_;
  71. // Get the path of theme by id_shop if exist
  72. if (is_numeric($id_shop) && $id_shop)
  73. {
  74. $shop = new Shop((int)$id_shop);
  75. $theme_name = $shop->getTheme();
  76. if (_THEME_NAME_ != $theme_name)
  77. $theme_path = _PS_ROOT_DIR_.'/themes/'.$theme_name.'/';
  78. }
  79. if (!isset($configuration['PS_MAIL_SMTP_ENCRYPTION']))
  80. $configuration['PS_MAIL_SMTP_ENCRYPTION'] = 'off';
  81. if (!isset($configuration['PS_MAIL_SMTP_PORT']))
  82. $configuration['PS_MAIL_SMTP_PORT'] = 'default';
  83. // Sending an e-mail can be of vital importance for the merchant, when his password is lost for example, so we must not die but do our best to send the e-mail
  84. if (!isset($from) || !Validate::isEmail($from))
  85. $from = $configuration['PS_SHOP_EMAIL'];
  86. if (!Validate::isEmail($from))
  87. $from = null;
  88. // $from_name is not that important, no need to die if it is not valid
  89. if (!isset($from_name) || !Validate::isMailName($from_name))
  90. $from_name = $configuration['PS_SHOP_NAME'];
  91. if (!Validate::isMailName($from_name))
  92. $from_name = null;
  93. // It would be difficult to send an e-mail if the e-mail is not valid, so this time we can die if there is a problem
  94. if (!is_array($to) && !Validate::isEmail($to))
  95. {
  96. Tools::dieOrLog(Tools::displayError('Error: parameter "to" is corrupted'), $die);
  97. return false;
  98. }
  99. if (!is_array($template_vars))
  100. $template_vars = array();
  101. // Do not crash for this error, that may be a complicated customer name
  102. if (is_string($to_name) && !empty($to_name) && !Validate::isMailName($to_name))
  103. $to_name = null;
  104. if (!Validate::isTplName($template))
  105. {
  106. Tools::dieOrLog(Tools::displayError('Error: invalid e-mail template'), $die);
  107. return false;
  108. }
  109. if (!Validate::isMailSubject($subject))
  110. {
  111. Tools::dieOrLog(Tools::displayError('Error: invalid e-mail subject'), $die);
  112. return false;
  113. }
  114. /* Construct multiple recipients list if needed */
  115. $to_list = new Swift_RecipientList();
  116. if (is_array($to) && isset($to))
  117. {
  118. foreach ($to as $key => $addr)
  119. {
  120. $addr = trim($addr);
  121. if (!Validate::isEmail($addr))
  122. {
  123. Tools::dieOrLog(Tools::displayError('Error: invalid e-mail address'), $die);
  124. return false;
  125. }
  126. if (is_array($to_name))
  127. {
  128. if ($to_name && is_array($to_name) && Validate::isGenericName($to_name[$key]))
  129. $to_name = $to_name[$key];
  130. }
  131. if ($to_name == null || $to_name == $addr)
  132. $to_name = '';
  133. else
  134. $to_name = self::mimeEncode($to_name);
  135. $to_list->addTo($addr, $to_name);
  136. }
  137. $to_plugin = $to[0];
  138. } else {
  139. /* Simple recipient, one address */
  140. $to_plugin = $to;
  141. if ($to_name == null || $to_name == $to)
  142. $to_name = '';
  143. else
  144. $to_name = self::mimeEncode($to_name);
  145. $to_list->addTo($to, $to_name);
  146. }
  147. if(isset($bcc)) {
  148. $to_list->addBcc($bcc);
  149. }
  150. $to = $to_list;
  151. try {
  152. /* Connect with the appropriate configuration */
  153. if ($configuration['PS_MAIL_METHOD'] == 2)
  154. {
  155. if (empty($configuration['PS_MAIL_SERVER']) || empty($configuration['PS_MAIL_SMTP_PORT']))
  156. {
  157. Tools::dieOrLog(Tools::displayError('Error: invalid SMTP server or SMTP port'), $die);
  158. return false;
  159. }
  160. $connection = new Swift_Connection_SMTP($configuration['PS_MAIL_SERVER'], $configuration['PS_MAIL_SMTP_PORT'],
  161. ($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'ssl') ? Swift_Connection_SMTP::ENC_SSL :
  162. (($configuration['PS_MAIL_SMTP_ENCRYPTION'] == 'tls') ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_OFF));
  163. $connection->setTimeout(4);
  164. if (!$connection)
  165. return false;
  166. if (!empty($configuration['PS_MAIL_USER']))
  167. $connection->setUsername($configuration['PS_MAIL_USER']);
  168. if (!empty($configuration['PS_MAIL_PASSWD']))
  169. $connection->setPassword($configuration['PS_MAIL_PASSWD']);
  170. }
  171. else
  172. $connection = new Swift_Connection_NativeMail();
  173. if (!$connection)
  174. return false;
  175. $swift = new Swift($connection, Configuration::get('PS_MAIL_DOMAIN', null, null, $id_shop));
  176. /* Get templates content */
  177. $iso = Language::getIsoById((int)$id_lang);
  178. if (!$iso)
  179. {
  180. Tools::dieOrLog(Tools::displayError('Error - No ISO code for email'), $die);
  181. return false;
  182. }
  183. $template = $iso.'/'.$template;
  184. $module_name = false;
  185. $override_mail = false;
  186. // get templatePath
  187. if (preg_match('#'.__PS_BASE_URI__.'modules/#', str_replace(DIRECTORY_SEPARATOR, '/', $template_path)) && preg_match('#modules/([a-z0-9_-]+)/#ui', str_replace(DIRECTORY_SEPARATOR, '/',$template_path), $res))
  188. $module_name = $res[1];
  189. if ($module_name !== false && (file_exists($theme_path.'modules/'.$module_name.'/mails/'.$template.'.txt') ||
  190. file_exists($theme_path.'modules/'.$module_name.'/mails/'.$template.'.html')))
  191. $template_path = $theme_path.'modules/'.$module_name.'/mails/';
  192. elseif (file_exists($theme_path.'mails/'.$template.'.txt') || file_exists($theme_path.'mails/'.$template.'.html'))
  193. {
  194. $template_path = $theme_path.'mails/';
  195. $override_mail = true;
  196. }
  197. if (!file_exists($template_path.$template.'.txt') && ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT))
  198. {
  199. Tools::dieOrLog(Tools::displayError('Error - The following e-mail template is missing:').' '.$template_path.$template.'.txt', $die);
  200. return false;
  201. }
  202. else if (!file_exists($template_path.$template.'.html') && ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML))
  203. {
  204. Tools::dieOrLog(Tools::displayError('Error - The following e-mail template is missing:').' '.$template_path.$template.'.html', $die);
  205. return false;
  206. }
  207. $template_html = file_get_contents($template_path.$template.'.html');
  208. $template_txt = strip_tags(html_entity_decode(file_get_contents($template_path.$template.'.txt'), null, 'utf-8'));
  209. if ($override_mail && file_exists($template_path.$iso.'/lang.php'))
  210. include_once($template_path.$iso.'/lang.php');
  211. else if ($module_name && file_exists($theme_path.'mails/'.$iso.'/lang.php'))
  212. include_once($theme_path.'mails/'.$iso.'/lang.php');
  213. else if (file_exists(_PS_MAIL_DIR_.$iso.'/lang.php'))
  214. include_once(_PS_MAIL_DIR_.$iso.'/lang.php');
  215. else
  216. {
  217. Tools::dieOrLog(Tools::displayError('Error - The language file is missing for:').' '.$iso, $die);
  218. return false;
  219. }
  220. /* Create mail and attach differents parts */
  221. $message = new Swift_Message('['.Configuration::get('PS_SHOP_NAME', null, null, $id_shop).'] '.$subject);
  222. $message->setCharset('utf-8');
  223. /* Set Message-ID - getmypid() is blocked on some hosting */
  224. $message->setId(Mail::generateId());
  225. $message->headers->setEncoding('Q');
  226. $template_vars = array_map(array('Tools', 'htmlentitiesDecodeUTF8'), $template_vars);
  227. $template_vars = array_map(array('Tools', 'stripslashes'), $template_vars);
  228. if (Configuration::get('PS_LOGO_MAIL') !== false && file_exists(_PS_IMG_DIR_.Configuration::get('PS_LOGO_MAIL', null, null, $id_shop)))
  229. $logo = _PS_IMG_DIR_.Configuration::get('PS_LOGO_MAIL', null, null, $id_shop);
  230. else
  231. {
  232. if (file_exists(_PS_IMG_DIR_.Configuration::get('PS_LOGO', null, null, $id_shop)))
  233. $logo = _PS_IMG_DIR_.Configuration::get('PS_LOGO', null, null, $id_shop);
  234. else
  235. $template_vars['{shop_logo}'] = '';
  236. }
  237. ShopUrl::cacheMainDomainForShop((int)$id_shop);
  238. /* don't attach the logo as */
  239. if (isset($logo))
  240. $template_vars['{shop_logo}'] = $message->attach(new Swift_Message_EmbeddedFile(new Swift_File($logo), null, ImageManager::getMimeTypeByExtension($logo)));
  241. if ((Context::getContext()->link instanceof Link) === false)
  242. Context::getContext()->link = new Link();
  243. $template_vars['{shop_name}'] = Tools::safeOutput(Configuration::get('PS_SHOP_NAME', null, null, $id_shop));
  244. $template_vars['{shop_url}'] = Context::getContext()->link->getPageLink('index', true, Context::getContext()->language->id, null, false, $id_shop);
  245. $template_vars['{my_account_url}'] = Context::getContext()->link->getPageLink('my-account', true, Context::getContext()->language->id, null, false, $id_shop);
  246. $template_vars['{guest_tracking_url}'] = Context::getContext()->link->getPageLink('guest-tracking', true, Context::getContext()->language->id, null, false, $id_shop);
  247. $template_vars['{history_url}'] = Context::getContext()->link->getPageLink('history', true, Context::getContext()->language->id, null, false, $id_shop);
  248. $template_vars['{color}'] = Tools::safeOutput(Configuration::get('PS_MAIL_COLOR', null, null, $id_shop));
  249. $swift->attachPlugin(new Swift_Plugin_Decorator(array($to_plugin => $template_vars)), 'decorator');
  250. if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_TEXT)
  251. $message->attach(new Swift_Message_Part($template_txt, 'text/plain', '8bit', 'utf-8'));
  252. if ($configuration['PS_MAIL_TYPE'] == Mail::TYPE_BOTH || $configuration['PS_MAIL_TYPE'] == Mail::TYPE_HTML)
  253. $message->attach(new Swift_Message_Part($template_html, 'text/html', '8bit', 'utf-8'));
  254. if ($file_attachment && !empty($file_attachment))
  255. {
  256. // Multiple attachments?
  257. if (!is_array(current($file_attachment)))
  258. $file_attachment = array($file_attachment);
  259. foreach ($file_attachment as $attachment)
  260. if (isset($attachment['content']) && isset($attachment['name']) && isset($attachment['mime']))
  261. $message->attach(new Swift_Message_Attachment($attachment['content'], $attachment['name'], $attachment['mime']));
  262. }
  263. /* Send mail */
  264. $send = $swift->send($message, $to, new Swift_Address($from, $from_name));
  265. $swift->disconnect();
  266. ShopUrl::resetMainDomainCache();
  267. return $send;
  268. }
  269. catch (Swift_Exception $e) {
  270. return false;
  271. }
  272. }
  273. public static function sendMailTest($smtpChecked, $smtpServer, $content, $subject, $type, $to, $from, $smtpLogin, $smtpPassword, $smtpPort = 25, $smtpEncryption)
  274. {
  275. $swift = null;
  276. $result = false;
  277. try
  278. {
  279. if ($smtpChecked)
  280. {
  281. $smtp = new Swift_Connection_SMTP($smtpServer, $smtpPort, ($smtpEncryption == 'off') ?
  282. Swift_Connection_SMTP::ENC_OFF : (($smtpEncryption == 'tls') ? Swift_Connection_SMTP::ENC_TLS : Swift_Connection_SMTP::ENC_SSL));
  283. $smtp->setUsername($smtpLogin);
  284. $smtp->setpassword($smtpPassword);
  285. $smtp->setTimeout(5);
  286. $swift = new Swift($smtp, Configuration::get('PS_MAIL_DOMAIN'));
  287. }
  288. else
  289. $swift = new Swift(new Swift_Connection_NativeMail(), Configuration::get('PS_MAIL_DOMAIN'));
  290. $message = new Swift_Message($subject, $content, $type);
  291. if ($swift->send($message, $to, $from))
  292. $result = true;
  293. $swift->disconnect();
  294. }
  295. catch (Swift_ConnectionException $e)
  296. {
  297. $result = $e->getMessage();
  298. }
  299. catch (Swift_Message_MimeException $e)
  300. {
  301. $result = $e->getMessage();
  302. }
  303. return $result;
  304. }
  305. /**
  306. * This method is used to get the translation for email Object.
  307. * For an object is forbidden to use htmlentities,
  308. * we have to return a sentence with accents.
  309. *
  310. * @param string $string raw sentence (write directly in file)
  311. */
  312. public static function l($string, $id_lang = null, Context $context = null)
  313. {
  314. global $_LANGMAIL;
  315. if (!$context)
  316. $context = Context::getContext();
  317. $key = str_replace('\'', '\\\'', $string);
  318. if ($id_lang == null)
  319. $id_lang = (!isset($context->language) || !is_object($context->language)) ? (int)Configuration::get('PS_LANG_DEFAULT') : (int)$context->language->id;
  320. $iso_code = Language::getIsoById((int)$id_lang);
  321. $file_core = _PS_ROOT_DIR_.'/mails/'.$iso_code.'/lang.php';
  322. if (Tools::file_exists_cache($file_core) && empty($_LANGMAIL))
  323. include_once($file_core);
  324. $file_theme = _PS_THEME_DIR_.'mails/'.$iso_code.'/lang.php';
  325. if (Tools::file_exists_cache($file_theme))
  326. include_once($file_theme);
  327. if (!is_array($_LANGMAIL))
  328. return (str_replace('"', '&quot;', $string));
  329. if (array_key_exists($key, $_LANGMAIL) && !empty($_LANGMAIL[$key]))
  330. $str = $_LANGMAIL[$key];
  331. else
  332. $str = $string;
  333. return str_replace('"', '&quot;', stripslashes($str));
  334. }
  335. /* Rewrite of Swift_Message::generateId() without getmypid() */
  336. protected static function generateId($idstring = null)
  337. {
  338. $midparams = array(
  339. "utctime" => gmstrftime("%Y%m%d%H%M%S"),
  340. "randint" => mt_rand(),
  341. "customstr" => (preg_match("/^(?<!\\.)[a-z0-9\\.]+(?!\\.)\$/iD", $idstring) ? $idstring : "swift") ,
  342. "hostname" => (isset($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : php_uname("n")),
  343. );
  344. return vsprintf("<%s.%d.%s@%s>", $midparams);
  345. }
  346. public static function isMultibyte($data)
  347. {
  348. $length = strlen($data);
  349. for ($i = 0; $i < $length; $i++)
  350. {
  351. $result = ord(($data[$i]));
  352. if ($result > 128)
  353. {
  354. return true;
  355. }
  356. }
  357. return false;
  358. }
  359. public static function mimeEncode($string, $charset = 'UTF-8', $newline = "\r\n")
  360. {
  361. if (!self::isMultibyte($string) && strlen($string) < 75)
  362. {
  363. return $string;
  364. }
  365. $charset = strtoupper($charset);
  366. $start = '=?' . $charset . '?B?';
  367. $end = '?=';
  368. $sep = $end . $newline . ' ' . $start;
  369. $length = 75 - strlen($start) - strlen($end);
  370. $length = $length - ($length % 4);
  371. if ($charset === 'UTF-8')
  372. {
  373. $parts = array();
  374. $maxchars = floor(($length * 3) / 4);
  375. $stringLength = strlen($string);
  376. while ($stringLength > $maxchars)
  377. {
  378. $i = (int)$maxchars;
  379. $result = ord($string[$i]);
  380. while ($result >= 128 && $result <= 191)
  381. {
  382. $i--;
  383. $result = ord($string[$i]);
  384. }
  385. $parts[] = base64_encode(substr($string, 0, $i));
  386. $string = substr($string, $i);
  387. $stringLength = strlen($string);
  388. }
  389. $parts[] = base64_encode($string);
  390. $string = implode($sep, $parts);
  391. }
  392. else
  393. {
  394. $string = chunk_split(base64_encode($string), $length, $sep);
  395. $string = preg_replace('/' . preg_quote($sep) . '$/', '', $string);
  396. }
  397. return $start . $string . $end;
  398. }
  399. }