Files
2021-09-17 12:41:45 +02:00

223 lines
7.1 KiB
PHP

<?php
class Zend_Controller_Action_Helper_Email extends Zend_Controller_Action_Helper_Abstract
{
protected $_configs;
protected $_ipManager;
public function init()
{
$this->_configs = new App_Service_ConfigManager();
$this->_ipManager = new App_Service_IpManager();
}
/**
* Invia un email all'utente con i parametri scelti
*
* @param String $message | $subject | $email
* @param boolean $text | $footer
* @return void
*/
public function direct($message, $subject, $email, $text = false, $footer = false)
{
$mail = new App_Model_Mail('UTF-8');
if ($footer) {
$message .= $this->addFooter();
}
$mail->setFrom($this->_configs->get('email_mittente'), $this->_configs->get('email_mittente_nome'))->setSubject($subject)->addTo($email, 'Fatturab2b.net');
if ($text == false) {
$mail->setBodyHtml($message);
} else {
$mail->setBodyText($message);
}
$mail->send();
}
/**
* Invia un email all'utente in base a un template memorizzato nel DB.
* @param $email
* @param $template
* @param array $params
* @param null $filename
* @throws Zend_Mail_Exception
*/
public function sendEmailByTemplate($email, $template, $params = array(), $nomeMittente = null, $filename = null,$fileNameInterno = null)
{
$mail = new App_Model_Mail('UTF-8');
if(!$template){
throw new Exception("Email template non trovato impossibile inviare l'email");
}
$params = $this->preparaDatiPerSostituzione($params);
if (count($params) > 0) {
foreach ($params as $key => $val) {
$template['Oggetto'] = str_replace($key, $val, $template['Oggetto']);
$template['Testo'] = str_replace($key, $val, $template['Testo']);
}
}
if ($template['Footer'])
$template['Testo'] .= $this->addFooter();
if ($filename != null) {
if (file_exists($filename))
$mail->createAttachment(file_get_contents($filename), 'application/pdf', Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, $filename);
}
$nomeMittente = ($nomeMittente) ? $nomeMittente : $this->_configs->get('email_mittente_nome');
$mail->setFrom($this->_configs->get('email_mittente'), $nomeMittente)->setSubject($template['Oggetto']);
if ($template['Tipologia'] == 'HTML') {
$mail->setBodyHtml($template['Testo']);
} else if ($template['Tipologia'] == 'TEXT') {
$mail->setBodyText($template['Testo']);
}
//Nel caso in cui sono in test le email vengono inviate solo a sviluppoweb
if($this->_ipManager->controllaAbilitazioneTestSdk()){
$email=$this->_configs->get('test_email');
if($template['Resend'])
$template['Resend'] = 'TEST';
}
//Inserisco il o i destinatari/o
if(is_string($email)){
$mail->addTo($email, $email);
}else if(is_array($email)){
foreach($email as $val){
$mail->addTo($val);
}
}
$mail->send();
//INVIO COAPIA AL NOSTRO STISTEMA
if($template['Resend']){
$mail->clearRecipients();
//Controllo se devo reinviare la mail anche al nostro server
if ($template['Resend'] == 'INFO')
$mail->addTo($this->_configs->get('email_mittente'), $this->_configs->get('email_mittente_nome'));
if ($template['Resend'] == 'ACQUISTI')
$mail->addTo($this->_configs->get('email_ordine'), $this->_configs->get('email_mittente_nome'));
if ($template['Resend'] == 'TEST')
$mail->addTo($this->_configs->get('test_email'), $this->_configs->get('email_mittente_nome'));
if($fileNameInterno){
$mail->setParts(array());
if (file_exists($fileNameInterno))
$mail->createAttachment(file_get_contents($fileNameInterno), 'application/pdf', Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, $fileNameInterno);
}
$mail->send();
//CANCELLO IL FILE DEL PDF
if ($fileNameInterno != null) {
if (file_exists($fileNameInterno))
@unlink($fileNameInterno);
}
}
//Invio l'email per la stampante con il testo vuoto
if ($template['Resend'] == 'ACQUISTI' && !$this->_ipManager->controllaAbilitazioneTestSdk()){
$mail->clearRecipients();
$mail->setBodyHtml("");
$mail->setBodyText("");
$mail->addTo("", $this->_configs->get('email_mittente_nome'));
$mail->send();
}
}
public function reportErrorToEmail($error, $shortdesc, Exception $e = null,$email = null)
{
$mail = new Zend_Mail('UTF-8');
if(!$email) $email = $this->_configs->get('test_email');
$subject = '--- SERVER Fatturab2b.net [EMAIL] --- ERRORE ---';
$message = '<br>ERROR-MESSAGE: ' . $shortdesc;
if ($e != null) {
$message .= '<br><br>STACK TRACE ERRORE:<br>';
foreach ($e->getTrace() as $key => $value) {
$message .= "<br>[ " . $key . "] = " . $value;
if (is_array($value)) {
$message .= '<br>[';
foreach ($value as $key2 => $value2) {
$message .= "[ " . $key2 . "] = " . $value2 . "<br>";
}
$message .= ']<br>';
}
}
$message .= '<br>EXCEPTION STAMP:<br>';
$message .= "<pre>";
$message .= print_r($e,true);
$message .= "</pre>";
}
$message .= '<br><br>PARAMETRI RICEVUTI:<br>';
$message .= "<pre>";
$message .= print_r($error,true);
$message .= "</pre>";
$message .= '<br>';
$message .= '<br><br> Errore inviato dal server il ' . date('d/m/Y H:i:s');
$mail->setFrom($this->_configs->get('email_mittente'), $this->_configs->get('email_mittente_nome'))
->setSubject($subject)
->addTo($email, '')
->setBodyHtml($message)
->send();
}
/**
* Funzione che restituisce il footer per ogni email
*
* @param String $ragsoc | $email
* @return void
*/
public function addFooter()
{
$footer = '<br><br>';
$footer .= '';
return $footer;
}
/****************************
*** FUNZIONI PRIVATE ******
***************************/
/**
* Questa funzione presi i parametri passati nell array ne aggiunge i simboli
* percentuale per farli riconoscere popi nel testo e sostituirli
* @param $dati
* @return array
*/
private function preparaDatiPerSostituzione($dati){
$newParams = array();
foreach($dati as $key => $val){
$newParams['%'.$key.'%'] = $val;
}
return $newParams;
}
}