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

  • SEARCH
  • Home
  • 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 983093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:48:04+00:00 2026-05-16T04:48:04+00:00

I have a client that wanted their sales staff to be able to populate

  • 0

I have a client that wanted their sales staff to be able to populate highly designed 2 column html templates and send emails using these. 6 recipients max per email.

Solution worked very well until they decided they want to send to each of the recipients separately. No CC, no BCC.

I figured where I had simply imploded the array of addresses before I would loop instead but this produced emails with empty content. Subject line was fine but the content was zero bytes.

Interestingly if I insert a die() and dump the $html_text to screen I see what I expect to have as content.

Initially I set the content, subject etc outside the loop but seeing empty content I thought perhaps I should set the vars inside the loop though this made no sense to me. It changed nothing.

Here is the looping code:

if (count($_SESSION['recipients']) > 0) {
 foreach ($_SESSION['recipients'] as $to) {
  $template = $_SERVER['DOCUMENT_ROOT'] . '/leads/templates/'.$_SESSION['templateFile'];
  ob_start();
  include($template);
  $html = ob_get_contents();
  ob_end_clean();

  if (strlen($html) == 0) {
   echo "The template at $template did not load.";
   exit;
  }

  $bullets = unslash($_SESSION['bullets']);
  $bullets = preg_replace('/<li>/', '* ', $bullets); 
  $bullets = strip_tags($bullets);
  $TextMessage =  strip_tags(unslash($_SESSION['message'])) . "\n\n" . $bullets;

  $notice_text = "This is a multi-part message in MIME format.";
  $plain_text =  str_replace('&nbsp;',' ', $TextMessage);

  $html_text = $html;

  $email   = $_SESSION['user']->email;
  $name    = $_SESSION['user']->first_name . ' ' . $_SESSION['user']->last_name;
  $from = "$name <$email>";
  $subject = unslash($_SESSION['subject']);

  $semi_rand = md5(time());
  $mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";
  $mime_boundary_header = chr(34) . $mime_boundary . chr(34);

  $body = "$notice_text

  --$mime_boundary
  Content-Type: text/plain; charset=us-ascii
  Content-Transfer-Encoding: 7bit

  $plain_text

  --$mime_boundary
  Content-Type: text/html; charset=us-ascii
  Content-Transfer-Encoding: 7bit

  $html_text

  --$mime_boundary--";

  if (@mail($to, $subject, $body,
   "From: " . $from . "\n" .
   "MIME-Version: 1.0\n" .
   "Content-Type: multipart/alternative;\n" .
   "     boundary=" . $mime_boundary_header)) {
   $out .=  " Email sent to " . htmlentities($to) . ".<br />";
  } else {
   $out .= " Email to htmlentities($to) NOT sent successfully!";
  }
 } // foreach 

Any advice much appreciated!

2010.08.06 – EDIT: I have tried replacing $html_text and $plain_text with hard coded text and found the same results: Empty mail sent. Also tried moving the $mime_boundary and $mime_boundary_header generation outside the loop with no success.

However if I add die('<pre>'.$body.'</pre>'); immediately after the empty mail is sent I see everything I would expect in the HTML and the text and the boundaries… Here is an example:

This is a multi-part message in MIME format.

  --==MULTIPART_BOUNDARY_94e43eed6cf2826fc5787c860814066e
  Content-Type: text/plain; charset=us-ascii
  Content-Transfer-Encoding: 7bit

  My plain text here


* Bullet point 1
* Bullet point 2


  --==MULTIPART_BOUNDARY_94e43eed6cf2826fc5787c860814066e
  Content-Type: text/html; charset=us-ascii
  Content-Transfer-Encoding: 7bit

// RENDERED HTML CONTENTS SHOW UP HERE
  • 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-16T04:48:04+00:00Added an answer on May 16, 2026 at 4:48 am

    I freely admit I have no idea why this succeeds where the foreach() failed but the kludgy solution was to eliminate the foreach() and to assign the six possible addresses like so:

    // #5 //
    if ($to = $_SESSION['recipients'][4]) {
        mail($to, $subject, $body,
        "From: " . $from . "\n" .
        "MIME-Version: 1.0\n" .
        "Content-Type: multipart/alternative;\n" .
        "     boundary=" . $mime_boundary_header);
        echo "Email sent to " . htmlentities($to) . ".<br />";
    }
    

    It does the job here with no other code alterations and as Larry Wall said “Makeshifts last longest.”

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

Sidebar

Related Questions

I have a client that wants their tab application to be searchable in Facebook.
I have a client that wants users to access a private area on their
I have a client that wants an app that will detect the persons location
We have a client that owns Google Apps Business Edition account and wants to
I have a client that has a PostgreSQL database and he cannot remember the
I have a client that is running a custom VB 6 application they wrote
I have a client that wants audio to play while the user is browsing
I have a have Winforms client that uses Web services on a IIS7 (W2008)
I have a client that uses a point-of-sale solution involving an Access database for
I have a client that has an app built with django. On every page

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.