[795] | 1 | <?php |
---|
| 2 | require_once(PHPGW_SERVER_ROOT . SEP . 'phpgwapi' . SEP . 'inc' . SEP . 'class.phpmailer.inc.php'); |
---|
| 3 | |
---|
| 4 | /** |
---|
| 5 | * Permite ao desenvolvedor de processos Workflow enviar e-mails em tempo de execução. |
---|
| 6 | * @author Sidnei Augusto Drovetto Junior |
---|
| 7 | * @version 1.0 |
---|
| 8 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 9 | * @package Workflow |
---|
| 10 | * @subpackage local |
---|
| 11 | */ |
---|
| 12 | class wf_mail extends PHPMailer |
---|
| 13 | { |
---|
| 14 | /** |
---|
| 15 | * @var boolean $validConfig indica se as configurações de e-mail carregadas são válidas. |
---|
| 16 | * @access private |
---|
| 17 | */ |
---|
| 18 | var $validConfig; |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * Construtor do wf_mail. |
---|
| 22 | * @return object |
---|
| 23 | * @access public |
---|
| 24 | */ |
---|
| 25 | function wf_mail() |
---|
| 26 | { |
---|
| 27 | //parent::PHPMailer(); |
---|
| 28 | $this->_init(); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * Inicializa a classe wf_mail (configura o PHPMailer com as configurações do processo). |
---|
| 33 | * @return void |
---|
| 34 | * @access private |
---|
| 35 | */ |
---|
| 36 | private function _init() |
---|
| 37 | { |
---|
| 38 | /* carrega as configurações de processo e do perfil de e-mail */ |
---|
| 39 | $requiredConfiguration = array('mail_smtp_profile' => false); |
---|
| 40 | $configuration = $GLOBALS['workflow']['wf_runtime']->process->getConfigValues($requiredConfiguration); |
---|
[2591] | 41 | $bo_emailadmin = Factory::getInstance('bo'); |
---|
[795] | 42 | $profileData = $bo_emailadmin->getProfile($configuration['mail_smtp_profile']); |
---|
| 43 | |
---|
| 44 | if (!is_array($profileData)) |
---|
| 45 | { |
---|
| 46 | $this->validConfig = false; |
---|
| 47 | return false; |
---|
| 48 | } |
---|
| 49 | else |
---|
| 50 | $this->validConfig = true; |
---|
| 51 | |
---|
| 52 | /* configura os parâmetros para envio de e-mail */ |
---|
| 53 | $userLang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; |
---|
| 54 | /* FIXME: hardcoded 'br' because phpmailer don't use pt-br */ |
---|
| 55 | if ($userLang == 'pt-br') |
---|
| 56 | { |
---|
| 57 | $userLang = 'br'; |
---|
| 58 | } |
---|
| 59 | $langFile = PHPGW_SERVER_ROOT."/phpgwapi/setup/phpmailer.lang-$userLang.php"; |
---|
| 60 | if(file_exists($langFile)) |
---|
| 61 | $this->SetLanguage($userLang, PHPGW_SERVER_ROOT."/phpgwapi/setup/"); |
---|
| 62 | else |
---|
| 63 | $this->SetLanguage("en", PHPGW_SERVER_ROOT."/phpgwapi/setup/"); |
---|
| 64 | |
---|
| 65 | $this->PluginDir = PHPGW_SERVER_ROOT."/phpgwapi/inc/"; |
---|
| 66 | $this->IsSMTP(); |
---|
| 67 | $this->Host = $profileData['smtpServer']; |
---|
| 68 | $this->Port = $profileData['smtpPort']; |
---|
| 69 | if ($profile['smtpAuth']) |
---|
| 70 | { |
---|
| 71 | $this->SMTPAuth = true; |
---|
| 72 | $this->Username = $GLOBALS['phpgw_info']['user']['userid']; |
---|
| 73 | $this->Password = $GLOBALS['phpgw_info']['user']['passwd']; |
---|
| 74 | } |
---|
| 75 | $this->Encoding = '8bit'; |
---|
| 76 | $this->AddCustomHeader("X-Mailer: Egroupware Workflow"); |
---|
| 77 | $this->WordWrap = 76; |
---|
| 78 | $this->IsHTML(true); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * Limpa os erros encontrados até o momento. |
---|
| 83 | * @return void |
---|
| 84 | * @access private |
---|
| 85 | */ |
---|
| 86 | private function clearErrors() |
---|
| 87 | { |
---|
| 88 | $this->error_count = 0;; |
---|
| 89 | $this->ErrorInfo = null; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Envia um e-mail de acordo com as propriedades da classe. |
---|
| 94 | * @return bool TRUE em caso de sucesso e FALSE caso contrário. |
---|
| 95 | * @access public |
---|
| 96 | */ |
---|
| 97 | function Send() |
---|
| 98 | { |
---|
| 99 | /* limpa possíveis erros (para que outras chamadas ao método não influenciem a chamada atual) */ |
---|
| 100 | $this->clearErrors(); |
---|
| 101 | |
---|
| 102 | /* checa se as configurações são válidas */ |
---|
| 103 | if (!$this->validConfig) |
---|
| 104 | return false; |
---|
| 105 | |
---|
| 106 | /* envia o e-mail */ |
---|
| 107 | return parent::Send(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * Envia um e-mail de acordo com os parâmetros passados. |
---|
| 112 | * @return bool TRUE em caso de sucesso e FALSE caso contrário. |
---|
| 113 | * @param string $from O e-mail de origem (remetente). |
---|
| 114 | * @param mixed $to Uma string contendo o e-mail do destino (destinatário) ou uma array contendo uma lista de destinatários. |
---|
| 115 | * @param string $subject O assunto do e-mail. |
---|
| 116 | * @param string $body O corpo da mensagem. |
---|
| 117 | * @access public |
---|
| 118 | */ |
---|
| 119 | function quickSend($from, $to, $subject, $body) |
---|
| 120 | { |
---|
| 121 | /* limpa possíveis erros (para que outras chamadas ao método não influenciem a chamada atual) */ |
---|
| 122 | $this->clearErrors(); |
---|
| 123 | |
---|
| 124 | /* checa se as configurações são válidas */ |
---|
| 125 | if (!$this->validConfig) |
---|
| 126 | return false; |
---|
| 127 | |
---|
| 128 | /* preenche as informações para envio */ |
---|
| 129 | $this->FromName = $GLOBALS['workflow']['wf_runtime']->process->getName(); |
---|
| 130 | $this->From = $from; |
---|
| 131 | $this->AddReplyTo($from); |
---|
| 132 | $this->Subject = $subject; |
---|
| 133 | $this->Body = str_replace("\n",'<br />',html_entity_decode($body)); |
---|
| 134 | // se for necessária compatibilidade com clientes de email antigos (e.g. mutt) descomente a linha abaixo |
---|
| 135 | //$this->AltBody = $body; |
---|
| 136 | $this->ClearAllRecipients(); |
---|
| 137 | if (!is_array($to)) |
---|
| 138 | $to = array($to); |
---|
| 139 | foreach ($to as $recipient) |
---|
| 140 | $this->AddAddress($recipient); |
---|
| 141 | |
---|
| 142 | /* envia o e-mail */ |
---|
| 143 | return parent::Send(); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * Verifica se o recipiente (endereço de e-mail) está ok. |
---|
| 148 | * @param string $recipient O endereço de e-mail do recipiente. |
---|
| 149 | * @return mixed true se o recipiente estiver ok, caso o recipiente não esteja ok. Será retornado null se houver algum problema ao se iniciar uma transação com o servidor SMTP. |
---|
| 150 | * @access public |
---|
| 151 | */ |
---|
| 152 | public function checkRecipient($recipient) |
---|
| 153 | { |
---|
| 154 | require_once $this->PluginDir . 'class.smtp.php'; |
---|
| 155 | |
---|
| 156 | /* tenta se conectar com o servidor SMTP */ |
---|
| 157 | if(!$this->SmtpConnect()) |
---|
| 158 | return false; |
---|
| 159 | |
---|
| 160 | /* estabelece uma transação */ |
---|
| 161 | if (!$this->smtp->Mail('')) |
---|
| 162 | return null; |
---|
| 163 | |
---|
| 164 | /* verifica se o e-mail é válido */ |
---|
| 165 | $output = $this->smtp->Recipient($recipient); |
---|
| 166 | |
---|
| 167 | /* finaliza a transação */ |
---|
| 168 | $this->smtp->Reset(); |
---|
| 169 | |
---|
| 170 | /* retorna a saída */ |
---|
| 171 | return $output; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | ?> |
---|