PHP email is truncated

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Montana Gramps
    New Member
    • Mar 2007
    • 3

    PHP email is truncated

    Hi,

    I work at a newspaper.

    I'm attempting to use the PHP mail function to send a computer generated message to an email list. The email message contains html, text and photos. Its the major headlines and pictures from the day's current news.

    The email message is getting truncated. The HTML displays fine in a browser.

    I've checked the HTML syntax. But I'm only sending half the message. No error messages are returned.

    Mike
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    Hi Mike,
    how do you planing to send HTML emails, is it a Remote web server or are you using our local machine's web server and PHP.
    If it is a news paper day by day you have to change the headings and other elements. Is there any back end system to arrange those stuffs with out change the mail page manually.

    And i need the script that you have completed so far.

    Comment

    • Montana Gramps
      New Member
      • Mar 2007
      • 3

      #3
      I'm using a remote server.
      Here's the script followed by the raw email content its generating.

      <?php

      /* CONNECT TO THE DB AND BUILD HASH OF EMAIL's*/

      $root = $_SERVER[ROOT_CONTENT_FO LDER];

      // Connect to email list db

      $connection = mysql_connect(' db.townnews.com ', 'dinterlake', <?=$password? >) or die ('Unable to connect!');

      mysql_select_db ('dailyinterlak e_com') or die ('Unable to select database');

      $results = mysql_query("se lect * from cust_email_list WHERE avail = 'Y'");

      while ($row = mysql_fetch_ass oc($results))
      {
      foreach($row as $key=>$val)
      {
      if(preg_match("/email/", "$key"))
      {
      $email_list[] .= $val;
      }
      }
      }


      /* BEGIN MESSAGE TO SEND */

      $site = "http://www.dailyinterl ake.com/content/current";

      $msg .= '<table width="80%"><tr ><td>';

      $msg .= '<h3>THE DAILY INTER LAKE NEWS ALERT</h3>
      Bringing you the latest news from around the Flathead Valley<br/>
      =============== =============== =============== ==<br/><br/>';

      /* BUILD CONTENT FOR EACH NEWS CATEGORY */

      foreach(array(' breaking_news', 'news','law_rou ndup','sports', 'business','lif estyle', 'weather', 'traffic') as $folder)
      {
      $filenames = '';

      $dir = $root . '/' . $folder;

      /* OPEN CONTENT FOLDER ON SERVER */

      if ($dh = opendir($dir))
      {
      while (($file = readdir($dh)) !== false)
      {
      if(preg_match("/([a-zA-Z0-9])+.txt/", $file)) // Don't save non-text files
      {
      $filenames[] .= $file; // Add to filenames array
      }
      else if(preg_match("/([a-zA-Z0-9])+.jpg/", $file)) // If photo is availble, add to hash with text file as key
      {
      $key = preg_replace("/.jpg/", '.txt', $file); // photo file name is same as corresponding text file
      $photos[$key] = $file;
      }

      }
      }

      if(!$filenames) // couldn't find any for this news category
      {
      continue;
      }

      sort($filenames );


      /* CREATE NEWS HEADING AND ADD TO MESSAGE */
      $fldr = strtoupper($fol der);

      $msg .= "<h3>$fldr</h3>\n";


      /* GET FILE CONTENTS FOR EACH FILE IN ARRAY */

      foreach($filena mes as $file)
      {

      $filename = $dir . '/' . $file;

      if(file_exists( "$filename" ))
      {

      $handle = fopen($filename , "r");
      $content = fread($handle, filesize($filen ame));
      fclose($handle) ;

      /* CLEANUP FILE CONTENTS */

      $content = preg_replace("/ +/", ' ', $content); // compress whitespace
      $content = preg_replace("/\n+/", "\n", $content); // remove blank lines
      $content = preg_replace("/^ +/", '', $content); // remove leading whitespace
      $content = preg_replace("/^\n+/", '', $content); // remove leading line feeds
      $content = preg_replace("~ ^[bB]y ([^\n])+\n~", "By $1 ", $content); // Fix byline in content
      $content = preg_replace("~ ^[bB]y ([^\n])+<br/?>~", "By $1 ", $content);

      $parts = preg_split("/\n/", $content); // split content into lines

      // initialize

      $head = '';
      $byline = '';
      $kicker = '';
      $teaser = '';


      /* DETECT PRINCIPAL PARTS OF CONTENT (Heading, byline, kicker, teaser ) */

      //Use XML tags if present

      $p = 1;

      foreach($parts as $part)
      {
      $sansxml = preg_replace("/<[^>]+>/", '', $part);

      if(preg_match("/<!--head/", $part) && ($head == ''))
      {
      $head = $sansxml;
      }
      else if(preg_match("/<!--byline/", $part) && ($byline == ''))
      {
      $byline = $sansxml;
      }
      else if(preg_match("/<!--kicker/", $part) && ($kicker == ''))
      {
      $kicker = $sansxml;
      }
      else if(preg_match("/[a-zA-Z]/", $part) && ($teaser == '') && ($header != ''))
      {
      $teaser .= $sansxml;
      }
      else
      {
      }

      if($p > 5) // Don't look at more than 5 lines
      {
      break;
      }
      }

      // use position if no xml

      $p = 1;

      foreach($parts as $part)
      {

      if(!preg_match( "/[a-zA-Z]/", $part))
      {
      continue;
      }

      if($byline == '' && preg_match("/^[Bb]y /", $part))
      {

      if(preg_match("/[dD]aily\s*[iI]nter\s*[lL]ake/", $part))
      {
      $byline = $part;
      }
      else if(preg_match("/[dD]aily\s*[iI]nter\s*[lL]ake/", $part[$p]))
      {
      $byline = $part;
      $part[$p] = '';
      }
      }
      else if($head == '')
      {

      $head = $part;
      }
      else if($teaser == '')
      {

      $teaser.= $part;

      }

      $p++;

      if($p > 5) // Don't look at more than 5 lines
      {
      break;
      }

      }

      /* ADD HEAD TO MESSAGE STRING */

      $content = '<h2>' . $head . '</h2>' . "\n";


      /* ADD CONTENT PARTS TO MESSAGE STRING */

      if($photos[$file] != '') // Add photo if there
      {
      $pic = $photos[$file];
      $content .= "<img src=\"$site/$folder/$pic\" width = '400px'><p>\n";
      }

      if($byline != '')
      {
      $content .= "<i><b>$byl ine</b></i><p>\n"; // Add byline to string
      }

      if($kicker != '')
      {
      $content .= "<b>$kicker </b><p>\n"; // Add kicker to string
      }

      if($teaser != '')
      {
      $content .= "<b>$teaser </b><p>\n"; // Add teaser to string
      }


      /* ADD MORE LINK TO STRING */

      $more = "http://www.dailyinterl ake.com/$folder/$file";

      $content .= "<p><a href=\"$more\"> Read More</a>\n";


      /* ADD HORIZONTAL RULE TO SEPARATE NEWS SECTIONS */

      $msg .= $content . '<P><hr></P>' . "\n";


      }

      }


      }

      /* END MESSAGE STRING */

      $msg .= '</td></tr></table>' . "\n";


      /* CREATE 'TO' STRING BY imploding ALL EMAIL ADDRESSES */

      $to = implode(', ', $email_list);

      // subject
      $subject = 'Daily Inter Lake News Alert';

      // To send HTML mail, the Content-type header must be set

      $headers = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      $headers .= 'From: News Alert <webmaster@dail yinterlake.com> ' . "\r\n";

      // Mail it

      $mail_sent = mail($to, $subject, $msg, $headers) or die ('Unable to mail');;

      ?>

      -------------------------------------------------------------------------------------------------
      Here's the raw email source (minus the heading stuff)

      <table width="80%"><tr ><td><h3>THE DAILY INTER LAKE NEWS ALERT</h3>
      Bringing you the latest news from around the Flathead Valley<br/>
      =============== =============== =============== ==<br/><br/><h3>NEWS</h3>
      <h2>Spring cleaning</h2>
      <img src="http://www.dailyinterl ake.com/content/current/news/news01.jpg" width = '400px'><p>
      <p><a href="http://www.dailyinterl ake.com/news/news01.txt">Rea d More</a>
      <P><hr></P>
      <h2>Biologist s pan subdivision rules</h2>
      <i><b>By WILLIAM L. SPENCE<br>The Daily Inter Lake</b></i><p>
      <b>Farmers also oppose proposals</b><p>
      <p><a href="http://www.dailyinterl ake.com/news/news02.txt">Rea d More</a>
      <P><hr></P>
      <h2>Counties may get funding increase</h2>
      <i><b>By JIM MANN<br>The Daily Inter Lake</b></i><p>
      <b>Montana counties would get substantially more money under a payment package announced Tuesday by Sen. Max Baucus, D-Mont. </b><p>
      <p><a href="http://www.dailyinterl ake.com/news/news03.txt">Rea d More</a>
      <P><hr></P>
      <h2>Whitefish stalls vote on control outside city</h2>
      <i><b>By LYNNETTE HINTZE<br>The Daily Inter Lake</b></i><p>
      <b>A proposal to give the city of Whitefish more control in its unincorporated planning area was put on hold Monday to allow time for county officials to address their concerns.</b><p>
      <p><a href="http://www.dailyinterl ake.com/news/news04.txt">Rea d More</a>
      <P><hr></P>
      <h2>Subdivisi on OK’d on Church Slough</h2>
      <i><b>By JIM MANN<br>The Daily Inter Lake</b></i><p>
      <b>Flathead County commissioners on Tuesday granted preliminary approval for a subdivision on Church Slough, with an agreement that paves the way for a public boat access.</b><p>
      <p><a href="http://www.dailyinterl ake.com/news/news05.txt">Rea d More</a>
      <P><hr></P>
      <h2>Whitefish , state blaze new deal for trails</h2>
      <i><b>By LYNNETTE HINTZE<br>The Daily Inter Lake</b></i><p>
      <b>A precedent-setting land-use license will allow the city of Whitefish to move forward with its Trail Runs Through It project without having to buy easements on state school trust land.</b><p>
      <p><a href="http://www.dailyinterl ake.com/news/news06.txt">Rea d More</a>
      <P><hr></P>
      <h2>Yea or neigh</h2>
      <i><b>By JOHN STANG</b></i><p>
      <b><!--byline-->By JOHN STANG</b><p>
      <p><a href="http://www.dailyinterl ake.com/news/news07.txt">Rea d More</a>
      <P><hr></P>
      <h3>LAW_ROUNDUP </h3>
      <h2>A night of restless teens, assaults</h2>
      <b>Reports of rowdy youths wreaking havoc keep Kalispell police busy Monday evening.</b><p>
      <p><a href="http://www.dailyinterl ake.com/law_roundup/law_roundup01.t xt">Read More</a>
      <P><hr></P>
      <h2>Flathead County Sheriff</h2>
      <b>He was extremely drunk when an ambulance brought him to the emergency room, and medical personnel didn’t want him to leave. He had other ideas, and since he couldn’t get a taxi, a Flathead County Sheriff’s deputy took him home to Bigfork.</b><p>
      <p><a href="http://www.dailyinterl ake.com/law_roundup/law_roundup02.t xt">Read More</a>
      <P><hr></P>
      <h2>Columbia Falls police</h2>
      <b>Columbia Falls police are investigating a report of a 12-year-old boy being assaulted on the bike path near the junior high school. He suffered cuts and broken bones.</b><p>
      <p><a href="http://www.dailyinterl ake.com/law_roundup/law_roundup03.t xt">Read More</a>
      <P><hr></P>
      <h2>Whitefish ambulance</h2>
      <b>A man who appeared to be in his mid-20s got sick and passed out at the Whitefish train depot. Whitefish ambulance took him to the hospital.</b><p>
      <p><a href="http://www.dailyinterl ake.com/law_roundup/law_roundup04.t xt">Read More</a>
      <P><hr></P>
      <h3>SPORTS</h3>
      <h2>Raising a ruck-us

      Comment

      • Montana Gramps
        New Member
        • Mar 2007
        • 3

        #4
        Thanks all.

        I found the problem.

        Montana Gramps

        Comment

        Working...