Link to home
Start Free TrialLog in
Avatar of pcshost
pcshost

asked on

Line breaking new array's when posting to email

Using the code below I'm trying to modify a web contact form from someone on the "net' to format the new array's I created which is the first part of the code snippet. The first 4 array's do not line break when returned via email. How do I get them to break? I have included snippets of what the author coded and had suggested and examples posted in the body here of what the results are for both the original and modified code.

Sent from MSC Contact form
==============================

Are you intersted in our Preventive Maintenance Program? Yes Would you like an appointment? Yes Client Information: Medical What Brand? Nikon
Name: test
E-mail: john@x.com
Tel/fax: 132
Country: Canada
Subject: Testing
Turing number: 2876
Please select this box in order to submit form Yes

Part B
------------------ When modified as the author suggest the form post is formatted as shown ----

Sent from MSC Contact form<b==============================<b<bAre you intersted in our Preventive Maintenance Program? Yes<bWould you like an appointment? Yes<bClient Information: Medical<bWhat Brand? Nikon<bName: test<bE-mail: john@pcshost.com<bTel/fax: 132<bCountry: Canada<bSubject: Testing<bTuring number: 2286<bPlease select this box in order to submit form Yes<b<btest<b


Part A ---> Code snippet from formxxx.php page

$contact_form_fields = array(
  
  array('name'    => 'Are you intersted in our Preventive Maintenance Program?',
        'type'    => 'select',
        'require' => 1,
        'default' => 'Select',
        'items'   => array('Yes',
                           'No')),
						   
  array('name'    => 'Would you like an appointment?',
        'type'    => 'select',
        'require' => 1,
        'default' => 'Select a Type',
        'items'   => array('Yes',
                           'No')),
						   
  array('name'    => 'Client Information:',
        'type'    => 'select',
        'require' => 1,
        'default' => 'Select a Type',
        'items'   => array('Medical',
                           'Industrial',
						   'Educational',
						   'Other')),
						   
 array('name'     => 'What Brand?',
        'type'    => 'select',
        'require' => 1,
        'default' => 'Select a Type',
        'items'   => array('Nikon',
                           'Olympus',
						   'Zeiss',
						   'Leitz',
						   'Wild',
						   'Leica',
						   'Reichert',
						   'B&L',
						   'A.O.',
						   'Mitutoyo',
						   'Swift',
						   'Other')),
  array('name'    => 'Name:',
        'type'    => 'name',
        'require' => 1),
  array('name'    => 'E-mail:',
        'type'    => 'email',
        'require' => 1),
  array('name'    => 'Tel/fax:',
        'type'    => 'input',
        'require' => 1),
  array('name'    => 'Country:',
        'type'    => 'select',
        'require' => 1,
        'default' => 'Select a Country',
        'items'   => array('Canada',
		                   'Germany',
						   'Mexico',
                           'United Kingdom',
                           'USA',
						   'Not Listed')),
  array('name'    => 'Subject:',
        'type'    => 'subject',
        'require' => 1),
  array('name'    => 'Message:',
        'type'    => 'textarea',
        'require' => 1),
  array('name'    => 'Attachment:',
        'type'    => 'upload',
        'require' => 0,
        'maxsize' => 128*1024),
  array('name'    => 'Turing number:',
        'type'    => 'turing',
        'require' => 1,
        'url'     => 'contact-form/image.php',
        'prompt'  => 'Enter the number displayed above'),
  array('name'    => '',
        'type'    => 'checkbox',
        'require' => 1,
        'prompt'  => 'Please select this box in order to submit form'));

$contact_form_graph           = false;
$contact_form_xhtml           = false;
$contact_form_email           = "testing@microscopeservicecenter.com";
$contact_form_encoding        = "utf-8";
$contact_form_default_subject = "Microscope Service Request";
$contact_form_message_prefix  = "Sent from MSC Contact form\r\n==============================\r\n\r\n";

include_once "contact-form/contact-form.php";

