Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Passing variables into email body


ArtcoInc

Recommended Posts

I'm working on a project where, when an item is out of stock, the customer can ask to be notified when the item is back in stock. When I send the email notifying the customer that the item is back in stock, I want to include in the body of the email both the name of the item, and the date the customer made the request to be notified.

 

On the screen, the email body shows the date and item name in the email body,

 

 

 

But, when the email is sent, both of these are stripped out.

 

Below is the (still in testing) code, which I've adapted from /admin/mail.php.

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : '');

  $customer_name           = tep_db_prepare_input($HTTP_POST_VARS['customer_name']);
  $customers_email_address = tep_db_prepare_input($HTTP_POST_VARS['customers_email_address']);
  $mail_sent_to            = tep_db_prepare_input($HTTP_POST_VARS['customers_email_address']);
  $from                    = tep_db_prepare_input($HTTP_POST_VARS['from']);
  $subject                 = tep_db_prepare_input($HTTP_POST_VARS['subject']);
//  $message                 = tep_db_prepare_input($HTTP_POST_VARS['message']);
  $date                    = tep_db_prepare_input($HTTP_POST_VARS['date']);
  $part                    = tep_db_prepare_input($HTTP_POST_VARS['part']);

  $sender_name             = 'Shelbo Whisper Tips';

  $message  = 'On ';
  $message .= substr(($HTTP_GET_VARS['date']),0,10);
  $message .= ', you asked us to notify you when a ';
  $message .= ($HTTP_GET_VARS['part']);
  $message .= ' was back in stock. We now have these in stock again. Thank you for your patience!' ;



  if ( ($action == 'send_email_to_user') && isset($HTTP_POST_VARS['customers_email_address']) && !isset($HTTP_POST_VARS['back_x']) ) {


    //Let's build a message object using the email class
    $mimemessage = new email(array('X-Mailer: osCommerce'));

    // Build the text version
    $text = strip_tags($message);
    if (EMAIL_USE_HTML == 'true') {
      $mimemessage->add_html($message, $text);
    } else {
      $mimemessage->add_text($text);
    }

    $mimemessage->build_message();
    $mimemessage->send($customer_name, $customers_email_address, $sender_name, $from, $subject);

    tep_redirect(tep_href_link(FILENAME_STOCK_ALERT_EMAIL, 'mail_sent_to=' . urlencode($mail_sent_to)));
  }

//  if (!isset($HTTP_POST_VARS['customers_email_address']) ) {
//    $messageStack->add(ERROR_NO_CUSTOMER_SELECTED, 'error');
//  }

  if (isset($HTTP_GET_VARS['mail_sent_to'])) {
    $messageStack->add(sprintf(NOTICE_EMAIL_SENT_TO, $HTTP_GET_VARS['mail_sent_to']), 'success');
  }

  require(DIR_WS_INCLUDES . 'template_top.php');
?>

    <table border="0" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td width="100%">
          <table border="0" width="100%" cellspacing="0" cellpadding="0">
            <tr>
              <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
              <td class="pageHeading" align="right">
                <?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?>
              </td>
            </tr>
          </table>
        </td>
      </tr>

      <tr>
        <td>
          <table border="0" width="100%" cellspacing="0" cellpadding="2">
            <tr>
              <?php echo tep_draw_form('mail', FILENAME_STOCK_ALERT_EMAIL, 'action=send_email_to_user'); ?>
                <td>
                  <table border="0" width="100%" cellpadding="0" cellspacing="2">
                    <tr>
                      <td>
                        <?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?>
                      </td>
                    </tr>

                    <tr>
                      <td class="main"><?php echo TEXT_CUSTOMER_NAME; ?></td>
                      <td>
                        <?php echo tep_draw_input_field('customer_name', $customer_name);?>
                      </td>
                    </tr>

                    <tr>
                      <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
                    </tr>

                    <tr>
                      <td class="main"><?php echo TEXT_CUSTOMER_EMAIL; ?></td>
                      <td>
                        <?php echo tep_draw_input_field('customers_email_address', $customers_email_address);?>
                      </td>
                    </tr>

                    <tr>
                      <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
                    </tr>

                    <tr>
                      <td class="main"><?php echo TEXT_FROM; ?></td>
                      <td><?php echo tep_draw_input_field('from', $from); ?></td>
                    </tr>

                    <tr>
                      <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
                    </tr>

                    <tr>
                      <td class="main"><?php echo TEXT_SUBJECT; ?></td>
                      <td><?php echo tep_draw_input_field('subject', $subject); ?></td>
                    </tr>


