Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.14 KB | None | 0 0
  1. <?php
  2. /**
  3. To run and send an email:
  4. php -f emailTemplateGenerator.php -- -email raphael@me.com
  5. php -f emailTemplateGenerator.php -- -email raphael@me.com -template 46
  6. */
  7.  
  8. require_once 'abstract.php';
  9.  
  10. /**
  11. * Magento Email Templates Generator Script
  12. *
  13. * @category Mage
  14. * @package Mage_Shell
  15. * @author Raphael Petrini <contact@digital-pianism.com>
  16. */
  17.  
  18. class Mage_Shell_Emailtemplatesgenerator extends Mage_Shell_Abstract {
  19.  
  20. protected $limit = 0;
  21. protected $filter = "";
  22. protected $templateId = "";
  23.  
  24. // select * from core_config_data where path like '%trans_email%';
  25. const XML_PATH_EMAIL_SENDER_EMAIL = 'trans_email/ident_general/email';
  26. const XML_PATH_EMAIL_SENDER_NAME = 'trans_email/ident_general/name';
  27.  
  28. // Dummy data
  29. //protected $_dummyOrderIncrementId = 100018716;
  30. protected $_dummyOrderIncrementId = 100000011;
  31. protected $_dummyInvoiceIncrementId = 100000003;
  32. protected $_dummyCreditmemoIncrementId = 100000004;
  33. protected $_dummyShipmentIncrementId = 100000002;
  34.  
  35. /**
  36. * Dummy Templates Variables
  37. *
  38. * @var array
  39. */
  40. protected $templVars = array(
  41. //"sender_name" => "Alannah Hill",
  42. "product_name" => "Dummy Product",
  43. "product_url" => "dummy-product.html",
  44. "product_image" => "default",
  45. "message" => "Dummy Message",
  46. "increment_id" => 100000011,
  47. "order_date" => "2011-03-07 02:30:59",
  48. "direct_url" => "dummy-url.html",
  49. "firstname" => "First Name",
  50. "bday_code" => "dummycode",
  51. //"store_name" => "Alannah Hill",
  52. "certificate_numbers" => "dummy-certificate-number",
  53. "amount" => 10,
  54. "expire_on" => "2012-12-21 00:00:00",
  55. "custom_message" => "dummy custom message",
  56. "recipient_name" => "Dummy Recipient",
  57. "productname" => "Dummy Product",
  58. "specialprice" => 10,
  59. "cartprice" => 25,
  60. "extraproductcount" => 2,
  61. "discount" => 100,
  62. //"storeName" => "Alannah Hill",
  63. "orderId" => 100000011,
  64. // customer
  65. "customerName" => "Dummy Customer",
  66. "customerEmail" => "", // set via cli arg
  67. "customer_name" => "Dummy Customer",
  68. "custFirstName" => "Dummy First Name",
  69. "itemList" => "Dummy item list",
  70. "comment" => "dummy comment",
  71. "items" => "dummy items",
  72. "salable" => "yes",
  73. "viewOnSiteLink" => "dummy-link"
  74. );
  75.  
  76. /**
  77. * Run script
  78. *
  79. */
  80. public function run() {
  81.  
  82. //$theme = Mage::getStoreConfig('design/theme/skin');
  83. $theme = Mage::getSingleton('core/design_package')->setStore(1)->getTheme('template');
  84.  
  85. //$package = Mage::getStoreConfig('design/package/name');
  86. /** @var Mage_Core_Model_Design_Package $package */
  87. $package = Mage::getSingleton('core/design_package')->getPackageName();
  88.  
  89.  
  90. /*$themeArgs = array('locale', 'layout', 'template', 'default', 'skin');
  91. foreach($themeArgs as $arg) {
  92. $themeVal = Mage::getSingleton('core/design_package')->getTheme($arg);
  93. echo sprintf("package_theme[%s]=%s/%s\n", $arg, $package, $themeVal);
  94. }*/
  95.  
  96. // setup vars
  97. $this->templVars['sender_name'] = Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER_NAME);
  98. $this->templVars['store_name'] = Mage::app()->getStore()->getName();
  99. $this->templVars['storeName'] = Mage::app()->getStore()->getName();
  100.  
  101. if ($this->getArg('l')) {
  102. $this->limit = $this->getArg('l');
  103. }
  104.  
  105. if ($this->getArg('f')) {
  106. $this->filter = $this->getArg('f');
  107. }
  108.  
  109. // Display the help
  110. if ($this->getArg('help')) {
  111. $this->usageHelp();
  112. exit();
  113. }
  114.  
  115. // check email
  116. if (!$this->getArg('email')) {
  117. exit("Please specify a recipient email address e.g. php -f emailTemplateGenerator.php -- -email test@test.com \n");
  118. }
  119. elseif (!$this->isValidEmail($this->getArg('email'))) {
  120. exit("Please specify a valid email address e.g. name@domain.com \n");
  121. }
  122.  
  123. // template id
  124. $this->templateId = $this->getArg('template');
  125.  
  126. $this->templVars['customerEmail'] = $this->getArg('email');
  127. $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($this->templVars['customerEmail']);
  128. if ($customer) {
  129. //$this->templVars['customer_name'] = $customer->getName();
  130. //$this->templVars['customerName'] = $customer->getName();
  131. $this->templVars['customer'] = $customer;
  132. echo sprintf("customer_name: %s\n", $this->templVars['customer_name']);
  133. }
  134. else {
  135. echo sprintf("error: no customer for '%s'!\n", $this->templVars['customerEmail']);
  136. }
  137.  
  138. //$packages = array('default', 'aw_mobile');
  139. $packages = array('default');
  140.  
  141. // do for each theme
  142. foreach($packages as $package) {
  143. echo sprintf("set package_theme=%s/%s\n", $package, $theme);
  144. Mage::getDesign()->setArea('frontend')->setPackageName($package)->setTheme($theme);
  145. $cnt = $this->sendTemplateEmails();
  146. echo sprintf("sent %d emails\n", $cnt);
  147. }
  148.  
  149. //$templVars = $this->getTemplateEmailVars();
  150. //echo sprintf("templVars=%s\n", print_r($templVars, true));
  151. }
  152.  
  153. /**
  154.  
  155. */
  156. function getTemplateEmailVars() {
  157. $templVars = array();
  158. $templates = Mage::getResourceSingleton('core/email_template_collection');
  159. foreach($templates as $template) {
  160. //$templateId = $template->getTemplateId();
  161. $templateVariablesArray = Mage::helper('core')->jsonDecode($template->getOrigTemplateVariables());
  162. //echo sprintf("templateVariablesArray=%s\n", print_r($templateVariablesArray, true));
  163.  
  164. if (is_array($templateVariablesArray)) {
  165. foreach ($templateVariablesArray as $key => $val) {
  166. if (preg_match("/^var\s+[\w\-]+\./", $key)) {
  167. echo sprintf("key:%s=%s\n", $key, $val);
  168. }
  169. /*
  170. elseif (preg_match("/^var/", $key)) {
  171. echo sprintf("key:%s=%s\n", $key, $val);
  172. preg_match("/(var\s+)([\w\-]+)/", $key, $matches);
  173. echo sprintf("matches=%s\n", print_r($matches, true));
  174. if (!in_array($matches[2], $templVars)) {
  175. $templVars[] = $matches[2];
  176. }
  177. }
  178. */
  179. //echo sprintf("key:%s=%s\n", $key, $val);
  180. }
  181. }
  182. }
  183. sort($templVars);
  184. return $templVars;
  185. }
  186.  
  187. /**
  188. */
  189. function sendTemplateEmails() {
  190.  
  191. $templates = Mage::getResourceSingleton('core/email_template_collection');
  192.  
  193. if (empty($templates)) {
  194. die("no templates!");
  195. }
  196. $cnt = 0;
  197.  
  198. // Mage_Core_Model_Email_Template
  199. foreach($templates as $template) {
  200.  
  201. $templateId = $template->getTemplateId();
  202.  
  203. // load from db core_email_template
  204. //$emailTemplate = Mage::getModel('core/email_template')->loadByCode('custom_template');
  205.  
  206. // file system is the default template
  207. //$emailTemplate = Mage::getModel('core/email_template')->loadDefault('custom_template');
  208.  
  209. if ($this->templateId && $this->templateId != $templateId) {
  210. continue;
  211. }
  212.  
  213. if ($this->limit != 0 && $cnt >= $this->limit) {
  214. echo sprintf("email limit %d reached!\n", $this->limit);
  215. break;
  216. }
  217.  
  218. /*
  219. skip plain text email template (no need to test them)
  220. if ($template->getTemplateType() == Mage_Core_Model_Template::TYPE_TEXT) {
  221. echo sprintf("skip template type '%s'!\n", Mage_Core_Model_Template::TYPE_TEXT);
  222. continue;
  223. }
  224. */
  225.  
  226. if (!empty($this->filter) && !preg_match(sprintf("/%s/i", $this->filter), $template->getData('template_code')) ) {
  227. //echo sprintf("skip sending to template named '%s', failed filter '%s'\n", $template->getData('template_code'), $this->filter);
  228. continue;
  229. }
  230.  
  231. //$template->getData('template_code')
  232. //$template->getTemplateCode();
  233. echo sprintf("template .%03d: %d, %s, [%s]:%s\n", ($cnt + 1), $templateId, $template->getTemplateType(), $template->getData('template_code'), $template->getTemplateSubject());
  234.  
  235.  
  236. // Get the original email variables
  237. $templateVariablesArray = Mage::helper('core')->jsonDecode($template->getOrigTemplateVariables());
  238. //echo sprintf("templateVariablesArray=%s\n", print_r($templateVariablesArray, true));
  239.  
  240. // Some emails need entire objects (order, invoice, customer, subscriber, wishlist) to be passed as variables
  241. // We need to differentiate these different types of emails
  242. $isDataEmail = false;
  243. $isCustomerEmail = false;
  244. $isSubscriptionEmail = false;
  245. if (is_array($templateVariablesArray)) {
  246. foreach (array_keys($templateVariablesArray) as $key) {
  247. //echo sprintf("key=%s\n", $key);
  248. // If we found an original email variable that contains one of these keywords
  249. // We assume it's a data email
  250. if (strpos($key,'$order.') || strpos($key,'$invoice.') || strpos($key,'$shipment') || strpos($key,'$creditmemo.') || strpos($key,'$customer.') || strpos($key,'subscriber.')) {
  251. $isDataEmail = true;
  252. break;
  253. }
  254. else {
  255. continue;
  256. }
  257. }
  258. }
  259.  
  260. /**
  261. Note. var=$subscriber. is not adding to keys
  262. */
  263. $txt = $template->getTemplateText();
  264. //echo sprintf("TemplateText=%s\n", print_r($txt, true));
  265. if (preg_match("/subscriber\./", $txt)) {
  266. $isSubscriptionEmail = true;
  267. }
  268.  
  269. // Increase the count
  270. $cnt++;
  271.  
  272. // Get the template ID and sender
  273. $emailSender = Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER_EMAIL);
  274. echo sprintf("send email to '%s' from '%s'\n", $this->templVars['customerEmail'], $emailSender);
  275.  
  276. // If it's a data email
  277. if ($isDataEmail) {
  278. // We get the dummy data
  279. $order = Mage::getModel('sales/order')->loadByIncrementId($this->_dummyOrderIncrementId);
  280. $invoice = Mage::getModel('sales/order_invoice')->loadByIncrementId($this->_dummyInvoiceIncrementId);
  281. $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($this->_dummyShipmentIncrementId);
  282. //$creditmemo = Mage::getModel('sales/order_creditmemo')->loadByIncrementId($this->_dummyCreditmemoIncrementId);
  283. $creditmemo = Mage::getModel('sales/order_creditmemo')->load(4);
  284. $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($this->templVars['customerEmail']);
  285. //$subscriber = Mage::getModel('newsletter/subscriber')->setWebsiteId(1)->loadByEmail($this->templVars['customerEmail'])
  286.  
  287. // mailer used to send the email
  288. $mailer = Mage::getModel('core/email_template_mailer');
  289. echo(sprintf("%s->var=%s", __METHOD__, print_r($mailer, true)) );
  290.  
  291. // Add some info to the email
  292. $emailInfo = Mage::getModel('core/email_info');
  293. $emailInfo->addTo($this->templVars['customerEmail']);
  294. $mailer->addEmailInfo($emailInfo);
  295.  
  296. // Set all required params and send emails
  297. $mailer->setSender($emailSender);
  298. $mailer->setTemplateId($templateId);
  299.  
  300. // Retrieve specified view block from appropriate design package (depends on emulated store)
  301. $paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())->setIsSecureMode(true);
  302. $paymentBlock->getMethod()->setStore($storeId);
  303. $paymentBlockHtml = $paymentBlock->toHtml();
  304. $mailer->setTemplateParams(array(
  305. 'customer' => $customer,
  306. 'order' => $order,
  307. 'invoice' => $invoice,
  308. 'creditmemo' => $creditmemo,
  309. 'shipment' => $shipment,
  310. 'billing' => $order->getBillingAddress(),
  311. 'payment_html' => $paymentBlockHtml
  312. )
  313. );
  314. echo sprintf("1. %s->send()\n", get_class($mailer));
  315. $mailer->send();
  316. }
  317. else if ($isSubscriptionEmail) {
  318.  
  319. $subscriber = Mage::getModel('newsletter/subscriber')->setWebsiteId(1)->loadByEmail($this->templVars['customerEmail']);
  320. $subscriber->setData("subscriber_coupon", "testCode");
  321.  
  322. // Mailer used to send the email
  323. $mailer = Mage::getModel('core/email_template_mailer');
  324.  
  325. // Add some info to the email
  326. $emailInfo = Mage::getModel('core/email_info');
  327. $emailInfo->addTo($this->templVars['customerEmail']);
  328. $mailer->addEmailInfo($emailInfo);
  329.  
  330. // Set all required params and send emails
  331. $mailer->setSender($emailSender);
  332. $mailer->setTemplateId($templateId);
  333.  
  334. $mailer->setTemplateParams(array(
  335. 'subscriber' => $subscriber
  336. )
  337. );
  338. echo sprintf("2. %s->send()\n", get_class($mailer));
  339. echo(sprintf("%s->var=%s", __METHOD__, print_r($mailer, true)) );
  340. $mailer->send();
  341. }
  342. else {
  343. $recepientName = $this->templVars['customer_name'];
  344. $storeId = Mage::app()->getStore()->getId();
  345.  
  346. echo sprintf("3. %s->send()\n", get_class(Mage::getModel('core/email_template')));
  347. echo $templateId . "|". $emailSender . "|". $this->templVars['customerEmail'] . "|". $recepientName . "|". $storeId . "\n";
  348.  
  349. $sender = array(
  350. 'name' => Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER_NAME),
  351. 'email' => Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER_EMAIL)
  352. );
  353.  
  354. $translate = Mage::getSingleton('core/translate');
  355. // ->setTemplateSubject($template->getTemplateSubject())
  356. Mage::getModel('core/email_template')->sendTransactional(
  357. $templateId,
  358. $sender,
  359. $this->templVars['customerEmail'],
  360. $recepientName,
  361. $this->templVars,
  362. $storeId
  363. );
  364. $translate->setTranslateInline(true);
  365. }
  366. sleep(1);
  367. }
  368. return $cnt;
  369. }
  370.  
  371. /**
  372. * Retrieve Usage Help Message
  373. *
  374. */
  375. public function usageHelp()
  376. {
  377. return <<<USAGE
  378. Usage: php -f emailTemplateGenerator.php -- [options]
  379.  
  380. --email <email_address> Set up the recipient email
  381. --template <template_id> See Magento: System -> Transactional Emails
  382. help This help
  383.  
  384. <indexer> Comma separated indexer codes or value "all" for all indexers
  385.  
  386. USAGE;
  387. }
  388.  
  389. /**
  390. * Validate Email Address
  391. *
  392. */
  393. function isValidEmail($emailRecipient)
  394. {
  395. return filter_var($emailRecipient, FILTER_VALIDATE_EMAIL);
  396. }
  397. }
  398.  
  399. $shell = new Mage_Shell_Emailtemplatesgenerator();
  400. try {
  401. $shell->run();
  402. }
  403. catch (Exception $e) {
  404. echo 'Caught exception: ' . $e->getMessage();
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement