PHPMailer::preSend PHP Method

preSend() public method

Prepare a message for sending.
public preSend ( ) : boolean
return boolean
    public function preSend()
    {
        try {
            $this->error_count = 0;
            // Reset errors
            $this->mailHeader = '';
            // Dequeue recipient and Reply-To addresses with IDN
            foreach (array_merge($this->RecipientsQueue, $this->ReplyToQueue) as $params) {
                $params[1] = $this->punyencodeAddress($params[1]);
                call_user_func_array(array($this, 'addAnAddress'), $params);
            }
            if (count($this->to) + count($this->cc) + count($this->bcc) < 1) {
                throw new phpmailerException($this->lang('provide_address'), self::STOP_CRITICAL);
            }
            // Validate From, Sender, and ConfirmReadingTo addresses
            foreach (array('From', 'Sender', 'ConfirmReadingTo') as $address_kind) {
                $this->{$address_kind} = trim($this->{$address_kind});
                if (empty($this->{$address_kind})) {
                    continue;
                }
                $this->{$address_kind} = $this->punyencodeAddress($this->{$address_kind});
                if (!$this->validateAddress($this->{$address_kind})) {
                    $error_message = $this->lang('invalid_address') . ' (punyEncode) ' . $this->{$address_kind};
                    $this->setError($error_message);
                    $this->edebug($error_message);
                    if ($this->exceptions) {
                        throw new phpmailerException($error_message);
                    }
                    return false;
                }
            }
            // Set whether the message is multipart/alternative
            if ($this->alternativeExists()) {
                $this->ContentType = 'multipart/alternative';
            }
            $this->setMessageType();
            // Refuse to send an empty message unless we are specifically allowing it
            if (!$this->AllowEmpty and empty($this->Body)) {
                throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL);
            }
            // Create body before headers in case body makes changes to headers (e.g. altering transfer encoding)
            $this->MIMEHeader = '';
            $this->MIMEBody = $this->createBody();
            // createBody may have added some headers, so retain them
            $tempheaders = $this->MIMEHeader;
            $this->MIMEHeader = $this->createHeader();
            $this->MIMEHeader .= $tempheaders;
            // To capture the complete message when using mail(), create
            // an extra header list which createHeader() doesn't fold in
            if ($this->Mailer == 'mail') {
                if (count($this->to) > 0) {
                    $this->mailHeader .= $this->addrAppend('To', $this->to);
                } else {
                    $this->mailHeader .= $this->headerLine('To', 'undisclosed-recipients:;');
                }
                $this->mailHeader .= $this->headerLine('Subject', $this->encodeHeader($this->secureHeader(trim($this->Subject))));
            }
            // Sign with DKIM if enabled
            if (!empty($this->DKIM_domain) && !empty($this->DKIM_selector) && (!empty($this->DKIM_private_string) || !empty($this->DKIM_private) && file_exists($this->DKIM_private))) {
                $header_dkim = $this->DKIM_Add($this->MIMEHeader . $this->mailHeader, $this->encodeHeader($this->secureHeader($this->Subject)), $this->MIMEBody);
                $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . self::CRLF . str_replace("\r\n", "\n", $header_dkim) . self::CRLF;
            }
            return true;
        } catch (phpmailerException $exc) {
            $this->setError($exc->getMessage());
            if ($this->exceptions) {
                throw $exc;
            }
            return false;
        }
    }

Usage Example

Exemplo n.º 1
2
 function enviar()
 {
     if ($this->cliente->getAccessToken()) {
         $service = new Google_Service_Gmail($this->cliente);
         try {
             $mail = new PHPMailer();
             $mail->CharSet = "UTF-8";
             $mail->From = Contants::FROM;
             $mail->FromName = Contants::ALIAS;
             $mail->AddAddress($this->destino);
             $mail->AddReplyTo(Contants::FROM, Contants::ALIAS);
             $mail->Subject = $this->asunto;
             $mail->Body = $this->mensaje;
             $mail->preSend();
             $mime = $mail->getSentMIMEMessage();
             $mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
             $mensaje = new Google_Service_Gmail_Message();
             $mensaje->setRaw($mime);
             $service->users_messages->send('me', $mensaje);
             $r = 1;
         } catch (Exception $e) {
             print $e->getMessage();
             $r = 0;
         }
     } else {
         $r = -1;
     }
     return $r;
 }
All Usage Examples Of PHPMailer::preSend