SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question submit form to multiple emails from db

    I have a form for a client which sends to the $baseemail when submitted. The $baseemail is stored in an include file and is set to one address.

    The client now wants the form submitted to multiple email addresses which he has input or updated onto his database (these are emails of employees etc not customers).

    What I need to know is how to pull the email addresses from the DB into the submission process.

    Can anyone help please?

  2. #2
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    I've made a suggestion for you. It works the way that the email adresses won't be displayed at all.
    First it pulls the name and an id from the db to be displayed as checkboxes in the form. User selects whivh persons to send email to.
    After form is submitted the script will query the database again (getting email adresses) based on the the checkboxes that the user ticked.
    The script just send one email to all the adresses at one time.
    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    if(
    count($_POST) > && $_POST['submit'] == 'sendemail') {
        
    $msg = isset($_POST['msg']) ? $_POST['msg'] : '';
        
    $to = isset($_POST['to']) ? $_POST['to'] : '';
        if(!empty(
    $to) && is_array($to)) {
            
    $in '';
            foreach(
    $to as $id) {
                
    $in .= ", $id";
            }
            
    $in trim(substr($id1));
        }

        
    $sql "SELECT name, email FROM TABLE WHERE id IN ($in)";
        
    $result mysql_query($sql) or die(mysql_error());
        
    $to '';
        while(
    $row mysql_fetch_array($result)) {
            
    $name $row['name'];
            
    $email $row['email'];
            
    $to .= ", $name <$email>";
        }
        
    $to trim(substr($to1));
        
        if(
    mail($to'Subject'$msg)) {
            echo 
    'Mail has been successfully send.';
        } else {
            echo 
    'An error occured. The mail didn\'t get sent.';
        }
         
    } else {

        
    $sql "SELECT id, name FROM table";
        
    $result mysql_query($sql) or die(mysql_error());

        
    $checkboxes '';
        while(
    $row mysql_fetch_array($result)) {
            
    $id $row['id'];
            
    $name $row['nane'];
            
    $checkboxes .= "<input type=\"checkbox\" name=\"to[]\" id=\"to\" value=\"$id\" />$name";
        }
    ?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <label for="to">Sen email to:</label>
        <?php echo $checkboxes?>

        <label for="msg">Message:</label>
        <textarea name="msg" id="msg"></textarea>

        <button type="submit" name="submit" value="sendemail">Send email</button>
        </form>
    <?php
    }
    ?>
    The script can be shortened if you don't worry about the email adresses beeing displayed. Then you only need one database quesy aswell.

    Remeber to customize the sql/queries. I've not tested the code, so there might be some bugs.

    -Helge
    Last edited by Helge; Oct 17, 2003 at 03:57. Reason: Error in code

  3. #3
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks for th help, but the email addresses or recipients won't be selected by the person filling in the form which is why I have the problem really.

    This is a quote request form that potential clients complete and submit, but my client wants it to be sent to all the relevant employees on submission (this is not something that the person submitting would know about).

    Aby more advice?

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aahh...
    Can't you then just specify the recipients where the form get procceded. Do you need the email adresses in the form then?

    Maybe something like this
    PHP Code:
    if(count($_POST) > 0 && $_POST['submit'] == 'sendemail') {
        $msg = isset($_POST['msg']) ? $_POST['msg'] : '';

        $recipients = array('bob@firm.com', 'sarah@firm.com', 'webmaster@firm.com');
        $to = '';
        foreach($$recipients as $email) {
                $to .= ", $email";
        }
        $in = trim(substr($to, 1));
        
        if(mail($to, 'Subject', $msg)) {
            echo 'Mail has been successfully send.';
        } else {
            echo 'An error occured. The mail didn\'t get sent.';
        }

    } else {
    ?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <label for="msg">Feedback:</label>
        <textarea name="msg" id="msg"></textarea>

        <button type="submit" name="submit" value="sendemail">Send email</button>
        </form>
    <?php
    }
    -Helge

  5. #5
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    That would be the ideal soultuion, but again this is where the problem comes. The client will be adding and updating the emails recorded in the DB himself, this is why I need to be able to call them from there rather than just inputting them in an array as I won't know when he has made any alterations.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When will I start reading the posts more carefully.?
    Isn't it something about doing it the third time
    Try
    PHP Code:
    if(count($_POST) > 0 && $_POST['submit'] == 'sendemail') {
        $msg = isset($_POST['msg']) ? $_POST['msg'] : '';

        $sql = "SELECT name, email FROM table"; // Need to be alteret to reflect your db strucure
        $result = mysql_query($sql) or die(mysql_error());
        $to = '';
        while($row = mysql_fetch_array($result)) {
            $name = $row['name'];
            $email = $row['email'];
            $to .= ", $name <$email>";
        }
        $to = trim(substr($to, 1));
        
        if(mail($to, 'Subject', $msg)) {
            echo 'Mail has been successfully send.';
        } else {
            echo 'An error occured. The mail sisn\'t get sent.';
         
    } else {
    ?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

        <label for="msg">Message:</label>
        <textarea name="msg" id="msg"></textarea>

        <button type="submit" name="submit" value="sendemail">Send email</button>
        </form>
    <?php
    }
    ?>
    -Helge


Bookmarks

Posting Permissions

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