------------ End of form --------------
Part B
Here's a snippet from the contact-form.php page and what was suggested as a replacement to create the line break is as follows:
(the original code)
 Replace
    $mail_sent = contact_form_mail($contact_form_email, $esubj, $ehead .
 $ebody,
                                   "From: $ename $email>\r\n", 
 $contact_form_encoding,

 isset($_SESSION['contact-form-upload']) ?
 $_SESSION['contact-form-upload'] : array());

> with (suggested replacemnet to fix line breaks)

   $mail_sent = contact_form_mail($contact_form_email, $esubj, 
 strtr($ehead . $ebody, "\r\n", '<br>'),
                                  "From: $ename <$email>\r\n", 
 $contact_form_encoding,

 isset($_SESSION['contact-form-upload']) ?
 $_SESSION['contact-form-upload'] : array());

Open in new window

Avatar of hielo
hielo
Flag of Wallis and Futuna image

where is your implementation/definition of contact_form_mail()?
Avatar of pcshost
pcshost

ASKER


--- Here's the contact form itself. I'm a nube at this as well as an fyi. ---

<?php

$contact_form_msg_clear    = 'Clear';
$contact_form_msg_submit   = 'Submit';
$contact_form_msg_submit   = $contact_form_graph ? '' : $contact_form_msg_submit;

$contact_form_msg_sent     = 'Message sent';
$contact_form_msg_not_sent = 'Message not sent';
$contact_form_msg_invalid  = 'Please, correct the fields marked with red';

?>


<!-- ***** PHP ************************************************************* -->

<?php

// ***** contact_form_mail *****

function contact_form_mail($to, $subject, $message, $headers = '', $charset = 'utf-8', $files = array())
{
  if (!count($files))
  {
    $ext_headers  = $headers;
    $ext_headers .= "Content-Type: text/plain; charset=\"$charset\"\r\n";
    $ext_message  = $message;
  }
  else
  {
    $boundary = 'a6cd792e';
    while (true)
    {
      if (strpos($subject, $boundary) !== false ||
          strpos($message, $boundary) !== false) { $boundary .= dechex(rand(0, 15)) . dechex(rand(0, 15)); continue; }
      foreach ($files as $fi_name => $fi_data)
      if (strpos($fi_name, $boundary) !== false ||
          strpos($fi_data, $boundary) !== false) { $boundary .= dechex(rand(0, 15)) . dechex(rand(0, 15)); continue; }
      break;
    }

      $ext_headers  = $headers;
      $ext_headers .= "MIME-Version: 1.0\r\n";
      $ext_headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";

      $ext_message  = "This is a multi-part message in MIME format.";
      $ext_message .= "\r\n--$boundary\r\n";

      $ext_message .= "Content-Type: text/plain; charset=\"$charset\"\r\n\r\n";
      $ext_message .= $message;
      $ext_message .= "\r\n--$boundary\r\n";

    foreach ($files as $i => $x)
    {
      $ext_message .= "Content-Type: {$x['type']}; name=\"{$x['name']}\"\r\n";
      $ext_message .= "Content-Disposition: attachment\r\n";
      $ext_message .= "Content-Transfer-Encoding: base64\r\n\r\n";
      $ext_message .= chunk_split(base64_encode($x['data']));
      $ext_message .= "\r\n--$boundary\r\n";
    }
  }

  $error_reportings = error_reporting(E_ERROR | E_PARSE);
  $res = mail($to, $subject, $ext_message, $ext_headers);
  $error_reportings = error_reporting($error_reportings);

  return $res;
}

// ***** contact_form_post *****

function contact_form_post($name)
{
  global $contact_form_encoding;
  if (isset($_POST[$name])) return htmlentities($_POST[$name], ENT_COMPAT, $contact_form_encoding);
  if (isset($_GET [$name])) return htmlentities($_GET [$name], ENT_COMPAT, $contact_form_encoding);
  return '';
}

// ***** Send Mail *****

if (count($_POST))
{
  if (get_magic_quotes_gpc() && !function_exists('strip_slashes_deep'))
  {
    function strip_slashes_deep($value)
    {
      if (is_array($value)) return array_map('strip_slashes_deep', $value);
      return stripslashes($value);
    }

    $_GET    = strip_slashes_deep($_GET);
    $_POST   = strip_slashes_deep($_POST);
    $_COOKIE = strip_slashes_deep($_COOKIE);
  }

  $patern_aux1 = "(\\w+(-\\w+)*)";
  $patern_aux2 = "($patern_aux1\\.)*$patern_aux1@($patern_aux1\\.)+$patern_aux1";

  $ename = '';
  $email = '';
  $esubj = $contact_form_default_subject;
  $ehead = $contact_form_message_prefix;
  $ebody = '';
  $valid = true;
  foreach ($contact_form_fields as $i => $x)
  {
    $_POST[$i] = isset($_POST[$i]) ? $_POST[$i] : '';

    if ($x['type'] === 'upload')
    {
      if (isset($_POST["$i-clear"]) && $_POST["$i-clear"])
          unset($_SESSION['contact-form-upload'][$i]);
      if (isset($_FILES[$i])             &&
                $_FILES[$i][    'type']  &&
                $_FILES[$i][    'name']  &&
                $_FILES[$i]['tmp_name']  &&
    file_exists($_FILES[$i]['tmp_name']) &&
       filesize($_FILES[$i]['tmp_name']) <= $x['maxsize'])
                $_SESSION['contact-form-upload'][$i] =
                    array('type' =>                   $_FILES[$i][    'type'],
                          'name' =>                   $_FILES[$i][    'name'],
                          'data' => file_get_contents($_FILES[$i]['tmp_name']));
    }

    if ($x['type'] === 'checkbox'   && trim($_POST[$i]) ||
        $x['type'] === 'department' && trim($_POST[$i]) ||
        $x['type'] === 'input'      && trim($_POST[$i]) ||
        $x['type'] === 'name'       && trim($_POST[$i]) ||
        $x['type'] === 'select'     && trim($_POST[$i]) ||
        $x['type'] === 'subject'    && trim($_POST[$i]) ||
        $x['type'] === 'textarea'   && trim($_POST[$i]) ||
        $x['type'] === 'email'      && preg_match("/^$patern_aux2$/sDX", $_POST[$i]) ||
        $x['type'] === 'turing'     && isset($_SESSION['contact-form-number']) && $_POST[$i] === $_SESSION['contact-form-number'] ||
        $x['type'] === 'upload'     && isset($_SESSION['contact-form-upload'][$i]))
    {
      if ( $x['type'] === 'textarea')
           $ebody .=             "\r\n" . $_POST[$i] . "\r\n";

      if ( $x['type'] !== 'textarea')
      if (!$x['name'] && isset($x['prompt']))
           $ehead .= $x['prompt'] . ' ' . $_POST[$i] . "\r\n";
      else $ehead .= $x['name'  ] . ' ' . $_POST[$i] . "\r\n";
    }
    elseif ($x['require'] || $_POST[$i] !== '')
    {
      $valid = false;
      if (!$x['name'] && isset($x['prompt']))
           $contact_form_fields[$i]['prompt'] = "<em>{$x['prompt']}</em>";
      else $contact_form_fields[$i]['name'  ] = "<em>{$x['name'  ]}</em>";
    }

    switch ($x['type'])
    {
      case 'department': foreach ($x['items'] as $j => $y) if ($y === $_POST[$i]) $contact_form_email = $j; break;
      case 'email':      $email = $_POST[$i]; break;
      case 'name':       $ename = $_POST[$i]; break;
      case 'subject':    $esubj = $_POST[$i]; break;
    }
  }

  if ($valid)
  {
$mail_sent = contact_form_mail($contact_form_email, $esubj, 
 strtr($ehead . $ebody, "\r\n", '<br>'),
                                  "From: $ename <$email>\r\n", 
 $contact_form_encoding,

 isset($_SESSION['contact-form-upload']) ?
 $_SESSION['contact-form-upload'] : array());


    if ($mail_sent)
         echo '<div class="error"><em>'               . $contact_form_msg_sent     . '</em></div>';
    else echo '<div class="error"><em class="error">' . $contact_form_msg_not_sent . '</em></div>';
    if ($mail_sent) $_POST    = array();
    if ($mail_sent) $_SESSION = array();
  }
  else   echo '<div class="error"><em>'               . $contact_form_msg_invalid  . '</em></div>';
}

