Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP Mailer Formatting Issues...

    Ok, I will try to put this as simply as possible:

    I have a mailer script I got from a website somewhere (I don't recall where exactly) that is successfully sending emails to a specific email account, the content of which is given from an HTML feedback form on my server.

    Here is the script:

    PHP Code:
    <?php

    $To 
    'webtester800@mywebserver.com';         
    $SmtpServer 'smtp.mywebserver.com';  
    $SmtpUserid 'webtester800@mywebserver.com';        
    $SmtpPassword '******';
    $NextPage 'fbgood.html';            
    $Debug 0;              


    function 
    ReceiveOnSocket($Socket,$Ok,$Debug) {
      
    $Line '';
      do {
        
    $Result fgets($Socket,10000);
        if (
    $Result == FALSE) return('Error reading from socket');
        
    $Line $Line $Result;
        if (
    $Debug 0) echo '<<< ' htmlentities(trim($Result)) . "\n";
        } while (
    $Result[3] == '-');
      if (
    intval($Line) != $Ok) return($Line);
      return(
    '');
      }


    function 
    SendOnSocket($Socket,$Message,$Debug) {
      if (
    $Debug 0) echo '>>> ' htmlentities(trim($Message)) . "\n";
      if (
    fputs($Socket,$Message) == FALSE) return('Error sending: ' $Message);
      return(
    '');
      }


    function 
    SendEmail(
        
    $SmtpServer,      
        
    $SmtpPort,      
        
    $Userid,         
        
    $Password,    
        
    $To,              
        
    $From,            
        
    $ReplyTo,        
        
    $Subject,        
        
    $Body,           
        
    $Debug) {        


      if (
    $SmtpServer == '') return('No SmtpServer specified');
      if (
    $SmtpPort == '') return('No SmtpPort specified');
      if (
    $To == '') return('No TO address specified');
      if (
    $From == '') return('No FROM address specified');
      if (
    $ReplyTo == '') return('No REPLYTO address specified');
      if (
    $Subject == '') return('No SUBJECT specified');
      if (
    $Body == '') return('No BODY specified');

     
      
    $Socket fsockopen($SmtpServer,$SmtpPort,&$errno,&$errstr);
      if (
    $Socket == FALSE) return('Cannot connect to mailserver');


      
    $Status '';
      if (
    $Status == ''$Status ReceiveOnSocket($Socket,220,$Debug);
      if (
    $Status == ''$Status SendOnSocket($Socket,'HELO ' $SmtpServer "\r\n",$Debug);
      if (
    $Status == ''$Status ReceiveOnSocket($Socket,250,$Debug);

      if ((
    $Userid != '') && ($Password != '')) {
        if (
    $Status == ''$Status SendOnSocket($Socket,"AUTH LOGIN\r\n",$Debug);
        if (
    $Status == ''$Status ReceiveOnSocket($Socket,334,$Debug);
        if (
    $Status == ''$Status SendOnSocket($Socket,base64_encode($Userid) . "\r\n",$Debug);
        if (
    $Status == ''$Status ReceiveOnSocket($Socket,334,$Debug);
        if (
    $Status == ''$Status SendOnSocket($Socket,base64_encode($Password) . "\r\n",$Debug);
        if (
    $Status == ''$Status ReceiveOnSocket($Socket,235,$Debug);
        }

      if (
    $Status == ''$Status SendOnSocket($Socket,'MAIL FROM:<' $From '>' "\r\n",$Debug);
      if (
    $Status == ''$Status ReceiveOnSocket($Socket,250,$Debug);
      
    $a explode(',',$To);
      for (
    $i 0$i count($a); $i++) {
        if (
    trim($a[$i]) == '') continue;
        if (
    $Status == ''$Status SendOnSocket($Socket,'RCPT TO:<' trim($a[$i]) . '>' "\r\n",$Debug);
        if (
    $Status == ''$Status ReceiveOnSocket($Socket,250,$Debug);
        }
      if (
    $Status == ''$Status SendOnSocket($Socket,"DATA\r\n",$Debug);
      if (
    $Status == ''$Status ReceiveOnSocket($Socket,354,$Debug);
      if (
    $Status == ''$Status SendOnSocket($Socket,
        
    'To: ' $To "\r\n" .
        
    'From: ' $From "\r\n" .
        
    'Reply-To: ' $ReplyTo "\r\n" .
        
    'Subject: ' $Subject "\r\n" .
        
    'Date: ' date('D, d M Y H:i:s ') . "\r\n" .
        
    "\r\n" .
        
    trim($Body) .
        
    "\r\n.\r\n",$Debug);
      if (
    $Status == ''$Status ReceiveOnSocket($Socket,250,$Debug);
      if (
    $Status == ''$Status SendOnSocket($Socket,"QUIT\r\n",$Debug);
      if (
    $Status == ''$Status ReceiveOnSocket($Socket,221,$Debug);

     
      if (
    $Socketfclose($Socket);

     
      return (
    $Status);
      }



    if (isset(
    $HTTP_GET_VARS)) {
      while (
    $a each($HTTP_GET_VARS)) {
        if (
    $a[0] == 'From') continue;
        if (
    $a[0] == 'Subject') continue;
        if (
    $a[0] == 'Submit') continue;
       
    $Body .= $a[0] . "\t= " $a[1] . "\r\n";
        }
      }
    if (isset(
    $HTTP_POST_VARS)) {
      while (
    $a each($HTTP_POST_VARS)) {
        if (
    $a[0] == 'From') continue;
        if (
    $a[0] == 'Subject') continue;
        if (
    $a[0] == 'Submit') continue;
       
    $Body .= $a[0] . "\t= " $a[1] . "\r\n";
        }
      }


    $Subject ereg_replace("[^-[:alnum:]_.!@=* ]","",$Subject);
    $From ereg_replace("[^-[:alnum:]_.!@=* ]","",$From);



    if (
    $Debug != 0) echo '<pre>';
    $Status SendEmail($SmtpServer,25,$SmtpUserid,$SmtpPassword,
      
    $To,$From,$From,$Subject,$Body,$Debug);
    if (
    $Debug != 0) echo '</pre>';
    if (
    $Status != '') {
      if (
    $Debug == 0) echo '<html><body>' "\n";
      echo 
    '<b>An error has occurred while sending the email.' "\n";
      echo 
    'The status message is:</b>' "\n";
      echo 
    '<p>' "\n";
      echo  
    htmlentities($Status,ENT_QUOTES) . "\n";
      echo 
    '</body></html>' "\n";
      exit;
      }



    if (
    $Debug == 0header('location: ' $NextPage);
    ?>
    Now, my issue is that the emails that this script is sending has the content formatted something like this:

    Great websiteName = John
    Body = Great website
    Submit_x = 22
    Submit_y = 4
    Optimistically, I would like the emails to not have the "Name =","Body =","Submit =" etc., plus the message or body content repeats twice. Once at beginning and once after "Body ="...

    I am not very knowledgeable in PHP scripting, so I am not sure what part of the script is formatting the emails, and what needs to be changed to remedy the situation...?

    Thanks in advance for any help you guys can give me!!
    - Michael

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It looks like someone copy/pasted this bit of code twice on accident:

    PHP Code:
    if (isset($HTTP_GET_VARS)) {
      while (
    $a each($HTTP_GET_VARS)) {
        if (
    $a[0] == 'From') continue;
        if (
    $a[0] == 'Subject') continue;
        if (
    $a[0] == 'Submit') continue;
       
    $Body .= $a[0] . "\t= " $a[1] . "\r\n";
        }
      }
    if (isset(
    $HTTP_POST_VARS)) {
      while (
    $a each($HTTP_POST_VARS)) {
        if (
    $a[0] == 'From') continue;
        if (
    $a[0] == 'Subject') continue;
        if (
    $a[0] == 'Submit') continue;
       
    $Body .= $a[0] . "\t= " $a[1] . "\r\n";
        }
      } 
    All you need to do is remove all that code and replace it with
    PHP Code:
    $Body $_POST['Body']; 
    (Assuming your form's element is named Body as is implied but not shown)

  • #3
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, Fumigator.

    However, I need to make sure the "Name" string is in the body of the email message as well (The name is "John" in the example from my previous post).

    For example, with the code you gave me, I just get an email that says "Great website" and nothing else.

    What do I need to add in order to get it looking like "John, Great website" (this comes from the Name field in the form which has the same string name in the PHP script respectively) or "John," and then "Great website" on the next line ?

    In other words, how do I get the input from the Name field and Body fields of the feedback form to both go into the message body of the email being sent?

    - Michael
    Last edited by moktoman; 06-29-2007 at 12:01 PM. Reason: Clarification purposes

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I just noticed the code I thought was duplicate code is actually checking both the POST and GET arrays so it's not duplicated, just flexible. Ooops...

    Anyways, you can build the body of the email however you want it to look by using simple variable assignments. $Body is the name of the variable that ends up in the body of your email. The form input comes into the script via the $_POST or $_GET arrays, using the names of the form elements. So if you have a form element <input type="text" name="fuzzydice"> and submit the form with action="post", then the $_POST array will contain an index "fuzzydice": $_POST['fuzzydice'].

    To "glue" strings together, use the period, like this: $Body = 'hi' . ' ' . 'there'; You can also imbed a variable within a string, like this (you must use double-quotes when imbedding): $Body = "You entered {$_POST['Name']} as the name.";

  • #5
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No big deal.
    That original code wasn't working right anyway, so I'm glad to be rid of it.

    I'll go ahead and try your suggestions, and see if I can't get it looking nice and proper...

    Thanks again.
    -Michael

  • #6
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, one last thing.

    The code looks like this now:

    PHP Code:
    $Body "Name = {$_POST['Name']} Message = {$_POST['Body']}"
    Of course, this works, but then everything is all displayed on one line in the resulting email.

    Is there a way to tell the script to add a line break in the email before the "Message =" portion?

    - Michael
    Last edited by moktoman; 06-30-2007 at 02:00 AM. Reason: Typo


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •