<?php
/**
* Created by PhpStorm.
* User: ehsan_pc
* Date: 3/14/2016
* Time: 4:22 PM
*/
namespace ApplicationBundle\Modules\System;
use ApplicationBundle\Constants\AccountsConstant;
use ApplicationBundle\Constants\GeneralConstant;
use ApplicationBundle\Entity\AccAccountsHead;
use ApplicationBundle\Entity\AccPrefix;
use ApplicationBundle\Entity\AccSuppliers;
use ApplicationBundle\Entity\AccClosingBalance;
use ApplicationBundle\Entity\AccTransactionDetails;
use ApplicationBundle\Entity\AccTransactions;
use ApplicationBundle\Entity\Approval;
use ApplicationBundle\Entity\ApprovalSettings;
use ApplicationBundle\Entity\PaymentLog;
use ApplicationBundle\Entity\PurchaseInvoice;
use ApplicationBundle\Entity\PurchaseInvoiceItem;
use ApplicationBundle\Helper\Generic;
use ApplicationBundle\Modules\Accounts\Accounts;
use ApplicationBundle\Modules\Inventory\Inventory;
use ApplicationBundle\Modules\Sales\Package;
use ApplicationBundle\Modules\Sales\SalesOrderM;
use ApplicationBundle\Modules\User\Company;
use ApplicationBundle\Modules\User\Users;
use ApplicationBundle\Modules\System\System;
use Symfony\Component\HttpFoundation\Session\Session;
use Twig\Environment;
class Email
{
public $secretKey = "";
private $em;
private $kernelRoot;
// private $dm;
private $router;
private $session;
// private $sha256salted_encoder;
private $email_enabled;
private $mailer;
private $twig;
public function __construct($router, $session, $db_credentials, $kernel_Root, $email_enabled, $mailer, Environment $twig)
{
// $this->secretKey=$secret;
$this->em = $db_credentials;
// $this->dm=$odm_credentials;
$this->router = $router;
$this->session = $session;
// $this->sha256salted_encoder = $sha256salted_encoder;
$this->email_enabled = $email_enabled;
$this->mailer = $mailer;
$this->twig = $twig;
$this->kernelRoot = $kernel_Root;
}
public function isEmailEnabled($em, $user_id = 0)
{
$data = [];
return $this->email_enabled;
//1st get login ids basedon user id
}
public function sendMyMail($data)
{
// $data=[];
if ($this->email_enabled == 0)
return false;
$senderSettingHash = '_DEFAULT_'; //form parameters settings ultimate fallback
if (isset($data['senderHash']))
$senderSettingHash = $data['senderHash'];
$em = $this->em;
$fromAddress = 'accounts@ourhoneybee.eu';
$toAddress = isset($data['toAddress']) ? $data['toAddress'] : (isset($data['forwardToMailAddress'])?$data['forwardToMailAddress']:"");
$subject = isset($data['subject']) ? $data['subject'] : 'Registration to Entity test 2';
$toAddress = explode(',', $toAddress);
$newToAddress=[];
foreach ($toAddress as $t)
{
if($t != null && $t != '')
$newToAddress[]=filter_var($t, FILTER_SANITIZE_EMAIL);
}
$toAddress=$newToAddress;
// $toAddress = filter_var($toAddress, FILTER_SANITIZE_EMAIL);
// $toAddress = filter_var($toAddress, FILTER_VALIDATE_EMAIL);
$encryptionMethod=isset($data['encryptionMethod'])?$data['encryptionMethod']:'ssl';
////**** Encryption method must be added on the table and then used
if (empty($toAddress)) {
return false;
}
if ($senderSettingHash == '_DEFAULT_') {
$transport = $this->mailer->getTransport();
$fromAddress = $transport->getUserName();
}
else {
//1st check if setting is defined in data or user wise
if ($senderSettingHash == '_CUSTOM_') {
$transport =(new \Swift_SmtpTransport($data['smtpServer'], $data['smtpPort'],$encryptionMethod))
->setUsername($data['userName'])
->setPassword($data['password']);
$fromAddress = $data['fromAddress'];
} else {
//1st check if the actual hash exists if not then check for general and if not then just use the default
$senderSettingData = $em->getRepository('ApplicationBundle\\Entity\\EmailSenderSettings')->findOneBy(
array(
'hash' => $senderSettingHash,
'valid' => 1
)
);
if ($senderSettingData) {
if($senderSettingData->getEncryptionMethod()!=null && $senderSettingData->getEncryptionMethod()!='')
$encryptionMethod=$senderSettingData->getEncryptionMethod();
$transport = (new \Swift_SmtpTransport($senderSettingData->getSmtpServer(), $senderSettingData->getSmtpPort(),$encryptionMethod))
->setUsername($senderSettingData->getUserName())
->setPassword($senderSettingData->getPassword());
$fromAddress = $senderSettingData->getEmail();
} else {
$senderSettingData = $em->getRepository('ApplicationBundle\\Entity\\EmailSenderSettings')->findOneBy(
array(
'hash' => '_GENERAL_',
'valid' => 1
)
);
if ($senderSettingData) {
if($senderSettingData->getEncryptionMethod()!=null && $senderSettingData->getEncryptionMethod()!='')
$encryptionMethod=$senderSettingData->getEncryptionMethod();
$transport = (new \Swift_SmtpTransport($senderSettingData->getSmtpServer(), $senderSettingData->getSmtpPort(),$encryptionMethod))
->setUsername($senderSettingData->getUserName())
->setPassword($senderSettingData->getPassword());
$fromAddress = $senderSettingData->getEmail();
} else {
$transport = $this->mailer->getTransport();
$fromAddress = $transport->getUserName();
}
}
}
}
// $transport
// ->setUserName(...)
// ->setPassword(...)
// ->setHost(...)
// ->setPort(...)
// ->setEncryption(...);
$mailer = new \Swift_Mailer($transport);
// $mailer = $this->mailer;
// $mailer->setTransport($transport);
// \Swift_Image::fromPath('path/to/image.jpg')) $company_data->getImage()
$companyId = 1;
if (isset($data['companyId'])) {
$companyId = $data['companyId'];
}
$companyData = [];
$emailmessage = (new \Swift_Message($subject))
->setFrom($fromAddress)
->setTo($toAddress);
$ccAddress = isset($data['ccAddress']) ? $data['ccAddress'] : (isset($data['cc']) ? $data['cc'] : '');
$bccAddress = isset($data['bccAddress']) ? $data['bccAddress'] : (isset($data['bcc']) ? $data['bcc'] : '');
$normalizeRecipients = function ($addresses) {
if (is_array($addresses)) {
$addresses = implode(',', $addresses);
}
$addresses = explode(',', (string) $addresses);
$normalized = [];
foreach ($addresses as $address) {
$address = trim((string) $address);
if ($address === '') {
continue;
}
$sanitized = filter_var($address, FILTER_SANITIZE_EMAIL);
if ($sanitized !== '') {
$normalized[] = $sanitized;
}
}
return $normalized;
};
$ccRecipients = $normalizeRecipients($ccAddress);
if (!empty($ccRecipients)) {
$emailmessage->setCc($ccRecipients);
}
$bccRecipients = $normalizeRecipients($bccAddress);
if (!empty($bccRecipients)) {
$emailmessage->setBcc($bccRecipients);
}
$companyImageCidSrc = '';
if (isset($data['embedCompanyImage'])) {
if ($data['embedCompanyImage']==1) {
$companyImagePath = '';
if (isset($data['companyImagePath']))
$companyImagePath = $data['companyImagePath'];
else {
$companyData = Company::getCompanyData($em, $companyId);
$companyImagePath = $companyData->getImage();
}
if ($companyImagePath != '') {
$companyImageCidSrc = $emailmessage->embed(\Swift_Image::fromPath($this->kernelRoot . '/../web' . $companyImagePath));
if (isset($data['templateData']))
$data['templateData']['companyImageCidSrc'] = $companyImageCidSrc;
}
}
}
if (isset($data['templateData']))
$data['templateData']['isEmailBody'] = 1;
$body_is_empty=1;
$email_body='';
if(isset($data['emailBody']))
$email_body=$data['emailBody'];
if($email_body=='')
$body_is_empty=1;
else
$body_is_empty=0;
$emailmessage
->setBody(
$body_is_empty==0? $data['emailBody'] :
$this->twig->render(
isset($data['mailTemplate']) ? $data['mailTemplate'] : '@Application/email/general/test.html.twig',
isset($data['templateData']) ? $data['templateData'] : array('name' => 'poka',
'companyData' => $companyData,
'userName' => "hello poka",
'password' => "mia",
)
)
,
'text/html'
);
if (isset($data['attachments'])) {
foreach ($data['attachments'] as $atc)
$emailmessage
->attach(new \Swift_Attachment(
$atc['file'],
(isset($atc['fileName']) ? $atc['fileName'] : 'document.pdf'),
(isset($atc['fileType']) ? $atc['fileType'] : 'application/pdf')
)
);
}
/*
* If you also want to include a plaintext version of the message
->addPart(
$this->renderView(
'Emails/registration.txt.twig',
array('name' => $name)
),
'text/plain'
)
*/
// ;
// $this->mailer->send($emailmessage);
$mailer->send($emailmessage);
//1st get login ids basedon user id
}
public static function sendMyMailStatic($em,$data,$kernelRoot,$templater)
{
// $data=[];
$senderSettingHash = '_DEFAULT_'; //form parameters settings ultimate fallback
if (isset($data['senderHash']))
$senderSettingHash = $data['senderHash'];
$fromAddress = 'registration@entity.innobd.com';
$toAddress = isset($data['toAddress']) ? $data['toAddress'] : "";
$subject = isset($data['subject']) ? $data['subject'] : 'Registration to Entity test 2';
$toAddress = explode(',', $toAddress);
$newToAddress=[];
foreach ($toAddress as $t)
{
$newToAddress[]=filter_var($t, FILTER_SANITIZE_EMAIL);
}
$toAddress=$newToAddress;
if (empty($toAddress)) {
return false;
}
$mailer = new \Swift_Mailer();
if ($senderSettingHash == '_DEFAULT_') {
$transport = $mailer->getTransport();
$fromAddress = $transport->getUserName();
} else {
//1st check if setting is defined in data or user wise
if ($senderSettingHash == '_CUSTOM_') {
$transport =(new \Swift_SmtpTransport($data['smtpServer'], $data['smtpPort']))
->setUsername($data['userName'])
->setPassword($data['password']);
$fromAddress = $data['fromAddress'];
} else {
//1st check if the actual hash exists if not then check for general and if not then just use the default
$senderSettingData = $em->getRepository('ApplicationBundle\\Entity\\EmailSenderSettings')->findOneBy(
array(
'hash' => $senderSettingHash,
'valid' => 1
)
);
if ($senderSettingData) {
$transport = (new \Swift_SmtpTransport($senderSettingData->getSmtpServer(), $senderSettingData->getSmtpPort()))
->setUsername($senderSettingData->getUserName())
->setPassword($senderSettingData->getPassword());
$fromAddress = $senderSettingData->getEmail();
} else {
$senderSettingData = $em->getRepository('ApplicationBundle\\Entity\\EmailSenderSettings')->findOneBy(
array(
'hash' => '_GENERAL_',
'valid' => 1
)
);
if ($senderSettingData) {
$transport = (new \Swift_SmtpTransport($senderSettingData->getSmtpServer(), $senderSettingData->getSmtpPort()))
->setUsername($senderSettingData->getUserName())
->setPassword($senderSettingData->getPassword());
$fromAddress = $senderSettingData->getEmail();
} else {
$transport =$mailer->getTransport();
$fromAddress = $transport->getUserName();
}
}
}
}
// $transport
// ->setUserName(...)
// ->setPassword(...)
// ->setHost(...)
// ->setPort(...)
// ->setEncryption(...);
$mailer = new \Swift_Mailer($transport);
// $mailer = $this->mailer;
// $mailer->setTransport($transport);
// \Swift_Image::fromPath('path/to/image.jpg')) $company_data->getImage()
$companyId = 1;
if (isset($data['companyId'])) {
$companyId = $data['companyId'];
}
$companyData = [];
$emailmessage = (new \Swift_Message($subject))
->setFrom($fromAddress)
->setTo($toAddress);
$companyImageCidSrc = '';
if (isset($data['embedCompanyImage'])) {
$companyImagePath = '';
if (isset($data['companyImagePath']))
$companyImagePath = $data['companyImagePath'];
else {
$companyData = Company::getCompanyData($em, $companyId);
$companyImagePath = $companyData->getImage();
}
if ($companyImagePath != '') {
$companyImageCidSrc = $emailmessage->embed(\Swift_Image::fromPath($kernelRoot . '/../web' . $companyImagePath));
if (isset($data['templateData']))
$data['templateData']['companyImageCidSrc'] = $companyImageCidSrc;
}
}
if (isset($data['templateData']))
$data['templateData']['isEmailBody'] = 1;
$emailmessage
->setBody(
isset($data['emailBody']) ? $data['emailBody'] :
$templater->render(
isset($data['mailTemplate']) ? $data['mailTemplate'] : '@Application/email/general/test.html.twig',
isset($data['templateData']) ? $data['templateData'] : array('name' => 'poka',
'companyData' => $companyData,
'userName' => "hello poka",
'password' => "mia",
)
),
'text/html'
);
if (isset($data['attachments'])) {
foreach ($data['attachments'] as $atc)
$emailmessage
->attach(new \Swift_Attachment(
$atc['file'],
(isset($atc['fileName']) ? $atc['fileName'] : 'document.pdf'),
(isset($atc['fileType']) ? $atc['fileType'] : 'application/pdf')
)
);
}
/*
* If you also want to include a plaintext version of the message
->addPart(
$this->renderView(
'Emails/registration.txt.twig',
array('name' => $name)
),
'text/plain'
)
*/
// ;
// $this->mailer->send($emailmessage);
$mailer->send($emailmessage);
//1st get login ids basedon user id
}
}