Pimcore\Mail::init PHP Метод

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

Initializes the mailer with the settings form Settings -> System -> Email Settings
public init ( $type = "email" ) : void
Результат void
    public function init($type = "email")
    {
        $systemConfig = \Pimcore\Config::getSystemConfig()->toArray();
        $emailSettings =& $systemConfig[$type];
        if ($emailSettings['sender']['email']) {
            \Zend_Mail::setDefaultFrom($emailSettings['sender']['email'], $emailSettings['sender']['name']);
        }
        if ($emailSettings['return']['email']) {
            \Zend_Mail::setDefaultReplyTo($emailSettings['return']['email'], $emailSettings['return']['name']);
        }
        if ($emailSettings['method'] == "smtp") {
            $config = [];
            if ($emailSettings['smtp']['name']) {
                $config['name'] = $emailSettings['smtp']['name'];
            }
            if ($emailSettings['smtp']['ssl']) {
                $config['ssl'] = $emailSettings['smtp']['ssl'];
            }
            if ($emailSettings['smtp']['port']) {
                $config['port'] = $emailSettings['smtp']['port'];
            }
            if ($emailSettings['smtp']['auth']['method']) {
                $config['auth'] = $emailSettings['smtp']['auth']['method'];
                $config['username'] = $emailSettings['smtp']['auth']['username'];
                $config['password'] = $emailSettings['smtp']['auth']['password'];
            }
            $transport = new \Zend_Mail_Transport_Smtp($emailSettings['smtp']['host'], $config);
            \Zend_Mail::setDefaultTransport($transport);
        }
        //setting debug email addresses
        if ($type == "email" && empty(self::$debugEmailAddresses)) {
            if ($emailSettings['debug']['emailaddresses']) {
                foreach (explode(',', $emailSettings['debug']['emailaddresses']) as $emailAddress) {
                    self::$debugEmailAddresses[] = $emailAddress;
                }
            }
        }
        $this->placeholderObject = new \Pimcore\Placeholder();
    }

Usage Example

Пример #1
0
 /**
  * @param Model\Tool\Newsletter\Config $newsletter
  * @param Object\Concrete $object
  */
 public static function sendMail($newsletter, $object, $emailAddress = null, $hostUrl = null)
 {
     $params = ["gender" => $object->getGender(), 'firstname' => $object->getFirstname(), 'lastname' => $object->getLastname(), "email" => $object->getEmail(), 'token' => $object->getProperty("token"), "object" => $object];
     $mail = new Mail();
     $mail->setIgnoreDebugMode(true);
     if (\Pimcore\Config::getSystemConfig()->newsletter->usespecific) {
         $mail->init("newsletter");
     }
     if (!Tool::getHostUrl() && $hostUrl) {
         $mail->setHostUrl($hostUrl);
     }
     if ($emailAddress) {
         $mail->addTo($emailAddress);
     } else {
         $mail->addTo($object->getEmail());
     }
     $mail->setDocument(Document::getById($newsletter->getDocument()));
     $mail->setParams($params);
     // render the document and rewrite the links (if analytics is enabled)
     if ($newsletter->getGoogleAnalytics()) {
         if ($content = $mail->getBodyHtmlRendered()) {
             include_once "simple_html_dom.php";
             $html = str_get_html($content);
             if ($html) {
                 $links = $html->find("a");
                 foreach ($links as $link) {
                     if (preg_match("/^(mailto)/", trim(strtolower($link->href)))) {
                         continue;
                     }
                     $glue = "?";
                     if (strpos($link->href, "?")) {
                         $glue = "&";
                     }
                     $link->href = $link->href . $glue . "utm_source=Newsletter&utm_medium=Email&utm_campaign=" . $newsletter->getName();
                 }
                 $content = $html->save();
                 $html->clear();
                 unset($html);
             }
             $mail->setBodyHtml($content);
         }
     }
     $mail->send();
 }