<!-- for testing only - Start -->

                    <tr>
                      <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
                    </tr>

                    <tr>
                      <td class="main"><?php echo 'Date '; ?></td>
                      <td><?php echo tep_draw_input_field('date', $date); ?></td>
                    </tr>

                    <tr>
                      <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
                    </tr>

                    <tr>
                      <td class="main"><?php echo 'Part '; ?></td>
                      <td><?php echo tep_draw_input_field('part', $part); ?></td>
                    </tr>

<!-- for testing only - End -->


                    <tr>
                      <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
                    </tr>

                    <tr>
                      <td valign="top" class="main"><?php echo TEXT_MESSAGE; ?></td>
                      <td><?php echo tep_draw_textarea_field('message', 'soft', '60', '15', $message); ?></td>
                    </tr>

                    <tr>
                      <td colspan="2"><?php echo tep_draw_separator('pixel_trans.gif', '1', '10'); ?></td>
                    </tr>

<?php

/* Re-Post all POST'ed variables */

//                    reset($HTTP_POST_VARS);
//                    while (list($key, $value) = each($HTTP_POST_VARS)) {
//                      if (!is_array($HTTP_POST_VARS[$key])) {
//                        echo tep_draw_hidden_field($key, htmlspecialchars(stripslashes($value)));
//                      }
//                    }


                    echo tep_draw_button(IMAGE_SEND_EMAIL, 'mail-closed', null, 'primary') .
                         tep_draw_button(IMAGE_CANCEL, 'close', tep_href_link(FILENAME_STOCK_ALERT));
?>


                  </table>
                </td>
              </form>
            </tr>
          </table>
        </td>
      </tr>
    </table>

<?php
  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>

Any guidance or assistance would be welcome! Thanks!

 

Malcolm

Link to comment
Share on other sites

@@multimixer , Thank you very much!

 

I had tried that earlier, but the date and part number did not appear on the screen. Since it *looked* like the message was not complete, I never tried actually sending the email.

 

Anyway, I just tried it again. Using this:

  $message  = 'On ';
  $message .= substr($date,0,10);
  $message .= ', you asked us to notify you when a ';
  $message .= $part;
  $message .= ' was back in stock. We now have these in stock again. Thank you for your patience!' ;

The date and part number still does not show on the screen ...

 

 

 

However, the email *does* have the date and part in the message, even if it isn't shown on the screen!

 

One more thing ... I know I can edit the customer's email address, and the email will be sent to the new edited address. However, I can not add to or otherwise edit the message. I realize that when I click on the 'Send Email' button, the code loops back to the top, and re-initializes the variables. Is there any way to also be able to edit the message before sending it?

 

Thank you again!

 

Malcolm

Link to comment
Share on other sites

So, at someone's suggestion, I've tried this small piece of code three ways now:

 

1)

  $message  = 'On ';
  $message .= substr(($HTTP_GET_VARS['date']),0,10);
  $message .= ', you asked us to notify you when a ';
  $message .= ($HTTP_GET_VARS['part']);
  $message .= ' was back in stock. We now have these in stock again. Thank you for your patience!' ;

Result: Variables are shown on the screen, but are not included in the email.

 

 

2)

  $message  = 'On ';
  $message .= substr($date,0,10);
  $message .= ', you asked us to notify you when a ';
  $message .= $part;
  $message .= ' was back in stock. We now have these in stock again. Thank you for your patience!' ;

Results: Variables are not shown on the screen, but are included in the email.

 

 

3)

  $message  = 'On ';
  $message .= substr(($_POST['date']),0,10);
  $message .= ', you asked us to notify you when a ';
  $message .= ($_POST['part']);
  $message .= ' was back in stock. We now have these in stock again. Thank you for your patience!' ;

Results: Variables are not shown on the screen, but are included in the email.

 

 

Is there any way to have the variables show up in both the screen and the email message? And, I still want to come up with a way to have the code generate the email message, display it on the screen before sending it, allow me to add to/edit the message, and send the edited message in the email.

 

Malcolm

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...