Hi all. I'm new to this forum.

I'm trying to send small bulk personalized emails (data driven) using htmlMimeMail5 without success. Does anyone know if this is possible? When I say small I'm talking about 20 or 30 html emails - small newsletter.

I retrieve a subscribers name, email address and record number from mySQL in an array. I loop through the array like so:

foreach ($addresses as $k=>$v) {
	$first_name = $v['first_name'];
	$rec_id = $v['rec_id'];

$text_part = file_get_contents('../newsletter/text_part.txt');
$html_part = include('../newsletter/newsletter.php');

$mail = new htmlMimeMail5();
$mail->setFrom('someone@somedomain');
$mail->setSubject($data['subject']);
$mail->setPriority('high');
$mail->setReturnPath('me@mydomain');
$mail->setText($text_part);
$mail->setHTML($html_part);
$sent = $mail->send(array($v['email']));
}

Using include or require for the $html_part variable correctly inserts the variable into the body of the email, but the email that is sent is simply the number 1 in the body of the email (the txt part of the email doesn't contain personalized information and is present in the email when viewing source).

If using file_get_contents the php code in the email isn't recognized, but the email is sent correctly without php interpretation.

BTW, From and ReturnPath have been substituted with fake info for this post.

Thanks in advance

    [man]include/man simply tells PHP to open and parse/execute another file as PHP code.

    What does this 'newsletter.php' file look like? Unless you capture all of the e-mail body in a variable and [man]return[/man] it from within that file (or just directly return a string), trying to capture the return value of [man]include[/man] doesn't make any sense (search for 'Handling returns' on the PHP manual page for [man]include[/man]).

      Thanks for your reply. The newsletter.php file is primarily html markup with only a couple of instances of php code using short syntax. I'm defining my variables, functions and the mail script in the file I'm "including" from.

      I don't really want to use include or require. If I understand you correctly I need the enclose all of the newsletter markup in a function, and return the newsletter from that function? With all the funky markup you need to make an html newsletter look right in most all clients that'll probably by ugly.

        Actually tried that, but the php vars in the newsletter aren't being parsed:

        function get_html($path) {
        	if (is_file($path)) {
        		ob_start();
        		include $path;
        		return ob_get_clean();
        	}
        		return false;
        }
        
        $html_part = get_html('../newsletter/newsletter.php');
        
        

        Is there something wrong with my implementation?

          Well now the problem is that of scope. Because now the contents of the file are only able to access variables defined within the scope of that function. so for example:

          // inc.php
          <html>
             <body>
                <h1><?php echo $var; ?></h1>
             </body>
          </html>
          
          
          
          // page1.php
          <?php
          
          $var = 'Hello World';
          ob_start();
          include 'inc.php';
          $var2 = ob_get_clean(); // Now holds <html><body><h1>Hello World</h1></body></html>
          
          // page2.php
          <?php
          
          function get_html($page) {
             ob_start();
             include $page;
             return ob_get_clean();
          }
          
          $var = 'Hello World';
          $var2 = get_html('inc.php'); 
          /* Now holds <html><body><h1>Some notice about undefined variable,
            if notices are being shown with error_reporting(E_ALL) because $var is
            not defined within the scope of the function</h1></body></html> */
          

          Hope that helps

            OK, Scope. So I've passed the variables to the function along with the path to the newsletter file. I'm assuming that if I need to use more variables it might be easier to declare the variables global...

            function get_html($path,$first_name,$rec_id) {
            	if (is_file($path)) {
            		ob_start();
            		include $path;
            		return ob_get_clean();
            	}
            		return false;
            }
            //function call:
            $html_part = get_html('../newsletter/newsletter.php',$first_name,$rec_id);
            

            in newsletter.php, to echo the variable value I'm using short tags:
            (open bracket?=$var;?close bracket)

            NOTE: if you are a list moderator, explicitly denoting the short tags within the BBcode php tags seems to produce live php code!! Try it, like <?= ?>

            I'll do much more testing before I implement this, but guys, can't thank you enough. This is fascinating. I've never used output control functions before. I don't know how much overhead this is going to produce on the server so I'll test it in increments.

            Bill

              bgreen52 wrote:

              I'm assuming that if I need to use more variables it might be easier to declare the variables global...

              It's easier in the same way that falling off a bicycle is easier than riding it 🙂

              Output buffering is already global in scope: once you start buffering, it continues until you end it, including during calls to functions and the like. So what you can do instead is start buffering before calling the [font=monospace]get_html[/font], and collect the result afterwards.

              function get_html($path) {
                  if (is_file($path)) {
                      include $path;
                      return true;
                  }
                  return false;
              }
              
              //function call:
              ob_start();
              $got_html = get_html('../newsletter/newsletter.php');
              $html_part = ob_get_clean();
              

              Note that the function here returns a flag stating whether it was successful or not; it outputs the result of evaluating the included file, with that output being collected by the buffer started just before the function was called.

                bgreen52;10996523 wrote:

                NOTE: if you are a list moderator, explicitly denoting the short tags within the BBcode php tags seems to produce live php code!! Try it, like <?= ?>

                Er.. not sure what gave you that impression, but that's definitely not the case:

                <?= "see what I mean?" ?>

                  Thanks again, Weedpacket. I'm studying. I've revised my code to your recommendations. I'm still having to pass other variables used in the newsletter to the function call.

                    Write a Reply...