Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New Lines in email text

Status
Not open for further replies.

Wulfgen

Technical User
Dec 31, 2004
283
US
Hello All,
I'm currently using a php based cart that autoreplys to the admin when an order is processed - it uses this to send the message
Code:
$lang['mail_txt'] =   'You have received a new order. --  Login to the Administration Panel for details. Click here -- [URL unfurl="true"]http://www.theurl.com/cart/admin.php';[/URL]

Is there a way to get the text in the email to format like this:

You have received a new order
-----------------------------
Login to the Administration Panel for details
Click here -- ------------------------------

tried (/n) - (\r) - <br /> that looks bad! and used a lot of spaces, but if the browser window is resized then the formatting goes blah!

any ideas
 
yep, in ALL types of combinations and configurations.. also tried this too; \r\n -- /r/n but nada

errr!
 
if you are sending the mail as html, then you need to insert tags like <br/> or <p> etc. or use "\r\n" and running the mail body throug nl2br() before submitting to the smtp server.
 
no the email is not going out with any html tags - here is the php file - the part that starts with $lang['mail_txt'] is towards the end..... this is what gets sent, this is where I would like to add a few lines to the email text -- any ideas?
Code:
<?php
/*
* English - compatible v1.2
*/
$config['charset'] =      'iso-8859-1';

/*
* Messages
*/
$lang['category_not_found'] =          'Category not found';

$lang['cf_no_word'] =                  'Please fill in all required fields';
$lang['cf_mail'] =                     'Enter e-mail address';
$lang['cf_wrong_value'] =              'Enter correct value';
$lang['cf_to_small_value'] =           'Value is too small';
$lang['cf_txt_to_short'] =             'Text is too short';

$lang['operation_saved'] =                          'Data save completed';
$lang['operation_changed'] =                        'Data update completed';
$lang['operation_sure_delete'] =                    'Do you really want to do this?';
$lang['operation_yes'] =                            'Yes';
$lang['operation_no'] =                             'No';
$lang['operation_deleted'] =                        'Data delete completed';
$lang['operation_completed'] =                      'Operation was successful';
$lang['operation_not_completed'] =                  'Operation was NOT successful';
$lang['operation_especify_all_required_fields'] =   'Please fill in all required fields';
$lang['operation_not_found'] =                      'Data not found';
$lang['operation_go_back'] =                        'go back';

$lang['Order_finished'] = 'Your order has been accepted<br />A receipt has been sent to you.<br />Click The link above to complete your order.';
$lang['Order_print']    = 'Print your order!';

$lang['Logged_in'] =            'Logged in correctly';
$lang['Error_login_or_pass'] =  'Wrong login or password!';
$lang['Logged_out'] =           'Logged out correctly';

$lang['products_not_found'] = 'Products not found';
$lang['Choose_courier'] =     'Choose courier';
$lang['Not_found'] =          'Not found';
$lang['basket_is_empty'] =    'Your cart is empty';

$lang['write_topic'] =      'Enter topic';
$lang['Error_email_send'] = 'Mail sending error';
$lang['Email_send'] =       'E-mail has been sent. Thank You';
$lang['Answer_soon'] =      'We\'ll get back to you soon';

$lang['Fill_login'] =                 'Enter login';
$lang['Fill_password'] =              'Enter password';
$lang['Fill_product_list'] =          'Enter products list limit on per page';
$lang['Fill_admin_list'] =            'Enter admin list limit on per page';
$lang['Fill_products_photo_size'] =   'Enter product photo size';
$lang['Fill_categories_photo_size'] = 'Enter category photo size';

