PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/magmi/plugins/base/general/emailreport/emailreport.php

https://bitbucket.org/jit_bec/shopifine
PHP | 133 lines | 114 code | 18 blank | 1 comment | 11 complexity | d3bd800eddc29bf6427cd11f3f03c5e3 MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. class EmailReportPlugin extends Magmi_GeneralImportPlugin
  3. {
  4. protected $_attach;
  5. public function initialize($params)
  6. {
  7. $this->_attach=array();
  8. }
  9. public function getPluginInfo()
  10. {
  11. return array(
  12. "name" => "Import Report Mail Notifier",
  13. "author" => "Dweeves",
  14. "version" => "1.0.0",
  15. "url"=>"http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Import_report_mail_notifier"
  16. );
  17. }
  18. public function send_email($to, $from, $from_name, $subject, $message, $attachments=false)
  19. {
  20. $headers = "From: ".$from_name."<".$from.">\n";
  21. $headers .= "Reply-To: ".$from_name."<".$from.">\n";
  22. $headers .= "Return-Path: ".$from_name."<".$from.">\n";
  23. $headers .= "Message-ID: <".time()."-".$from.">\n";
  24. $headers .= "X-Mailer: PHP v".phpversion();
  25. $msg_txt="";
  26. $email_txt = $message;
  27. $semi_rand = md5(time());
  28. $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  29. $headers .= "\nMIME-Version: 1.0\n" .
  30. "Content-Type: multipart/mixed;\n" .
  31. " boundary=\"{$mime_boundary}\"";
  32. $email_txt .= $msg_txt;
  33. $email_message = $email_txt;
  34. $email_message .= "This is a multi-part message in MIME format.\n\n" .
  35. "--{$mime_boundary}\n" .
  36. "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
  37. "Content-Transfer-Encoding: 7bit\n\n" .
  38. $email_txt . "\n\n";
  39. $attachments=$this->_attach;
  40. if ($attachments !== false)
  41. {
  42. for($i=0; $i < count($attachments); $i++)
  43. {
  44. if (is_file($attachments[$i]))
  45. {
  46. $fileatt = $attachments[$i];
  47. $fileatt_type = "application/octet-stream";
  48. $start= strrpos($attachments[$i], '/') == -1 ? strrpos($attachments[$i], '//') : strrpos($attachments[$i], '/')+1;
  49. $fileatt_name = substr($attachments[$i], $start, strlen($attachments[$i]));
  50. $file = fopen($fileatt,'rb');
  51. $data = fread($file,filesize($fileatt));
  52. fclose($file);
  53. $data = chunk_split(base64_encode($data));
  54. $email_message .= "--{$mime_boundary}\n" .
  55. "Content-Type: {$fileatt_type};\n" .
  56. " name=\"{$fileatt_name}\"\n" .
  57. "Content-Transfer-Encoding: base64\n\n" .
  58. $data . "\n\n";
  59. }
  60. }
  61. }
  62. $email_message .= "--{$mime_boundary}--\n";
  63. $this->log("Sending report to : $to","info");
  64. $ok= mail($to, $subject, $email_message, $headers);
  65. return $ok;
  66. }
  67. public function addAttachment($fname)
  68. {
  69. $this->_attach[]=$fname;
  70. $this->_attach=array_unique($this->_attach);
  71. }
  72. public function getPluginParams($params)
  73. {
  74. $pp=array();
  75. foreach($params as $k=>$v)
  76. {
  77. if(preg_match("/^EMAILREP:.*$/",$k))
  78. {
  79. $pp[$k]=$v;
  80. }
  81. }
  82. return $pp;
  83. }
  84. public function afterImport()
  85. {
  86. $eng=$this->_callers[0];
  87. if($this->getParam("EMAILREP:to","")!="" && $this->getParam("EMAILREP:from","")!="")
  88. {
  89. if($this->getParam("EMAILREP:attachcsv",false)==true)
  90. {
  91. $ds=$eng->getPluginInstanceByClassName("datasources","Magmi_CSVDataSource");
  92. if($ds!=null)
  93. {
  94. $csvfile=$ds->getParam("CSV:filename");
  95. $this->addAttachment($csvfile);
  96. }
  97. }
  98. if($this->getParam("EMAILREP:attachlog",false)==true)
  99. {
  100. //copy magmi report
  101. $pfile=Magmi_StateManager::getProgressFile(true);
  102. $this->addAttachment($pfile);
  103. }
  104. $ok=$this->send_email($this->getParam("EMAILREP:to"),
  105. $this->getParam("EMAILREP:from"),
  106. $this->getParam("EMAILREP:from_alias",""),
  107. $this->getParam("EMAILREP:subject","Magmi import report"),
  108. $this->getParam("EMAILREP:body","report attached"),$this->_attach);
  109. if(!$ok)
  110. {
  111. $this->log("Cannot send email","error");
  112. }
  113. }
  114. }
  115. }