src/ApplicationBundle/Modules/System/Email.php line 117

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: ehsan_pc
  5.  * Date: 3/14/2016
  6.  * Time: 4:22 PM
  7.  */
  8. namespace ApplicationBundle\Modules\System;
  9. use ApplicationBundle\Constants\AccountsConstant;
  10. use ApplicationBundle\Constants\GeneralConstant;
  11. use ApplicationBundle\Entity\AccAccountsHead;
  12. use ApplicationBundle\Entity\AccPrefix;
  13. use ApplicationBundle\Entity\AccSuppliers;
  14. use ApplicationBundle\Entity\AccClosingBalance;
  15. use ApplicationBundle\Entity\AccTransactionDetails;
  16. use ApplicationBundle\Entity\AccTransactions;
  17. use ApplicationBundle\Entity\Approval;
  18. use ApplicationBundle\Entity\ApprovalSettings;
  19. use ApplicationBundle\Entity\PaymentLog;
  20. use ApplicationBundle\Entity\PurchaseInvoice;
  21. use ApplicationBundle\Entity\PurchaseInvoiceItem;
  22. use ApplicationBundle\Helper\Generic;
  23. use ApplicationBundle\Modules\Accounts\Accounts;
  24. use ApplicationBundle\Modules\Inventory\Inventory;
  25. use ApplicationBundle\Modules\Sales\Package;
  26. use ApplicationBundle\Modules\Sales\SalesOrderM;
  27. use ApplicationBundle\Modules\User\Company;
  28. use ApplicationBundle\Modules\User\Users;
  29. use ApplicationBundle\Modules\System\System;
  30. use Symfony\Component\HttpFoundation\Session\Session;
  31. use Twig\Environment;
  32. class Email
  33. {
  34.     public $secretKey "";
  35.     private $em;
  36.     private $kernelRoot;
  37. //    private $dm;
  38.     private $router;
  39.     private $session;
  40. //    private $sha256salted_encoder;
  41.     private $email_enabled;
  42.     private $mailer;
  43.     private $twig;
  44.     public function __construct($router$session$db_credentials$kernel_Root$email_enabled$mailerEnvironment $twig)
  45.     {
  46. //        $this->secretKey=$secret;
  47.         $this->em $db_credentials;
  48. //        $this->dm=$odm_credentials;
  49.         $this->router $router;
  50.         $this->session $session;
  51. //        $this->sha256salted_encoder = $sha256salted_encoder;
  52.         $this->email_enabled $email_enabled;
  53.         $this->mailer $mailer;
  54.         $this->twig $twig;
  55.         $this->kernelRoot $kernel_Root;
  56.     }
  57.     public function isEmailEnabled($em$user_id 0)
  58.     {
  59.         $data = [];
  60.         return $this->email_enabled;
  61. //1st get login ids basedon user id
  62.     }
  63.     public function sendMyMail($data)
  64.     {
  65. //        $data=[];
  66.         if ($this->email_enabled == 0)
  67.             return false;
  68.         $senderSettingHash '_DEFAULT_'//form parameters settings ultimate fallback
  69.         if (isset($data['senderHash']))
  70.             $senderSettingHash $data['senderHash'];
  71.         $em $this->em;
  72.         $fromAddress 'accounts@ourhoneybee.eu';
  73.         $toAddress = isset($data['toAddress']) ? $data['toAddress'] : (isset($data['forwardToMailAddress'])?$data['forwardToMailAddress']:"");
  74.         $subject = isset($data['subject']) ? $data['subject'] : 'Registration to Entity test 2';
  75.         $toAddress explode(','$toAddress);
  76.         $newToAddress=[];
  77.         foreach ($toAddress as $t)
  78.         {
  79.             if($t != null && $t != '')
  80.                 $newToAddress[]=filter_var($tFILTER_SANITIZE_EMAIL);
  81.         }
  82.         $toAddress=$newToAddress;
  83. //        $toAddress = filter_var($toAddress, FILTER_SANITIZE_EMAIL);
  84. //        $toAddress = filter_var($toAddress, FILTER_VALIDATE_EMAIL);
  85.         $encryptionMethod=isset($data['encryptionMethod'])?$data['encryptionMethod']:'ssl';
  86.         ////**** Encryption method must be added on the table and then used
  87.         if (empty($toAddress)) {
  88.             return false;
  89.         }
  90.         if ($senderSettingHash == '_DEFAULT_') {
  91.             $transport $this->mailer->getTransport();
  92.             $fromAddress $transport->getUserName();
  93.         }
  94.         else {
  95.             //1st check if setting is defined in data or user wise
  96.             if ($senderSettingHash == '_CUSTOM_') {
  97.                 $transport =(new  \Swift_SmtpTransport($data['smtpServer'], $data['smtpPort'],$encryptionMethod))
  98.                     ->setUsername($data['userName'])
  99.                     ->setPassword($data['password']);
  100.                 $fromAddress $data['fromAddress'];
  101.             } else {
  102.                 //1st check if the actual hash exists if not then check for general and if not then just use the default
  103.                 $senderSettingData $em->getRepository('ApplicationBundle\\Entity\\EmailSenderSettings')->findOneBy(
  104.                     array(
  105.                         'hash' => $senderSettingHash,
  106.                         'valid' => 1
  107.                     )
  108.                 );
  109.                 if ($senderSettingData) {
  110.                     if($senderSettingData->getEncryptionMethod()!=null && $senderSettingData->getEncryptionMethod()!='')
  111.                         $encryptionMethod=$senderSettingData->getEncryptionMethod();
  112.                     $transport = (new  \Swift_SmtpTransport($senderSettingData->getSmtpServer(), $senderSettingData->getSmtpPort(),$encryptionMethod))
  113.                         ->setUsername($senderSettingData->getUserName())
  114.                         ->setPassword($senderSettingData->getPassword());
  115.                     $fromAddress $senderSettingData->getEmail();
  116.                 } else {
  117.                     $senderSettingData $em->getRepository('ApplicationBundle\\Entity\\EmailSenderSettings')->findOneBy(
  118.                         array(
  119.                             'hash' => '_GENERAL_',
  120.                             'valid' => 1
  121.                         )
  122.                     );
  123.                     if ($senderSettingData) {
  124.                         if($senderSettingData->getEncryptionMethod()!=null && $senderSettingData->getEncryptionMethod()!='')
  125.                             $encryptionMethod=$senderSettingData->getEncryptionMethod();
  126.                         $transport = (new  \Swift_SmtpTransport($senderSettingData->getSmtpServer(), $senderSettingData->getSmtpPort(),$encryptionMethod))
  127.                             ->setUsername($senderSettingData->getUserName())
  128.                             ->setPassword($senderSettingData->getPassword());
  129.                         $fromAddress $senderSettingData->getEmail();
  130.                     } else {
  131.                         $transport $this->mailer->getTransport();
  132.                         $fromAddress $transport->getUserName();
  133.                     }
  134.                 }
  135.             }
  136.         }
  137. //        $transport
  138. //            ->setUserName(...)
  139. //            ->setPassword(...)
  140. //            ->setHost(...)
  141. //            ->setPort(...)
  142. //            ->setEncryption(...);
  143.         $mailer = new \Swift_Mailer($transport);
  144. //        $mailer = $this->mailer;
  145. //        $mailer->setTransport($transport);
  146. //        \Swift_Image::fromPath('path/to/image.jpg')) $company_data->getImage()
  147.         $companyId 1;
  148.         if (isset($data['companyId'])) {
  149.             $companyId $data['companyId'];
  150.         }
  151.         $companyData = [];
  152.         $emailmessage = (new \Swift_Message($subject))
  153.             ->setFrom($fromAddress)
  154.             ->setTo($toAddress);
  155.         $ccAddress = isset($data['ccAddress']) ? $data['ccAddress'] : (isset($data['cc']) ? $data['cc'] : '');
  156.         $bccAddress = isset($data['bccAddress']) ? $data['bccAddress'] : (isset($data['bcc']) ? $data['bcc'] : '');
  157.         $normalizeRecipients = function ($addresses) {
  158.             if (is_array($addresses)) {
  159.                 $addresses implode(','$addresses);
  160.             }
  161.             $addresses explode(',', (string) $addresses);
  162.             $normalized = [];
  163.             foreach ($addresses as $address) {
  164.                 $address trim((string) $address);
  165.                 if ($address === '') {
  166.                     continue;
  167.                 }
  168.                 $sanitized filter_var($addressFILTER_SANITIZE_EMAIL);
  169.                 if ($sanitized !== '') {
  170.                     $normalized[] = $sanitized;
  171.                 }
  172.             }
  173.             return $normalized;
  174.         };
  175.         $ccRecipients $normalizeRecipients($ccAddress);
  176.         if (!empty($ccRecipients)) {
  177.             $emailmessage->setCc($ccRecipients);
  178.         }
  179.         $bccRecipients $normalizeRecipients($bccAddress);
  180.         if (!empty($bccRecipients)) {
  181.             $emailmessage->setBcc($bccRecipients);
  182.         }
  183.         $companyImageCidSrc '';
  184.         if (isset($data['embedCompanyImage'])) {
  185.             if ($data['embedCompanyImage']==1) {
  186.             $companyImagePath '';
  187.             if (isset($data['companyImagePath']))
  188.                 $companyImagePath $data['companyImagePath'];
  189.             else {
  190.                 $companyData Company::getCompanyData($em$companyId);
  191.                 $companyImagePath $companyData->getImage();
  192.             }
  193.             if ($companyImagePath != '') {
  194.                 $companyImageCidSrc $emailmessage->embed(\Swift_Image::fromPath($this->kernelRoot '/../web' $companyImagePath));
  195.                 if (isset($data['templateData']))
  196.                     $data['templateData']['companyImageCidSrc'] = $companyImageCidSrc;
  197.             }
  198.         }
  199.         }
  200.         if (isset($data['templateData']))
  201.             $data['templateData']['isEmailBody'] = 1;
  202.         $body_is_empty=1;
  203.         $email_body='';
  204.         if(isset($data['emailBody']))
  205.             $email_body=$data['emailBody'];
  206.         if($email_body=='')
  207.             $body_is_empty=1;
  208.         else
  209.             $body_is_empty=0;
  210.         $emailmessage
  211.             ->setBody(
  212.                 $body_is_empty==0$data['emailBody'] :
  213.                     $this->twig->render(
  214.                         isset($data['mailTemplate']) ? $data['mailTemplate'] : '@Application/email/general/test.html.twig',
  215.                         isset($data['templateData']) ? $data['templateData'] : array('name' => 'poka',
  216.                             'companyData' => $companyData,
  217.                             'userName' => "hello poka",
  218.                             'password' => "mia",
  219.                         )
  220.                     )
  221.                 ,
  222.                 'text/html'
  223.             );
  224.         if (isset($data['attachments'])) {
  225.             foreach ($data['attachments'] as $atc)
  226.                 $emailmessage
  227.                     ->attach(new \Swift_Attachment(
  228.                         $atc['file'],
  229.                         (isset($atc['fileName']) ? $atc['fileName'] : 'document.pdf'),
  230.                         (isset($atc['fileType']) ? $atc['fileType'] : 'application/pdf')
  231.                     )
  232.                     );
  233.         }
  234.         /*
  235.          * If you also want to include a plaintext version of the message
  236.         ->addPart(
  237.             $this->renderView(
  238.                 'Emails/registration.txt.twig',
  239.                 array('name' => $name)
  240.             ),
  241.             'text/plain'
  242.         )
  243.         */
  244. //            ;
  245. //        $this->mailer->send($emailmessage);
  246.         $mailer->send($emailmessage);
  247. //1st get login ids basedon user id
  248.     }
  249.     public static function sendMyMailStatic($em,$data,$kernelRoot,$templater)
  250.     {
  251. //        $data=[];
  252.         $senderSettingHash '_DEFAULT_'//form parameters settings ultimate fallback
  253.         if (isset($data['senderHash']))
  254.             $senderSettingHash $data['senderHash'];
  255.         $fromAddress 'registration@entity.innobd.com';
  256.         $toAddress = isset($data['toAddress']) ? $data['toAddress'] : "";
  257.         $subject = isset($data['subject']) ? $data['subject'] : 'Registration to Entity test 2';
  258.         $toAddress explode(','$toAddress);
  259.         $newToAddress=[];
  260.         foreach ($toAddress as $t)
  261.         {
  262.             $newToAddress[]=filter_var($tFILTER_SANITIZE_EMAIL);
  263.         }
  264.         $toAddress=$newToAddress;
  265.         if (empty($toAddress)) {
  266.             return false;
  267.         }
  268.         $mailer = new \Swift_Mailer();
  269.         if ($senderSettingHash == '_DEFAULT_') {
  270.             $transport $mailer->getTransport();
  271.             $fromAddress $transport->getUserName();
  272.         } else {
  273.             //1st check if setting is defined in data or user wise
  274.             if ($senderSettingHash == '_CUSTOM_') {
  275.                 $transport =(new  \Swift_SmtpTransport($data['smtpServer'], $data['smtpPort']))
  276.                     ->setUsername($data['userName'])
  277.                     ->setPassword($data['password']);
  278.                 $fromAddress $data['fromAddress'];
  279.             } else {
  280.                 //1st check if the actual hash exists if not then check for general and if not then just use the default
  281.                 $senderSettingData $em->getRepository('ApplicationBundle\\Entity\\EmailSenderSettings')->findOneBy(
  282.                     array(
  283.                         'hash' => $senderSettingHash,
  284.                         'valid' => 1
  285.                     )
  286.                 );
  287.                 if ($senderSettingData) {
  288.                     $transport = (new  \Swift_SmtpTransport($senderSettingData->getSmtpServer(), $senderSettingData->getSmtpPort()))
  289.                         ->setUsername($senderSettingData->getUserName())
  290.                         ->setPassword($senderSettingData->getPassword());
  291.                     $fromAddress $senderSettingData->getEmail();
  292.                 } else {
  293.                     $senderSettingData $em->getRepository('ApplicationBundle\\Entity\\EmailSenderSettings')->findOneBy(
  294.                         array(
  295.                             'hash' => '_GENERAL_',
  296.                             'valid' => 1
  297.                         )
  298.                     );
  299.                     if ($senderSettingData) {
  300.                         $transport = (new  \Swift_SmtpTransport($senderSettingData->getSmtpServer(), $senderSettingData->getSmtpPort()))
  301.                             ->setUsername($senderSettingData->getUserName())
  302.                             ->setPassword($senderSettingData->getPassword());
  303.                         $fromAddress $senderSettingData->getEmail();
  304.                     } else {
  305.                         $transport =$mailer->getTransport();
  306.                         $fromAddress $transport->getUserName();
  307.                     }
  308.                 }
  309.             }
  310.         }
  311. //        $transport
  312. //            ->setUserName(...)
  313. //            ->setPassword(...)
  314. //            ->setHost(...)
  315. //            ->setPort(...)
  316. //            ->setEncryption(...);
  317.         $mailer = new \Swift_Mailer($transport);
  318. //        $mailer = $this->mailer;
  319. //        $mailer->setTransport($transport);
  320. //        \Swift_Image::fromPath('path/to/image.jpg')) $company_data->getImage()
  321.         $companyId 1;
  322.         if (isset($data['companyId'])) {
  323.             $companyId $data['companyId'];
  324.         }
  325.         $companyData = [];
  326.         $emailmessage = (new \Swift_Message($subject))
  327.             ->setFrom($fromAddress)
  328.             ->setTo($toAddress);
  329.         $companyImageCidSrc '';
  330.         if (isset($data['embedCompanyImage'])) {
  331.             $companyImagePath '';
  332.             if (isset($data['companyImagePath']))
  333.                 $companyImagePath $data['companyImagePath'];
  334.             else {
  335.                 $companyData Company::getCompanyData($em$companyId);
  336.                 $companyImagePath $companyData->getImage();
  337.             }
  338.             if ($companyImagePath != '') {
  339.                 $companyImageCidSrc $emailmessage->embed(\Swift_Image::fromPath($kernelRoot '/../web' $companyImagePath));
  340.                 if (isset($data['templateData']))
  341.                     $data['templateData']['companyImageCidSrc'] = $companyImageCidSrc;
  342.             }
  343.         }
  344.         if (isset($data['templateData']))
  345.             $data['templateData']['isEmailBody'] = 1;
  346.         $emailmessage
  347.             ->setBody(
  348.                 isset($data['emailBody']) ? $data['emailBody'] :
  349.                     $templater->render(
  350.                         isset($data['mailTemplate']) ? $data['mailTemplate'] : '@Application/email/general/test.html.twig',
  351.                         isset($data['templateData']) ? $data['templateData'] : array('name' => 'poka',
  352.                             'companyData' => $companyData,
  353.                             'userName' => "hello poka",
  354.                             'password' => "mia",
  355.                         )
  356.                     ),
  357.                 'text/html'
  358.             );
  359.         if (isset($data['attachments'])) {
  360.             foreach ($data['attachments'] as $atc)
  361.                 $emailmessage
  362.                     ->attach(new \Swift_Attachment(
  363.                             $atc['file'],
  364.                             (isset($atc['fileName']) ? $atc['fileName'] : 'document.pdf'),
  365.                             (isset($atc['fileType']) ? $atc['fileType'] : 'application/pdf')
  366.                         )
  367.                     );
  368.         }
  369.         /*
  370.          * If you also want to include a plaintext version of the message
  371.         ->addPart(
  372.             $this->renderView(
  373.                 'Emails/registration.txt.twig',
  374.                 array('name' => $name)
  375.             ),
  376.             'text/plain'
  377.         )
  378.         */
  379. //            ;
  380. //        $this->mailer->send($emailmessage);
  381.         $mailer->send($emailmessage);
  382. //1st get login ids basedon user id
  383.     }
  384. }