/*
* Name of fields in forms
*/
$lang['Category'] =       'Category';
$lang['Order'] =          'Order';
$lang['Name'] =           'Name';
$lang['Price'] =          'Price';
$lang['Quantity'] =       'Quantity';
$lang['Summary'] =        'Total';
$lang['Summary_basket'] = 'Subtotal';
$lang['First_name'] =     'First Name';
$lang['Last_name'] =      'Last Name';
$lang['Company'] =        'Company';
$lang['Street'] =         'Street';
$lang['Zip_code'] =       'Zip Code';
$lang['City'] =           'City';
$lang['Telephone'] =      'Telephone';
$lang['Email'] =          'Email';
$lang['Courier'] =        'Courier';
$lang['Login'] =          'Login';
$lang['Password'] =       'Password';
$lang['File'] =           'File';
$lang['Files'] =          'Files';
$lang['Add_file'] =       'Add File';
$lang['Added_files'] =    'Files added to product';
$lang['First_last_name'] ='First and Last Name';
$lang['Date'] =           'Date';
$lang['Title'] =          'Title';
$lang['Image_size'] =     'Image Size';
$lang['Description'] =    'Description';
$lang['Delivery_cost'] =  'Delivery Cost';
$lang['Summary_cost'] =   'Total';
$lang['Language'] =       'Language';
$lang['Ip'] =             'IP';
$lang['Visible'] =        'Visible';
$lang['Position'] =       'Position';
$lang['Status'] =         'Status';
$lang['Start_page'] =     'Start Page';
$lang['Currency_symbol'] ='Currency Symbol';
$lang['Mail_informing'] = 'E-mail new orders to Admin';
$lang['Comment_order'] =  'Comment on Order';
$lang['ParentCategory'] = 'Parent category'; 
$lang['content'] = 'content';

$lang['Products_on_page'] =   'Products per Page';
$lang['Admin_items_on_page'] ='Products/orders per Page in Administration';
$lang['Keywords'] =           'Keywords';

$lang['Products_image_size'] =    'Product Image Size';
$lang['Categories_image_size'] =  'Category Image Size';
$lang['Content_image_size'] =     'Content Image Size';

$lang['Short_description'] =  'Short Description';
$lang['Full_description'] =   'Full Description';

$lang['Your_email'] =   'Your E-mail';
$lang['Topic'] =        'Topic';
$lang['Content_mail'] = 'Content';
$lang['send'] =         'Order';

$lang['Logo']     = 'Logo';
$lang['Template'] = 'Template';

$lang['Type'] = 'Type';

$lang['Contact_page'] = 'Contact Page';

/*
* Actions
*/
$lang['All'] =            'All';
$lang['choose'] =         'Choose';
$lang['Delete'] =         'Delete';
$lang['Next'] =           'Buy Item';
$lang['Calc'] =           'Update';
$lang['Add_to_basket'] =  'Add to basket';
$lang['back'] =           'back';
$lang['log_in'] =         'sign in';
$lang['logout'] =         'log out';
$lang['delete'] =         'delete';
$lang['save'] =           'save';
$lang['go_back'] =        'go back';
$lang['edit'] =           'edit';
$lang['Add_product'] =    'Add product';
$lang['Add_category'] =   'Add category';
$lang['Add_courier'] =    'Add courier';
$lang['more'] =           'more';
$lang['Search'] =         'Search';
$lang['print'] =          'print';
$lang['Buy_now'] =        'Buy now!';

/*
* Menu, name of pages
*/
$lang['basket'] =         'basket';
$lang['products'] =       'products';
$lang['Homepage'] =       'Homepage';
$lang['homepage'] =       'homepage';
$lang['Categories'] =     'Categories';
$lang['categories'] =     'categories';
$lang['couriers'] =       'couriers';
$lang['orders'] =         'orders';
$lang['configuration'] =  'configuration';
$lang['tools'] =          'tools';
$lang['Validate'] =       'Validate';

$lang['Category_data'] =  'Category data';
$lang['Category_list'] =  'Category list';

$lang['Courier_data'] =   'Courier data';
$lang['Courier_list'] =   'Courier list';

$lang['Orders_list'] =    'Order list';
$lang['Order_id'] =       'Order id';

$lang['Config_page'] =    'Configuration';
$lang['Page_data'] =      'Page data';

$lang['Product_data'] =   'Products data';
$lang['Products_list'] =  'Products list';

/*
* Other
*/
$lang['delivery_addres'] ='Delivery address';
$lang['Pages'] =          'Pages';