$_SESSION['contact-form-number'] = str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT);

?>


<!-- ***** HTML ************************************************************ -->

<form method="post" action="<?=$_SERVER['REQUEST_URI'];?>" enctype="multipart/form-data">
<table>
<?php

$slash = $contact_form_xhtml ? '/' : '';
foreach ($contact_form_fields as $i => $x)
{
  ?>
  <tr>
  <th><?=$x['name'];?></th>
  <td>
  <?php
  switch ($x['type'])
  {
    case 'name':
    case 'email':
    case 'input':
    case 'subject':
      ?>
      <div class="input"><input name="<?=$i;?>" type="text" value="<?=contact_form_post($i);?>" <?=$slash;?>></div>
      <?php
      break;
    case 'turing':
      ?>
      <div class="input"><input name="<?=$i;?>" type="text" value="<?=contact_form_post($i);?>" <?=$slash;?>></div>
      <img width="60" height="17" src="<?=$x['url'];?>?sname=<?=session_name();?>&amp;rand=<?=rand();?>" alt="" <?=$slash;?>>
      <br style="clear: both;" <?=$slash;?>>
      <small><?=$x['prompt'];?></small>
      <?php
      break;
    case 'upload':
      ?>
      <input name="<?=$i;?>" type="file" value="<?=contact_form_post($i);?>" <?=$slash;?>>
      <?php
      if (isset($_SESSION['contact-form-upload'][$i]))
      {
        ?>
        <input name="<?=$i;?>-clear" type="checkbox" value="Yes" <?=$slash;?>>
        <?=$contact_form_msg_clear;?> <?=$_SESSION['contact-form-upload'][$i]['name'];?>
        <?php
      }
      break;
    case 'checkbox':
      ?>
      <input name="<?=$i;?>" type="checkbox" value="Yes" <?=contact_form_post($i) ? 'checked="checked"' : '';?> <?=$slash;?>>
      <small><?=$x['prompt'];?></small>
      <?php
      break;
    case 'textarea':
      ?>
      <div class="input"><textarea name="<?=$i;?>" cols="45" rows="6"><?=contact_form_post($i);?></textarea></div>
      <?php
      break;
    case 'select':
    case 'department':
      ?>
      <select name="<?=$i;?>">
      <option value=""><?=$x['default'];?></option>
      <?php foreach ($x['items'] as $j => $y) { ?><option value="<?=$y;?>" <?=contact_form_post($i) === $y ? 'selected="selected"' : '';?>><?=$y;?></option><?php } ?>
      </select>
      <?php
      break;
  }
  ?>
  </td>
  </tr>
  <?php
}

?>

<tr><th></th><td><input id="submit_contact" class="submit" type="submit" value="<?=$contact_form_msg_submit;?>" <?=$slash;?>></td></tr>

</table>
</form>


</div>

Open in new window

Avatar of pcshost

ASKER

--- Here's the contact form itself. I'm a nube at this as well as an fyi. ---

Avatar of pcshost

ASKER

As another sidenote: This is where I think it might be at issue. Is this the way to but a line break in a plain text email?

 strtr($ehead . $ebody, "\r\n", '<br>'),
ASKER CERTIFIED SOLUTION
Avatar of pcshost
pcshost

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi,
Press delete button in question history if u got the answer frm some one assign him pts nothing else.
Or you would have got an email
press abandon question