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); |
---|
41 | $bo_emailadmin = Factory::getInstance('bo'); |
---|
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, $fromName = '') |
---|
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 | if (empty($fromName)) |
---|
129 | $fromName = $GLOBALS['workflow']['wf_runtime']->process->getName(); |
---|
130 | |
---|
131 | /* preenche as informações para envio */ |
---|
132 | $this->FromName = $fromName; |
---|
133 | $this->From = $from; |
---|
134 | $this->AddReplyTo($from); |
---|
135 | $this->Subject = $subject; |
---|
136 | $this->Body = str_replace("\n",'<br />',html_entity_decode($body)); |
---|
137 | // se for necessária compatibilidade com clientes de email antigos (e.g. mutt) descomente a linha abaixo |
---|
138 | //$this->AltBody = $body; |
---|
139 | $this->ClearAllRecipients(); |
---|
140 | if (!is_array($to)) |
---|
141 | $to = array($to); |
---|
142 | foreach ($to as $recipient) |
---|
143 | $this->AddAddress($recipient); |
---|
144 | |
---|
145 | /* envia o e-mail */ |
---|
146 | return parent::Send(); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Verifica se o recipiente (endereço de e-mail) está ok. |
---|
151 | * @param string $recipient O endereço de e-mail do recipiente. |
---|
152 | * @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. |
---|
153 | * @access public |
---|
154 | */ |
---|
155 | public function checkRecipient($recipient) |
---|
156 | { |
---|
157 | require_once $this->PluginDir . 'class.smtp.php'; |
---|
158 | |
---|
159 | /* tenta se conectar com o servidor SMTP */ |
---|
160 | if(!$this->SmtpConnect()) |
---|
161 | return false; |
---|
162 | |
---|
163 | /* estabelece uma transação */ |
---|
164 | if (!$this->smtp->Mail('')) |
---|
165 | return null; |
---|
166 | |
---|
167 | /* verifica se o e-mail é válido */ |
---|
168 | $output = $this->smtp->Recipient($recipient); |
---|
169 | |
---|
170 | /* finaliza a transação */ |
---|
171 | $this->smtp->Reset(); |
---|
172 | |
---|
173 | /* retorna a saída */ |
---|
174 | return $output; |
---|
175 | } |
---|
176 | } |
---|
177 | ?> |
---|