✏️ 正在编辑: Mail.php
路径:
/srv/systems_dir/yuppiecred-bkp/library/Core/Mail.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php use Doctrine\Common\Collections\ArrayCollection; class Core_Mail extends Zend_Mail { /** @var string */ private const TRANSPORT_HOST = 'email-smtp.us-east-1.amazonaws.com', FROM = 'no-reply@yuppietech.com.br', ENVIADO = 'Mensagem enviada com sucesso.' ; /** @var string[] */ private const ASSUNTOS = [ 'informativo' => 'Informativo da - %s' ]; /** @var string */ private const FILE_NOT_FOUND = 'O arquivo não foi encontrado'; /** @var ?Zend_Mail_Transport_Smtp */ protected $_transport; /** @var ?Zend_Mail */ protected $_mail; /** @var Service_Config */ private $config; /** @var ArrayCollection */ private $files; /** @var ArrayCollection */ private $destinatarios; /** * @param string|null $charset */ public function __construct(?string $charset = 'UTL-8') { $this->config = service('config')->get(); $this->validConfig() ->initTransport() ->initMail() ; $this->files = new ArrayCollection(); $this->destinatarios = new ArrayCollection(); if (!empty($this->config->amazon->smtp->transport_host)) { define("TRANSPORT_HOST", $this->config->amazon->smtp->transport_host); } parent::__construct($charset); } public function newMessage(){ $this->_mail->clearRecipients(); $this->_mail->clearMessageId(); $this->_mail->clearSubject(); $email = $this->_from; if( !empty(Yuppie_Auth::getIdentity()->email) ){ $email = Yuppie_Auth::getIdentity()->email; } if($this->_mail->getReturnPath() === null){ $this->_mail->setReturnPath($email); } $this->_mail->addHeader('Errors-To', $email, true); //seta o header com o e-mail para retorno dos erros - recebe os bounces if($this->_mail->getFrom() === null){ $this->_mail->setFrom( self::verificarEmail($email) ); } if (!(new Zend_Validate_EmailAddress())->isValid(self::verificarEmail($email))) { throw new Core_Exception("O e-mail " . $email . " do remetente é inválido." ); } $this->_mail->setMessageId(); //seta o id da mensagem } public function enviarMensagemHtml($nomePara, $emailPara, $assunto, $mensagem, $emailsAdicionais = array(), $receberEmailsComCopia = array()) { if ($this->_transport instanceof Zend_Mail_Transport_Smtp && $this->_mail instanceof Zend_Mail) { if( !$this->emailIsValid($emailPara) ){ return array( 'status' => 0, 'msg' => "O e-mail $emailPara do destinatário é inválido." ); } $this->newMessage(); //envia uma cópia oculta para o email cadastrado no usuário if (!empty(Yuppie_Auth::getIdentity()->email_copia) && $this->emailIsValid(Yuppie_Auth::getIdentity()->email_copia)) { $this->_mail->addBcc(Yuppie_Auth::getIdentity()->email_copia); } $this->_mail->addTo($emailPara, $nomePara); $this->_mail->setSubject($assunto); $this->_mail->setBodyHtml($mensagem, "UTF-8"); if ($emailsAdicionais) { foreach ($emailsAdicionais as $adicional) { $adicional['email'] = strtolower($adicional['email']); if (isset($adicional['receber_email']) && $adicional['receber_email'] == 1) { if($this->emailIsValid($adicional['email'])){ $this->_mail->addCc($adicional['email'], $adicional['nome']); } } } } if (!empty(Yuppie_Auth::getIdentity()->receber_email_copia)) { $this->_mail->addCc(Yuppie_Auth::getIdentity()->email); } if ($receberEmailsComCopia) { foreach ($receberEmailsComCopia as $receberEmail) { if ($this->emailIsValid($receberEmail['email'])) { $this->_mail->addCc($receberEmail['email'], $receberEmail['nome']); } } } $this->_mail->_parts = $this->_parts; $this->_mail->send($this->_transport); return array('status' => 1, 'msg' => 'Mensagem enviada com sucesso.'); } else { throw new Exception('Configuração de SMTP inválida.'); } } public function enviarEmailCorretor($assunto = null, $mensagem = null, $corretorId = null, $fileName = null) { $corretor = new Model_Corretor(); $dadosCorretor = $corretor->exibir($corretorId); $modelEmpresa = new Model_Empresa(); $dadosEmpresa = $modelEmpresa->exibir(Yuppie_Auth::getIdentity()->empresa_id); if( !$dadosCorretor ){ return array( 'status' => 0, 'msg' => "Corretor não identificado!." ); } if( !$this->emailIsValid($dadosCorretor->email) ) { return array( 'status' => 0, 'msg' => "O e-mail $dadosCorretor->email do destinatário é inválido." ); } if ($fileName) { $this->addFile(PUBLIC_PATH . '/../public/uploads/' . $fileName, $fileName); } try { if ($this->_transport instanceof Zend_Mail_Transport_Smtp) { $this->addDestinaraio($dadosCorretor->email); $assunto = $this->assunto($assunto, 'informativo', $dadosEmpresa->nome); $this->newSend($assunto, $mensagem, self::FROM); return ['status' => 1, 'msg' => self::ENVIADO]; } } catch ( Zend_Mail_Exception $e ) { return ['status' => 0, 'msg' => "Uma falha ocorreu ao enviar o E-mail"]; } } public function emailIsValid($email){ $validateEmail = new Zend_Validate_EmailAddress(); $zendValidate = $validateEmail->isValid($email); if(!$zendValidate) return false; $notifications = new Yuppie_Mail_Notifications(); if($notifications->isBounced($this->_mail->getFrom(), $email)) return false; if($notifications->isComplained($this->_mail->getFrom(), $email)) return false; return true; } public function verificarEmail( $email ){ if ( empty($email) ){ $email = !empty(Yuppie_Auth::getIdentity()->email_grupo) ? Yuppie_Auth::getIdentity()->email_grupo : null; } return $email; } /** * @return $this */ private function validConfig(): self { if (empty($this->config) || !$this->config->amazon->smtp) { throw new Service_Exception_ConfigException(); } return $this; } /** * @param bool $fileContent * @return $this */ private function validAddFile(bool $fileContent): self { if (!$fileContent) { throw new DomainException(self::FILE_NOT_FOUND); } return $this; } /** * @return $this */ private function initTransport(): self { $this->_transport = new Zend_Mail_Transport_Smtp( self::TRANSPORT_HOST, $this->config->amazon->smtp->toArray() ); return $this; } /** * @param string|null $charset * @return $this */ private function initMail(?string $charset = 'UTF-8'): self { $this->_mail = new Zend_Mail($charset); $this->_mail->setDefaultTransport($this->_transport); return $this; } /** * @param string $file * @param string $fileName * @return $this */ public function addFile(string $file, string $fileName): self { $fileContent = file_get_contents($file, true); $this->validAddFile($fileContent); $this->files->add( $this->getAttachment($fileContent, $fileName) ); return $this; } /** * @param string $fileContent * @param string $filename * @return Zend_Mime_Part */ public function getAttachment(string $fileContent, string $filename): Zend_Mime_Part { $attachment = new Zend_Mime_Part($fileContent); $attachment->type = Zend_Mime::TYPE_OCTETSTREAM; $attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT; $attachment->encoding = Zend_Mime::ENCODING_BASE64; $attachment->filename = $filename; return $attachment; } /** * @param ArrayCollection $emails * @return $this */ public function setDestinatarios(ArrayCollection $emails): self { $emails->map(function ($index) { $this->destinatarios->add($index['email']); }); return $this; } /** * @param string $email * @return $this */ public function addDestinaraio(string $email): self { $this->destinatarios->add($email); return $this; } /** * @param string $assunto * @param string|null $body * @param string $from */ public function newSend(string $assunto, ?string $body, string $from): void { $this->destinatarios->map(function ($to) { $this->_mail->addTo($to); }); $this->files->map(function ($file) { $this->_mail->addAttachment($file); }); $this->_mail->setFrom($from); $this->_mail->setSubject(utf8_decode($assunto)); if ($body) { $this->_mail->setBodyHtml($body); } $this->_mail->send($this->_transport); } public function assunto(?string $assunto, string $assuntoDefault, string $value = null) { return $assunto ? $assunto : sprintf(self::ASSUNTOS[$assuntoDefault], $value); } }
💾 保存文件
← 返回文件管理器