$lang['Log_in_administration'] ='Login administration';
$lang['version'] =              'version';
$lang['administration'] =       'administration';

$lang['Pending'] =      'Pending';
$lang['Processing'] =   'Processing';
$lang['Finished'] =     'Finished';
$lang['Canceled'] =     'Canceled';

$lang['Pendings'] =     'Pendings';

$lang['big'] =          'big';
$lang['small'] =        'small';
$lang['noSubcategory']= 'Main category';

$lang['mail_title'] = 'New Order';
$lang['mail_txt'] =   'You have received an online order. --  Login to the Administration Panel for details. Click here -- [URL unfurl="true"]http://www.theurl.com/cart/admin.php';[/URL]
/*
* HtmlEditor messages 
*/
$lang['Enter_text_to_be_formatted'] = "Enter text to be formatted";
$lang['Enter_list_type'] =            "Type '1' for numeric list, type 'a' for alphanumeric list, or leave empty for point list";
$lang['Enter_list_item'] =            'Type list elements.\nLeave empty or click \'Cancel\' to close list:';
$lang['Enter_link_url'] =             'Type URL address for link:';
$lang['Enter_email_link'] =           'Type email address for link:';
$lang['Bold'] =           'Bold';
$lang['Italic'] =         'Italic';
$lang['Underline'] =      'Underline';
$lang['Align_left'] =     'Align Left';
$lang['Align_center'] =   'Align Center';
$lang['Align_right'] =    'Align Right';
$lang['Add_li'] =         'Add List';
$lang['Add_hyperlink'] =  'Add Hyperlink';
$lang['Add_email_link'] = 'Add Email Link';
$lang['New_line'] =       'New Line';
$lang['Close_all'] =      'Close All Tags';
$lang['Size_plus'] =      'Enlarge Size';
$lang['Size_minus'] =     'Reduce Size';

/*
* Validator messages
*/
$lang['msg_more_info'] =              'More info';
$lang['msg_php_version'] =            'PHP version';
$lang['msg_php_version_info'] =       'Your PHP version is too old. You need at least PHP version 4.3.3 on your server';
$lang['msg_files_permission'] =       'Files permission';
$lang['msg_files_permission_info'] =  'You need to set every file in the "db/" directory to chmod 0777';
$lang['msg_config_permission'] =      'Confguration file permission';
$lang['msg_config_permission_info'] = 'If you want to change your config settings, you need to set chmod 0777 for the "config/general.php" file';
$lang['msg_login'] =                  'Login and password';
$lang['msg_login_info'] =             'Now you have to change your password; go to "configuration" in the Administration zone';
$lang['msg_gd2'] =                    'GD2.0 library';
$lang['msg_gd2_info'] =               'For correct thumbnails generation you need the gd2.0 library. Contact your Administrator to enable it';
$lang['msg_dirs_permission'] =        'Directories permission';
$lang['msg_dirs_permission_info'] =   'You need to set every subdirectory in the "files/" directory to chmod 0777';

/*
* Dont change below 
*/
define( 'LANG_YES_SHORT', $lang['operation_yes'] );
define( 'LANG_NO_SHORT', $lang['operation_no'] );
?>
 

Maybe I'm wrong, but you may have to edit something in the real PHP code instead of just the config file.
 
i still think you're trying to send it as html.

where is the actual code that sends the email?
 
I'm a little puzzled about the inconsistent use of \" and ".

That probably has nothing to do with the actual problem tho.
 
Looks like this is where it all happens - my guess is the js code match() that stips the html and the line breaks etc is what does it -- is there a possible workaround? -- without killing the function of this. Its part of the return details function of the shopping cart.

Code:
<?php
require_once DIR_PLUGINS.'htmlMailOrderDetails/phpmailer/class.phpmailer.php';

