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
    May 2017
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP Email Form Help

    I have a PHP email form that I have sending information from form fields to multiple email addresses. However, the trouble I'm having is figuring out a way to exclude one line of information from the emails all addresses receive EXCEPT for one.

    Basically, a visitor to the page fills out the information and clicks "submit." An email of all that information gets sent to all the email addresses in my array, except I want to exclude one of those form fields from all the email addresses except one. Find my code below:

    PHP Code:
    <?php
    if(isset($_POST['email'])) {
     
        
    $email_to "[email protected],[email protected],[email protected]";
        
    $email_subject "Your email subject line";
     
        function 
    died($error) {
            echo 
    "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo 
    "These errors appear below.<br /><br />";
            echo 
    $error."<br /><br />";
            echo 
    "Please go back and fix these errors.<br /><br />";
            die();
        }
     
        if(!isset(
    $_POST['first_name']) ||
            !isset(
    $_POST['last_name']) ||
            !isset(
    $_POST['business_name']) ||
            !isset(
    $_POST['email']) ||
            !isset(
    $_POST['telephone']) ||
            !isset(
    $_POST['comments'])) {
            
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
        }
     
         
     
        
    $first_name $_POST['first_name']; // required
        
    $last_name $_POST['last_name']; // required
        
    $business_name $_POST['business_name']; // required
        
    $email_from $_POST['email']; // required
        
    $telephone $_POST['telephone']; // not required
        
    $comments $_POST['comments']; // required
     
        
    $error_message "";
        
    $email_exp '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
     
      if(!
    preg_match($email_exp,$email_from)) {
        
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
      }
     
        
    $string_exp "/^[A-Za-z .'-]+$/";
     
      if(!
    preg_match($string_exp,$first_name)) {
        
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
      }
     
      if(!
    preg_match($string_exp,$last_name)) {
        
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
     
      if(
    strlen($comments) < 2) {
        
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
      }
     
      if(
    strlen($error_message) > 0) {
        
    died($error_message);
      }
     
        
    $email_message "Form details below.\n\n";
     
         
        function 
    clean_string($string) {
          
    $bad = array("content-type","bcc:","to:","cc:","href");
          return 
    str_replace($bad,"",$string);
        }
     
         
     
        
    $email_message .= "First Name: ".clean_string($first_name)."\n";
        
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
        
    $email_message .= "Business Name: ".clean_string($business_name). "\n";
        
    $email_message .= "Email: ".clean_string($email_from)."\n";
        
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
        
    $email_message .= "Comments: ".clean_string($comments)."\n";
     
    $headers 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' phpversion();
    @
    mail($email_to$email_subject$email_message$headers);  
    ?>
     
    <!-- include your own success html here -->
     
    Thank you for contacting us. We will be in touch with you very soon.

     
    <?php
     
    }
    ?>

    I'm not by any means an advanced coder; I'm more so building this feature on my website. So it would be tremendously helpful to know WHERE I put whatever code remedies my hang up, as others have sent me snippets of code and given me no direction as to where the code should go.

    Thank you so much!
    Last edited by vinyl-junkie; May 7th, 2017 at 12:28 AM. Reason: added code tags

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,378
    Thanks
    10
    Thanked 1,190 Times in 1,180 Posts
    In your example, you are sending one email "body" to several email addresses (all entered on the "to" line).
    There is no way to selectively change one line for one of the emails.

    So you'll have to create an array of email addresses and then send each one separately in a loop. Then you can selectively manipulate the "body" of the email.
    It might also be a better way anyhow, since each person getting the email won't be able to see which other people have gotten it.

    As you have it now, if I get an email from you, I can see everyone else's email address.

    Also, your webhost may or may not allow you to use any email address in the "from" part of the email.
    They may require you to use your own domain name as the "from". You know the email is coming from your website, so it won't matter who the "from" is.

    So to answer your question, it would have to be done another way.

    Answer some more things for us:
    1) How many email addresses will there be for each "mass" mailing? Webhosts have limits on how many per hour, day, etc.
    2) Is the contact form filled out by random people? Do the people have to login to send the contact form?
    3) Do you really want a bunch of people to get an email each time the form is filled out and submitted? Are they expecting the emails to arrive?
    4) Would it better to have the contact form put into a database and your "list of email address" people could view the data online instead of getting emails?

    .... I will elaborate more on item 3 ... Let's say 10 people get the email form sent to them and I am one of those people. I open the email and read it. How do I know how many others have seen it? Do you expect me to "reply to all" with a response? What if 3 others read it, but nobody else did? What if nobody reads it?

    See what I mean? Once that contact form is filled-out and submitted, you've lost control over who actually reads it. Or, if someone reads it, what are they supposed to do in order to let everyone else know they read it? With 10 people getting the email, I could end-up with 50 emails in replies and conversation because of 1 contact form.

    In summary, I'm thinking you might be making a "help desk". A customer asks a question and you want anyone in your small company to provide an answer. If this is the case, you would want to use a PHP script with a database. The form is given a "ticket number" and your employees open the same ticket number and can reply and view everyone else's input.
    Last edited by mlseim; May 7th, 2017 at 02:44 PM.

  3. #3
    New to the CF scene
    Join Date
    May 2017
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for the suggestions and the help. Let me answer your questions below:

    1) Not many, but my business is growing. I wasn't aware there was a limit on how many emails can be sent from a contact form. The email addresses are my employees.

    2) The contact form is filled out by any customer that comes to us, so essentially I need it to be sent to all employees with one line redacted from the input fields that only myself and my partner need. Perhaps a "help desk" type process would make sense; however, I need my employees to receive the information ASAP, as they are on the go and won't likely check as frequently as you'd think. Having the customer details sent directly to them is best for my company.

    3) In short, yes. They are expecting the emails.

    4) Databases sound cleaner; however, my employees will not necessarily check as frequently as if it were to arrive at their phone via text message.

    Basically, I just need any customer that goes to our contact form, fills out what they need from us and clicks send, that information sans one line that only I need to be sent to my employees immediately. I did more research, and an array with loops seems to work, but I can't find any leg-up snippet of code nor where I'd put that in my code. Any help with that?

    But again, if there is some way to do this simply and more efficiently, that'd be fantastic. Thank you!

  4. #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    4,264
    Thanks
    3
    Thanked 554 Times in 539 Posts
    Using the help desk approach, you would still send emails to employees to let them know that a new inquiry has been submitted, with just a brief summery in the email message, with the same message being sent to everyone, but they would view and manage any responses through a web interface, as mlseim has indicated, to avoid duplicating work and sending multiple replies to the person submitting the form.

    The web interface would display whatever customer information you want, based on who the logged in user is.

    This would also have the advantage that email replies sent back and forth with the customer would go through a general email address for your company, rather than various employee email addresses.

    Regardless of how and to who you send emails to, you should record the form submission in a database, so that you will even know that a submission has taken palace. Email delivery is not reliable. Your sending mail server could be 'down' and during the course of fixing the problem with it, new emails won't get queued and existing emails may get cleared before they have been sent. Receiving mail servers can have similar problems, and you may never get notification that a submission has occurred. If the data is stored in a database, with the database also recording the instance of replies having been made, you can get regular, scheduled, reports of submissions that have not been replied to. In fact, the web interface could highlight submissions that have not received replies.

    Also, by storing the information in a database, you can detect duplicate submissions and can detect floods sent by spammers/bot scripts.
    Last edited by CFMaBiSmAd; May 8th, 2017 at 05:04 PM.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  5. Users who have thanked CFMaBiSmAd for this post:

    Bradl3y (May 9th, 2017)

  6. #5
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,378
    Thanks
    10
    Thanked 1,190 Times in 1,180 Posts
    I modified your script. You can enter email addresses and whether they are "admin" or not.
    Modify the lines/message for "admin" or "everyone else".

    The script
    PHP Code:
    <?php 
    if(isset($_POST['email'])) { 
      
      
    // Enter all email addresses here and specify if they are admin or not
      // Make sure last entry has no comma after it.
      
    $email_to = array(
      
    "[email protected]=> array("level" => ""),
      
    "[email protected]=> array("level" => "admin"),
      
    "[email protected]=> array("level" => ""),
      
    "[email protected]=> array("level" => "admin")
      );
      
        
    $email_subject "Your email subject line"
      
        function 
    died($error) { 
            echo 
    "We are very sorry, but there were error(s) found with the form you submitted. "
            echo 
    "These errors appear below.<br /><br />"
            echo 
    $error."<br /><br />"
            echo 
    "Please go back and fix these errors.<br /><br />"
            die(); 
        } 
        
        if(!isset(
    $_POST['first_name']) || 
            !isset(
    $_POST['last_name']) || 
            !isset(
    $_POST['business_name']) || 
            !isset(
    $_POST['email']) || 
            !isset(
    $_POST['telephone']) || 
            !isset(
    $_POST['comments'])) { 
            
    died('We are sorry, but there appears to be a problem with the form you submitted.');        
        } 
         
      
        
    $first_name $_POST['first_name']; // required 
        
    $last_name $_POST['last_name']; // required 
        
    $business_name $_POST['business_name']; // required 
        
    $email_from $_POST['email']; // required 
        
    $telephone $_POST['telephone']; // not required 
        
    $comments $_POST['comments']; // required 
      
        
    $error_message ""
        
    $email_exp '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'
      
      if(!
    preg_match($email_exp,$email_from)) { 
        
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'
      } 
      
        
    $string_exp "/^[A-Za-z .'-]+$/"
      
      if(!
    preg_match($string_exp,$first_name)) { 
        
    $error_message .= 'The First Name you entered does not appear to be valid.<br />'
      } 
      
      if(!
    preg_match($string_exp,$last_name)) { 
        
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />'
      } 
      
      if(
    strlen($comments) < 2) { 
        
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'
      } 
      
      if(
    strlen($error_message) > 0) { 
        
    died($error_message); 
      } 
      
        
    $email_message "Form details below.\n\n"
      
          
        function 
    clean_string($string) { 
          
    $bad = array("content-type","bcc:","to:","cc:","href"); 
          return 
    str_replace($bad,"",$string); 
        } 

    // Loop through array of email addresses and determine if they are "admin" or not ...    
    $keys array_keys($email_to);     
    for(
    $i 0$i count($email_to); $i++){
        
    $email_message=""// Clear email message before each recipient
        
    $recipient $keys[$i];
        foreach(
    $email_to[$keys[$i]] as $key => $value) {
            if(
    $key=="level"){
            
    $level=$value;
            }
        }
            if(
    $level=="admin"){
            
    // do admin stuff ... what they should see in their email ...
            
    $email_message .= "First Name: ".clean_string($first_name)."\n"
            
    $email_message .= "Last Name: ".clean_string($last_name)."\n"
            
    $email_message .= "Business Name: ".clean_string($business_name). "\n"
            
    $email_message .= "Email: ".clean_string($email_from)."\n"
            
    $email_message .= "Telephone: ".clean_string($telephone)."\n"
            
    $email_message .= "Comments: ".clean_string($comments)."\n"
            }
            else{
            
    // what everyone else will see in their emails ...
            
    $email_message .= "First Name: ".clean_string($first_name)."\n"
            
    $email_message .= "Last Name: ".clean_string($last_name)."\n"
            
    $email_message .= "Email: ".clean_string($email_from)."\n"
            }
            
    $headers 'From: '.$email_from."\r\n"
            
    'Reply-To: '.$email_from."\r\n" 
            
    'X-Mailer: PHP/' phpversion(); 
                if(
    mail($recipient$email_subject$email_message$headers)){
                
    // email sent
                

    }
      
    ?> 
      
    <!-- include your own success html here --> 
      
    Thank you for contacting us. We will be in touch with you very soon. 

      
    <?php 
      

    ?>
    Last edited by mlseim; May 8th, 2017 at 10:11 PM.

  7. Users who have thanked mlseim for this post:

    Bradl3y (May 9th, 2017)

  8. #6
    New to the CF scene
    Join Date
    May 2017
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you so much for the help everyone. We will be using the email method to start while I work to build a database to create a help desk instead. Couldn't have done it without you!


 

Tags for this Thread

Posting Permissions

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