Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:25:36+00:00 2026-05-31T08:25:36+00:00

I have a question regarding loops within an email template. My current email library

  • 0

I have a question regarding loops within an email template. My current email library gets passed an array of data and a template(.html) to search and replace. This works fine if I just need to replace any element wrapped in a square bracket [variable] . My library however can not deal with arrays of data(looping) and I need a more elegant solution than the ugly approach below

Cheers.

Email Library

<?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;

 }

Ugly Approach for non-defined templates

Note: sorry about poor formating here…

Instead of using a pre-made template, I have to pass a template on the fly so I can run a foreach loop with the data

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;
                    }

Edit: Answer Thanks to 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;
                    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:25:38+00:00Added an answer on May 31, 2026 at 8:25 am

    Have you considered simply passing data to a view (and returning the content, rather than displaying it)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding handling errors in a J2EE application. Our current application
I have a question regarding nested doseq loops. In the start function, once I
I have a PHP array question regarding searching which I'm hoping some kind person
I have question regarding macros. How could I cast through macro a template class
I have a question regarding the initialization of an array of structs in C.
I have a question regarding C++. This is my current function: string clarifyWord(string str)
I have a question regarding encoding for text email messages using C# .net because
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.