电子邮件搜索和替换电子邮件模板(.html)循环


Email Search and replace email templates(.html) Looping

我有一个关于电子邮件模板中循环的问题。我当前的电子邮件库传递了一个数据数组和一个要搜索和替换的模板(.html)。如果我只需要替换任何封装在方括号[variable]中的元素,这就很好了。然而,我的库无法处理数据数组(循环),我需要一个比下面的丑陋方法更优雅的解决方案

干杯。

电子邮件库

<?php
    if (!defined('BASEPATH'))
    exit('No direct script access allowed');

require_once('phpmailer/phpmailer.php');
 class Mailer{
    protected static $mailer;
    protected static $CI;
    public function __construct()
    {
        self::$mailer = new PHPMailer();
        self::$CI =& get_instance();
        self::$CI->load->helper('file');
    }
    /**
     *
     * @param type $data
     * @param type $template
     * @return type 
     */
    public static function prepIt($data, $template)
    {
         $callback = function ($matches) use ($data){
            return ( isset($data[$matches[1]]) ) 
               ? $data[$matches[1]] 
               : $matches[0];
        };
        return preg_replace_callback(
                '/'[(.*?)']/', 
                $callback, 
                read_file(EMAIL_TEMPLATES . $template));
    }
    /**
     *
     * @param type $data
     * @param type $template
     * @param type $to
     * @param type $subject
     * @param type $prep
     * @return type 
     */
    public static function sendIt($data, $template='', $to, $subject, $prep=false)
    {
        self::$mailer->CharSet = 'utf-8'; 
        if(self::$CI->config->item('email_smtp') === TRUE){
            self::$mailer->SMTPSecure = "ssl";
            self::$mailer->Host= self::$CI->config->item('email_host');  
            self::$mailer->Port= self::$CI->config->item('email_port');
            self::$mailer->Username   = self::$CI->config->item('email_user');  
            self::$mailer->Password   = self::$CI->config->item('email_passw');  
            self::$mailer->SMTPKeepAlive = true;  
            self::$mailer->Mailer = "smtp"; 
            self::$mailer->IsSMTP(); 
            self::$mailer->SMTPAuth   = true;                   
            self::$mailer->SMTPDebug  = 0; 
        }
        if($prep)
        {
            self::$mailer->Body = self::prepIt($data, $template);
        }
        else
        {
            self::$mailer->Body = $data;
        }
        self::$mailer->IsHTML(true);
        self::$mailer->Subject = $subject;
        self::$mailer->AddAddress($to);
        self::$mailer->FromName = self::$CI->config->item('email_from');
        self::$mailer->From = self::$CI->config->item('email_primary');
        try{
            if(self::$mailer->Send()){
                return true;
            }else{
                 throw new phpmailerException(self::$mailer->ErrorInfo);
            }
        }catch(phpmailerException $e){
            log_message('error', $e->getMessage());
        }

    }
    return false;
 }

未定义模板的丑陋方法

注意:很抱歉这里的格式不正确

我必须在运行中传递一个模板,这样我就可以用数据运行foreach循环,而不是使用预先制作的模板

if ($order->update_attributes($update_order)) {
   $output ='<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type"content="text/html; charset=utf-8"/><title>Reciept</title><style type="text/css">#outlook a{padding:0;}
                                body{width:100%!important;}.ReadMsgBody{width:100%;}.ExternalClass{width:100%;}emails at full width*/body{-webkit-text-size-adjust:none;-ms-text-size-adjust:none;}
                                body{margin:0;padding:0;font:normal 14px tahoma,sans-serif;color:#515151;line-height:1.6em;}
                                img{height:auto;line-height:100%;outline:none;text-decoration:none;}
                                a img{border:none;}#backgroundTable{margin:0;padding:0;width:100%!important;}
                                p{margin-bottom:18px;line-height:1.6;color:#767676;}
                                h1,h2,h3,h4,h5,h6{color:black!important;line-height:100%!important;}
                                h1 a,h2 a,h3 a,h4 a,h5 a,h6 a{color:blue!important;}
                                h1 a:active,h2 a:active,h3 a:active,h4 a:active,h5 a:active,h6 a:active{color:red!important;}
                                h1 a:visited,h2 a:visited,h3 a:visited,h4 a:visited,h5 a:visited,h6 a:visited{color:purple!important;}
                                table td{border-collapse:collapse;}
                                table th{text-align:left;padding:25px;}.yshortcuts,.yshortcuts a,.yshortcuts a:link,.yshortcuts a:visited,.yshortcuts a:hover,.yshortcuts a span{color:black;text-decoration:none!important;border-bottom:none!important;background:none!important;}
                                table#product{border-spacing:0;width:100%;}
                                table#product th{padding:8px;}
                                table#product td{padding:8px;border-bottom:1px solid#b1b1b1;}</style></head><body><table width="100%" style="background:#e3e3e3;text-align:center;padding:10px;width:100%;"cellpadding="0"cellspacing="0"border="0"id="backgroundTable"><tr><td><table cellpadding="0"cellspacing="0"border="0"width="650"style="text-align:left;padding:8px;background:#ffffff;border:1px solid #b1b1b1;"><thead><tr><th style="text-align:left;font-size:30px;padding:0;">Philip Kavanagh</th><th style="text-align:right;"><img src="http://localhost/crack_ie/assets/front/img/logo.png"alt=""/></th></tr></thead><tbody><tr><td colspan="2"><h2>Reciept</h2><p>Thank you for your recent purchase(s)from the store.<br>The Order has been proccessed by paypal successfully!</p></td></tr><tr><td colspan="2"><h3>Purchase Information</h3><p>Below you will find links to your digital downloads</p></td></tr><tr><td colspan="2"><table class="product"id="product"><thead><tr><td>&nbsp;</td><td>Title</td><td>Price</td><td>Download</td></tr></thead><tbody>';
      foreach ($email_data as $out) {
          $output .= '
                            <tr><td><img src="'.site_url(MEDIA . 'products/' . $out['img']).'" alt=""/></td><td>'.$out['title'].'</td><td>&euro;'.$out['price'].'</td><td><a href="'.$out['link'].'" style="padding:5px;background:#d53015;color:#fafafa;text-decoration:none;">Download</a></td></tr>
                        ';
                    }
          $output .= '
                        </tbody></table></td></tr><tr><td style="width:70%;">&nbsp;</td><td style="width:30%;text-align:right;"><table style="text-align:right;border-spacing:0;"><tbody><tr><td style="width:100%;padding:5px;">Order#</td><td style="margin-left:30px;"><strong>7782tgh5</strong></td></tr><tr><td style="width:100%;padding:5px;">Items</td><td style="margin-left:30px;">'.$email_additional['items'].'</td></tr><tr><td>Tax</td><td>&euro;0.00</td></tr><tr style="background:#d53015;color:#fafafa;width:100%;"><td>Total Amount</td><td style="font-size: 30px; padding: 8px;">&euro;'.$email_additional['total'].'</td></tr></tbody></table></td></tr></tbody></table></td></tr></table></body></html>
                    ';
          if (Mailer::sendIt($output, '', $user->email, 'Purchase Confirmation: #' . $order->order_sku . '', false)) {
                        return true;
                    }

编辑:答案感谢landons,

if ($order->update_attributes($update_order)) {
          $output = $this->load->view('orders/email', array(
              'data'   => $somedata    
          ), true);
          if (Mailer::sendIt($output, '', $user->email, 'Purchase Confirmation: #' . $order->order_sku . '', false)) {
                        return true;
                    }
}

您是否考虑过简单地将数据传递到视图(并返回内容,而不是显示内容)?