if(!class_exists('qcMailer')){
  class qcMailer extends PHPMailer{
    var $bSendToAdmin = false;
    var $bSendToClient = false;
    var $bResendToAdmin = false; // temporary override
    var $bResendToClient = false; // temporary override
    var $sShopFolder; // with trailing slash
    var $sTemplate;
    var $sSubjectAdmin;
    var $sSubjectClient;
    var $sMailBody;
    var $sMailAltBody;
    var $sAdministrator;
    var $sTo;
    var $sToName;
    var $iInterceptID;
    var $bAdmin = false; // running from Admin?
    var $bEchoErrors = false;
    var $bShowAndStop = false;
    var $bHtmlAdmin = false;
    var $bTextAdmin = false;
    var $bHtmlClient = false;
    var $bTextClient = false;

    /* Constructor
    * @return object qcMailer
    */
    function qcMailer(){
      global $config, $lang;
      $this->bAdmin = (basename($_SERVER['PHP_SELF']) == 'admin.php');
      $this->sShopFolder = dirname( ( (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']!='') ? 'https' : 'http' ) . "://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}" ) . '/';
      $this->bSendToAdmin = (isset($config['hmod']['sendAdmin']) && $config['hmod']['sendAdmin'] === true);
      $this->bSendToClient = (isset($config['hmod']['sendClient']) && $config['hmod']['sendClient'] === true);
      // make sure the subjects have no new lines or carriage returns ...
      $this->sSubjectClient = preg_replace(array("/[\n\r]/"), array(" "), $lang['hmodSubjectClient']);
      $this->sSubjectAdmin = preg_replace(array("/[\n\r]/"), array(" "), $lang['hmodSubjectAdmin']);
      $this->sMailBody = $this->sMailAltBody = '';
      $this->sTemplate = 'htmlMailOrderDetails.tpl';
      $this->sTo = '';
      $this->sAdministrator = $config['hmod']['adminName'];
      $this->iInterceptID = null;
      $this->bResendToAdmin = $this->bResendToClient = false;
      $this->bEchoErrors = (isset($config['hmod']['reportErrors']) && $config['hmod']['reportErrors'] === true) ? true : false;
      $this->bShowAndStop = (isset($config['hmod']['showAndStop']) && $config['hmod']['showAndStop'] === true) ? true : false;

      if (!isset($config['shop_root'])) $config['shop_root'] = $this->sShopFolder;

      $this->CharSet = $config['charset'];
      $this->SetLanguage($config['language'],DIR_PLUGINS.'htmlMailOrderDetails/phpmailer/language/');
      switch($config['hmod']['method']){
        case 'sendmail':
          $this->IsSendmail(); if($config['hmod']['sendmailPath']!='') $this->Sendmail = $config['hmod']['sendmailPath']; break;
        case 'qmail':
          $this->IsQmail(); if($config['hmod']['qmailPath']!='') $this->Sendmail = $config['hmod']['qmailPath']; break;
        case 'smtp':
          $this->IsSMTP();
          $this->Host = $config['hmod']['smtpHosts'];
          if($config['hmod']['smtpPort'] > 0) $this->Port = $config['hmod']['smtpPort'];
          $this->SMTPAuth = $config['hmod']['smtpAuth'];
          if($this->SMTPAuth){ $this->Username = $config['hmod']['smtpUser']; $this->Password = $config['hmod']['smtpPword']; }
        case 'mail':
        default:
          $this->IsMail();
      }
      $this->bHtmlAdmin = in_array($config['hmod']['formatAdmin'], array('html','both'));
      $this->bTextAdmin = in_array($config['hmod']['formatAdmin'], array('text','both'));
      $this->bHtmlClient = in_array($config['hmod']['formatClient'], array('html','both'));
      $this->bTextClient = in_array($config['hmod']['formatClient'], array('text','both'));
      // if config says we can send neither HTML nor Text then we can't send anything! ...
      if(!$this->bHtmlAdmin && !$this->bTextAdmin){ $this->bSendToAdmin = false; }
      if(!$this->bHtmlClient && !$this->bTextClient){ $this->bSendToClient = false; }
      $this->WordWrap = 70;
      $this->From = EMAIL;
      $this->FromName = $GLOBALS['config']['title'];
      $this->AddReplyTo(EMAIL);
    } // end function qcMailer

/********************/
/* PUBLIC FUNCTIONS */
/********************/
// GETTERS
    /* Gets the template
    * @return string
    */
    function getTemplate(){
      return $this->sTemplate;
    } // end function getTemplate

    /* Returns false if the intercept has not YET been set (at all)
    * @return boolean
    */
    function isSetIntercept(){
      return is_null($this->iInterceptID) ? false : true;
    }

    /* Gets the current intercept for access via $tpl; returns false if not yet set
    * @return integer
    */
    function getInterceptID(){
      return is_null($this->iInterceptID) ? false : $this->iInterceptID;
    }

    /* Gets the shop URL
    * @return string
    */
    function getShopURL(){
      return $this->sShopFolder;
    }

// SETTERS
    /* Sets the email address of the client
    * @param integer order number
    * @return void
    */
    function setSendTo($sTo = '', $sToName = '') {
      $this->sTo = $sTo;
      $this->sToName = $sToName;
    } // end function setSendTo

    /* Sets the body of the order
    * @param integer order number
    * @return void
    */
    function setMailBody($sContent = '') {
      $this->sMailBody = $sContent;
    } // end function setMailBody

    /* Sets the current intercept
    * @param integer
    * @return void
    */
    function setInterceptID($intercept){
      $this->iInterceptID = $intercept;
    }

    /* Sets error reporting flag
    * @param boolean reportErrors
    */
    function setErrorReporting($reportErrors=true){
      $this->bEchoErrors = is_bool($reportErrors) ? $reportErrors : false;
    }

    /* Send Email
    * @return void
    */
    function Send(){
      if(($this->bSendToClient || $this->bResendToClient || $this->bSendToAdmin || $this->bResendToAdmin) && $this->sMailBody != ''){
        if($this->bTextAdmin || $this->bTextClient){ $this->_formatAsText(); } // we need a text-only version
        if($this->bShowAndStop){
          if($this->bHtmlAdmin || $this->bHtmlClient){ echo $this->sMailBody; }
          if($this->bTextAdmin || $this->bTextClient){ echo $this->sMailAltBody; }
          exit();
        }
      }
      // Only send to client/customer if there is something to send and somewhere to send it to ...
      if(($this->bSendToClient || $this->bResendToClient) && $this->sMailBody != '' && $this->sTo != ''){
        $this->IsHTML($this->bHtmlClient);
        $this->Body = $this->bHtmlClient ? $this->sMailBody : $this->sMailAltBody;
        $this->AltBody = ($this->bHtmlClient && $this->bTextClient) ? $this->sMailAltBody : '';
        $this->Subject = $this->sSubjectClient;
        $this->AddAddress($this->sTo, $this->sToName);
        if(!parent::Send()){
          if($this->bEchoErrors){ echo $this->ErrorInfo; }
        }
        $this->ClearAddresses();
      }
      // Only send to admin if there is something to send ...
      if(($this->bSendToAdmin || $this->bResendToAdmin) && $this->sMailBody != ''){
        $this->IsHTML($this->bHtmlAdmin);
        $this->Body = $this->bHtmlAdmin ? $this->sMailBody : $this->sMailAltBody;
        $this->AltBody = ($this->bHtmlAdmin && $this->bTextAdmin) ? $this->sMailAltBody : '';
        $this->Subject = $this->sSubjectAdmin;
        $this->AddAddress(EMAIL, $this->sAdministrator);
        if(!parent::Send()){
          if($this->bEchoErrors){ echo $this->ErrorInfo; }
        }
      }
      $this->setMailBody(); $this->setSendTo(); $this->sMailAltBody = ''; // make sure we can't send previous data
    } // end function Send

    /* (Re)sends emails
    * @param integer order number
    * @param boolean toClient
    * @param boolean toAdmin
    * @return void
    */
    function reSend($order, $toClient=null, $toAdmin=null){
      $this->bResendToAdmin = is_null($toAdmin) ? $this->bSendToAdmin : (is_bool($toAdmin) ? $toAdmin : false);
      $this->bResendToClient = is_null($toClient) ? $this->bSendToClient : (is_bool($toClient) ? $toClient : false);
      // if neither HTML nor Text formats are enabled, can't send ...
      if(!$this->bHtmlAdmin && !$this->bTextAdmin){ $this->bResendToAdmin = false; }
      if(!$this->bHtmlClient && !$this->bTextClient){ $this->bResendToClient = false; }
      // are we resending to someone? ...
      if($this->bResendToAdmin || $this->bResendToClient){
/* Not available yet!
        resendHtmlMailDetails($order);
        $this->Send();
*/
        $this->bResendToAdmin = $this->bResendToClient = false;
      }
    } // end function reSend
    
    /* Format an HTML page as text-only
    */
    function _formatAsText(){
      // chop everything above BODY tag ...
      $this->sMailAltBody = (($pos = strpos(strtolower($this->sMailBody),'<body')) !== false) ? substr($this->sMailBody, $pos) : $this->sMailBody;
      // Strip out javascript ...
      $this->sMailAltBody = preg_replace('@<script[^>]*?>.*?</script>@si', '', $this->sMailAltBody);
      // convert TRs to one line, ie. containing no newlines or carriage returns ...
      $this->sMailAltBody = preg_replace_callback( '@<tr.*\/tr@isU'
                                                 , create_function( '$match'
                                                                  , 'return preg_replace("/[\r\n]/", "", $match[0]);'
                                                                  )
                                                 , $this->sMailAltBody
                                                 );
      $search = array( '@<[\/\!]*?[^<>]*?>@si'          // Strip out HTML tags
                     , '@([\r\n])[\s]+@'                // Strip out white space
                     , '@&(quot|#34);@i'                // Replace HTML entities ...
                     , '@&(amp|#38);@i'
                     , '@&(lt|#60);@i'
                     , '@&(gt|#62);@i'
                     , '@&(nbsp|#160);@i'
                     , '@&#36;@i'
                     , '@&(iexcl|#161);@i'
                     , '@&(cent|#162);@i'
                     , '@&(pound|#163);@i'
                     , '@&(copy|#169);@i'
                     );
      $replace = array( ''
                      , '\1'
                      , '"'
                      , '&'
                      , '<'
                      , '>'
                      , ' '
                      , '$'
                      , chr(161)
                      , chr(162)
                      , chr(163)
                      , chr(169)
                      );
      $this->sMailAltBody = preg_replace($search, $replace, $this->sMailAltBody);
      // clear multiple spaces, except where contained within a set of double quotes ...
      $this->sMailAltBody = preg_replace_callback( '@([^"]*)("[^"]*")?@'
                                                 , create_function( '$match'
                                                                  , 'return preg_replace("/[ ]{2,}/", " ", $match[1]).(isset($match[2])?$match[2]:"");'
                                                                  )
                                                 , $this->sMailAltBody
                                                 );
    } // end function _formatAsText

  } // end class qcMailer
}
?>
 
This is how I do it in my mail functions...
Code:
$Body = "Title: ".$Title." \n";
		$Body .= "Name: ".$Name;
		$Body .= " ".$LastName." \n";
		$Body .= "Email: ".$Email." \n";
		$Body .= "Address: ".$Address." \n";
		$Body .= "City: ".$City." \n";
		$Body .= "State: ".$State." \n";
		$Body .= "Country: ".$Country." \n";
		$Body .= "Zip: ".$Zip." \n";
		$Body .= "Phone: ".$Phone." \n";
		$Body .= "Best Time To Call: ".$TTC." \n";
		$Body .= "Alt Phone: ".$AltPhone." \n";
		$Body .= "Email: ".$Email ."\n";
		$Body .= "Category: ".$Category ."\n";
		$Body .= "Capital To Invest: ".$LiqCapChk ."\n";
 
Oh yea I get that, I use a similar thing in my other forms too, with a little more additions such as /n------------------/n -- This note is from • .$Name; .$LastName./n -- of; .$Address. etc, etc

however, with this particular canned php cart the functions are coded by the developer (and they dont seem to respond well to questions - its a good cart too) the only option I have is to somehow rework this ---- eee!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top