Web Analytics Made Easy -
StatCounter Sending EMail using PHP - CodingForum

Announcement

Collapse
No announcement yet.

Sending EMail using PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Sending EMail using PHP

    I have a form that gets completed and then I want to email the results of the form to different people. The person completing the form will choose who they want to send the results to. I have a dropdown box, where the list of names used is obtained from the database.

    Is there a way, that when a person hilites multiple names that the email can be sent to these people.

    Here is what I have for the dropdown box.
    Code:
    <div>
                                            <center><B>Send this form to:</B><BR>
                                            <select name='tech' multiple>
    
                                            <?php
    
        $result = mysql_query("SELECT * FROM employees") or die(mysql_error());
        while ($row = mysql_fetch_assoc($result)) {
            echo "<option>$row[name]</option>";
        }
    Once you have selected who the email goes to, and completed the form, I want to create a query that will send the email to these people when you press the submit button for the rest of the form. The data is being saved in a database, but I don't have it set up to save the names of the people receiving an email (although I could if necessary.)

    Thanks for any help as this has me totally stumped.

    Ken

  • #2
    ....give the options a value (I am assuming a DB field called `id`)
    PHP Code:
    <?php
    echo "<option value=\"sendto[{$row['id']}]\">{$row['name']}</option>";
    ?>
    then in the receiving script ...

    PHP Code:
    <?php
    $subject
    ='blah';
    $headers='FROM:[email protected]';
    $content="Hello {NAME}, etc";

    foreach(
    $_POST['sendto'] as $id){
     
    $q=mysql_query("SELECT `email`, `name` FROM employees WHERE `id`=$id");
     
    $r=mysql_fetch_assoc($q);
     
    $content str_replace('{NAME}'$r['name'], $content);
     
    mail($r['email'], $subject$content$headers);
    }
    ?>
    OK thats not exactly how I would do it but it gives the general idea.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

    Comment


    • #3
      If you allow multiple selections in a dropdown list (as your code does allow) then the selected entries are passed to the server as an array. All you need to do is to convert that array into a comman separated list for use as to address in order for it to be sent to all selected recipients.


      PHP Code:
      <?php
      $subject
      ='blah';
      $headers='FROM:[email protected]';
      $content="Hello {NAME}, etc";
      $to implode(","$_POST['tech']);
       
      mail($to$subject$content$headers);
      Stephen
      Learn Modern JavaScript - http://javascriptexample.net/
      Helping others to solve their computer problem at http://www.felgall.com/

      Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

      Comment

      Working...
      X