Contao\Email::sendTo PHP Метод

sendTo() публичный Метод

Friendly name portions (e.g. Admin ) are allowed. The method takes an unlimited number of recipient addresses.
public sendTo ( ) : boolean
Результат boolean True if the e-mail was sent successfully
    public function sendTo()
    {
        $arrRecipients = $this->compileRecipients(func_get_args());
        if (empty($arrRecipients)) {
            return false;
        }
        $this->objMessage->setTo($arrRecipients);
        $this->objMessage->setCharset($this->strCharset);
        // Default subject
        if ($this->strSubject == '') {
            $this->strSubject = 'No subject';
        }
        $this->objMessage->setSubject($this->strSubject);
        // HTML e-mail
        if ($this->strHtml != '') {
            // Embed images
            if ($this->blnEmbedImages) {
                if ($this->strImageDir == '') {
                    $this->strImageDir = TL_ROOT . '/';
                }
                $arrCid = array();
                $arrMatches = array();
                $strBase = \Environment::get('base');
                // Thanks to @ofriedrich and @aschempp (see #4562)
                preg_match_all('/<[a-z][a-z0-9]*\\b[^>]*((src=|background=|url\\()["\']??)(.+\\.(jpe?g|png|gif|bmp|tiff?|swf))(["\' ]??(\\)??))[^>]*>/Ui', $this->strHtml, $arrMatches);
                // Check for internal images
                if (!empty($arrMatches) && isset($arrMatches[0])) {
                    for ($i = 0, $c = count($arrMatches[0]); $i < $c; $i++) {
                        $url = $arrMatches[3][$i];
                        // Try to remove the base URL
                        $src = str_replace($strBase, '', $url);
                        $src = rawurldecode($src);
                        // see #3713
                        // Embed the image if the URL is now relative
                        if (!preg_match('@^https?://@', $src) && file_exists($this->strImageDir . $src)) {
                            if (!isset($arrCid[$src])) {
                                $arrCid[$src] = $this->objMessage->embed(\Swift_EmbeddedFile::fromPath($this->strImageDir . $src));
                            }
                            $this->strHtml = str_replace($arrMatches[1][$i] . $arrMatches[3][$i] . $arrMatches[5][$i], $arrMatches[1][$i] . $arrCid[$src] . $arrMatches[5][$i], $this->strHtml);
                        }
                    }
                }
            }
            $this->objMessage->setBody($this->strHtml, 'text/html');
        }
        // Text content
        if ($this->strText != '') {
            if ($this->strHtml != '') {
                $this->objMessage->addPart($this->strText, 'text/plain');
            } else {
                $this->objMessage->setBody($this->strText, 'text/plain');
            }
        }
        // Add the administrator e-mail as default sender
        if ($this->strSender == '') {
            list($this->strSenderName, $this->strSender) = \StringUtil::splitFriendlyEmail(\Config::get('adminEmail'));
        }
        // Sender
        if ($this->strSenderName != '') {
            $this->objMessage->setFrom(array($this->strSender => $this->strSenderName));
        } else {
            $this->objMessage->setFrom($this->strSender);
        }
        // Set the return path (see #5004)
        $this->objMessage->setReturnPath($this->strSender);
        // Send the e-mail
        $intSent = \System::getContainer()->get('swiftmailer.mailer')->send($this->objMessage, $this->arrFailures);
        // Log failures
        if (!empty($this->arrFailures)) {
            \System::log('E-mail address rejected: ' . implode(', ', $this->arrFailures), __METHOD__, $this->strLogFile);
        }
        // Return if no e-mails have been sent
        if ($intSent < 1) {
            return false;
        }
        $arrCc = $this->objMessage->getCc();
        $arrBcc = $this->objMessage->getBcc();
        // Add a log entry
        $strMessage = 'An e-mail has been sent to ' . implode(', ', array_keys($this->objMessage->getTo()));
        if (!empty($arrCc)) {
            $strMessage .= ', CC to ' . implode(', ', array_keys($arrCc));
        }
        if (!empty($arrBcc)) {
            $strMessage .= ', BCC to ' . implode(', ', array_keys($arrBcc));
        }
        \System::log($strMessage, __METHOD__, $this->strLogFile);
        return true;
    }

Usage Example

Пример #1
0
 /**
  * Send an admin notification e-mail
  *
  * @param integer $intId
  * @param array   $arrData
  */
 protected function sendAdminNotification($intId, $arrData)
 {
     $objEmail = new \Email();
     $objEmail->from = $GLOBALS['TL_ADMIN_EMAIL'];
     $objEmail->fromName = $GLOBALS['TL_ADMIN_NAME'];
     $objEmail->subject = sprintf($GLOBALS['TL_LANG']['MSC']['adminSubject'], \Idna::decode(\Environment::get('host')));
     $strData = "\n\n";
     // Add user details
     foreach ($arrData as $k => $v) {
         if ($k == 'password' || $k == 'tstamp' || $k == 'activation' || $k == 'dateAdded') {
             continue;
         }
         $v = \StringUtil::deserialize($v);
         if ($k == 'dateOfBirth' && strlen($v)) {
             $v = \Date::parse(\Config::get('dateFormat'), $v);
         }
         $strData .= $GLOBALS['TL_LANG']['tl_member'][$k][0] . ': ' . (is_array($v) ? implode(', ', $v) : $v) . "\n";
     }
     $objEmail->text = sprintf($GLOBALS['TL_LANG']['MSC']['adminText'], $intId, $strData . "\n") . "\n";
     $objEmail->sendTo($GLOBALS['TL_ADMIN_EMAIL']);
     $this->log('A new user (ID ' . $intId . ') has registered on the website', __METHOD__, TL_ACCESS);
 }
All Usage Examples Of Contao\Email::sendTo