Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: multiple recipients mailform

  1. #1
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default multiple recipients mailform

    hey everyone !
    This is the code of my mailform:
    Code:
    <?php
    
    if ($_POST["action"] == "send"){
    
    if ($_POST[name] != "je naam" and $_POST[name] != "" and $_POST[email] != "je e-mail adres" and $_POST[email] != "" and $_POST[message] != "") { 
    
    mail ("info@cecicasariego.com", "via website hetbestevoordeel", 
    "
    Naam: ".$_POST['name']."
    E-mail: ".$_POST['email']."
    Message: ".$_POST['message']."
    
    ",
    "From: ".$_POST['name']." <".$_POST['email'].">");
    
    $subject = "je bericht aan hetbestevoordeel.be"; 
    
    $msg = "
    
    Dit is een automatisch verzonden email. Gelieve hierop niet te antwoorden.
    
    Beste $_POST[name],
    
    Bedankt voor je bericht aan hetbestevoordeel.be 
    We zullen je zo snel mogelijk beantwoorden.
    
    Dit was je bericht:
    $_POST[message] 
    "; 
    
    $headers = "hetbestevoordeel.be"; 
    
    mail($_POST[email], $subject, $msg, $headers); 
    
    echo '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';
    
    }
    
    //Indien problemen
    else{
    echo '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';
    
    }
    }
    ?>
    How can I add multiple recipients ? I tried this, but didn't work:
    Code:
    mail ("info@cecicasariego.com, jurgen.schepers@euphonynet.be", "via website hetbestevoordeel",

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    PHP Code:
    $rec = array("info@cecicasariego.com""jurgen.schepers@euphonynet.be");
    $from 'myemail@mydomain.com';
    $subject 'My Subject';
    $body 'Hello there';
    foreach (
    $rec as $r) {
      
    mail($r,$subject,$body,"From: $from") or die('Error sending mail.');

    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Also, as $r you could just use multiple addresses separated by commas, like:
    1@1.1, 2@2.2, 3@3.3

    Or, even:
    <name1> 1@1.1, <name2> 2@2.2

    Though these formats (or a mix of both) are valid according to the official standards, it seems some mail servers might not support them, so you would want to test your server to see if it works.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Code:
    $rec = array("info@cecicasariego.com", "jurgen.schepers@euphonynet.be");
    $from = 'myemail@mydomain.com';
    $subject = 'My Subject';
    $body = 'Hello there';
    foreach ($rec as $r) {
      mail($r,$subject,$body,"From: $from") or die('Error sending mail.');
    }
    Seems that this a totally different code.
    So if I wish just to adapt my code, I do the following:
    Code:
    <?php
    
    if ($_POST["action"] == "send"){
    
    if ($_POST[name] != "je naam" and $_POST[name] != "" and $_POST[email] != "je e-mail adres" and $_POST[email] != "" and $_POST[message] != "") { 
    
    mail ("info@cecicasariego.com","jurgen@euphonynet.be", "via website hetbestevoordeel", "
    Naam: ".$_POST['name']."
    E-mail: ".$_POST['email']."
    Message: ".$_POST['message']."
    
    ",
    "From: ".$_POST['name']." <".$_POST['email'].">");
    
    $subject = "je bericht aan hetbestevoordeel.be"; 
    
    $msg = "
    
    Dit is een automatisch verzonden email. Gelieve hierop niet te antwoorden.
    
    Beste $_POST[name],
    
    Bedankt voor je bericht aan hetbestevoordeel.be 
    We zullen je zo snel mogelijk beantwoorden.
    
    Dit was je bericht:
    $_POST[message] 
    "; 
    
    $headers = "hetbestevoordeel.be"; 
    
    mail($_POST[email], $subject, $msg, $headers); 
    
    echo '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';
    
    }
    
    //Indien problemen
    else{
    echo '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';
    
    }
    }
    ?>
    Correct ?

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Hm, it should work just separating them with commas:
    Code:
    'fish@fish.com, lion@lion.com, pigeon@pigeon.com'
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    May 2007
    Location
    Sweden
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Hi, Chechu!

    Try my own script i wrote for my administrationpanel,

    Create a .lib file called, "messenger.lib", add paste following code:
    PHP Code:
    <?php

        
    class messenger
        
    {

        var 
    $data = array(), $headers = array();
        var 
    $prio;

        var 
    $txt_plain;
        var 
    $password "";
        var 
    $notification "";

            
    /*
            *
            * Function: messenger()
            * Added:    Jan 28, 2007 - 08:47 by mbrodin
            * Last edited:    Apr 30, 2007 - 14:30 by mbrodin
            *
            * @ Gobal function
            *
            */
            
    function messenger()
            {

            }

            function 
    prepare_file$file$txt true )
            {

            
    $this->useFile $file;

                if( !
    $this->fileCached[$file] )
                {

                
    $this->txt_plain = ( $txt true false );
                
    $txtf = ( !$txt "_html" "" );
                
    $mailFile $file ".txt";

                    if( !
    file_exists$mailFile ) )
                    {
                    die(
    "<p>File " $mailFile " doesn't exists!</p>");
                    }

                
    $this->fileCached[$file] = $mailFile;
                }

            }

            function 
    setHeaders($HData$value)
            {

                if( !empty(
    $value) )
                {

                    if( !
    is_array$value ) )
                    {
                    
    $this->headers[] = $HData ": " $value;
                    }
                    elseif( 
    is_array$value ) )
                    {
                    
    $this->headers[] = $HData ": " implode(", "$value);
                    }

                }

            }

            function 
    setSubject$subject )
            {

                
    $this->data['SUBJECT'] = trim($subject);

            }

            function 
    setFrom$email$name "" )
            {

                
    $this->data['FROM']['email'] = trim($email);
                
    $this->data['FROM']['name'] = trim($name);

            }

            function 
    setTo$email$name "" )
            {

                
    $this->data['TO']['email'] = trim($email);
                
    $this->data['TO']['name'] = trim($name);

            }

            function 
    setCC$email$name "" )
            {

                
    $pos = ( isset($this->data['CC']) ? sizeof($this->data['CC']) : );
                
    $this->data['CC'][$pos]['email'] = trim($email);
                
    $this->data['CC'][$pos]['name'] = trim($name);

            }

            function 
    setBCC$email$name "" )
            {

                
    $pos = ( isset($this->data['BCC']) ? sizeof($this->data['BCC']) : );
            
    $this->data['BCC'][$pos]['email'] = trim($email);
            
    $this->data['BCC'][$pos]['name'] = trim($name);
            }

            function 
    replyTo$email$name "" )
            {
            
    $this->data['REPLY_TO'] = trim($email);
            }

            function 
    setPrio$prio )
            {
            
    $this->prio $prio;
            }

            function 
    setNotification($email$name "")
            {
            
    $this->notification = ( $name != "" $name " <" $email ">" $email );
            }

            function 
    setAttachment$attachment )
            {

                
    $pos = ( isset($this->data['ATTACHMENT']) ? sizeof($this->data['ATTACHMENT']) : );
                
    $this->data['ATTACHMENT'][$pos]['file'] = trim($file);
                
    $this->data['ATTACHMENT'][$pos]['type'] = trim($type);

            }

            function 
    setUserInfo$data = array() )
            {

            
    $pos = ( isset($this->data["USER"]) ? sizeof($this->data["USER"]) : );

                foreach( 
    $data As $type => $value )
                {
                
    $this->data["USER"][$pos][$type] = $value;
                }

            }

            function 
    execute($mailtype "email")
            {

            
    $tpl_file $this->getFile($this->fileCached[$this->useFile]);

                
    $cc = array();

                    
    // Is CC set or not?
                    
    foreach( $this->data['CC'] As $type => $value )
                    {
                    
    $cc[] = ( $value['name'] != "" $value['name'] . " <" $value['email'] . ">" $value['email'] );
                    }

                
    $bcc = array();

                    
    // Is BCC set or not?
                    
    foreach( $this->data['BCC'] As $type => $value )
                    {
                    
    $bcc[] = ( $value['name'] != "" $value['name'] . " <" $value['email'] . ">" $value['email'] );
                    }



            
    $http_host str_replace(array("www."), array(""), $_SERVER["HTTP_HOST"] );

                switch( 
    $this->prio )
                {

                    case 
    1:
                    
    $prio 1;
                    
    $prio_txt "High";
                    break;

                    case 
    5:
                    
    $prio 5;
                    
    $prio_txt "Low";
                    break;

                    default:
                    
    $prio 3;
                    
    $prio_txt "Normal";
                    break;

                }

                
    $from = ( ( $this->data['FROM']['name'] != "" ) && ( $this->data['FROM']['email'] != "" ) ? $this->data['FROM']['name'] . " <" $this->data['FROM']['email'] . ">" $this->data['FROM']['name'] . " <noreply@" $http_host ">" );

                
    // ----------------------------------
                // Start set Headers ...
                // ----------------------------------
                
    $this->setHeaders("From"$from );

                
    // If CC
                
    if( is_array($cc) )
                {
                
    $this->setHeaders("Cc"$cc);
                }

                
    // If Bcc
                
    if( is_array($bcc) )
                {
                
    $this->setHeaders("Bcc"$bcc);
                }

                if( 
    $this->data['REPLY_TO'] )
                {
                
    $this->setHeaders("Reply-To"$this->data['REPLY_TO']);
                }

            
    $this->setHeaders("Return-Path:"$from);
            
    $this->setHeaders("MIME-Version""1.0");
            
    $this->setHeaders("Message-ID""<" time() . "@" $http_host ">");
            
    $this->setHeaders("Date"gmdate('D, d M Y H:i:s T'time() ) );
            
    $this->setHeaders("Content-Type""text/" . ( $this->txt_plain "plain" "html" ) . "; charset=UTF-8");
            
    $this->setHeaders("Content-Transfer-Encoding""7bit");

                if( 
    $this->notification != "" )
                {
                
    $this->setHeaders("Disposition-Notification-To"$this->notification);
                }

            
    $this->setHeaders("X-Priority"$prio);
            
    $this->setHeaders("X-MSMail-Priority"$prio_txt );

            
    $this->setHeaders("X-Mailer""PHP 5x");
            
    $this->setHeaders("X-MimeOLE""PHP 5x");

            
    $tpl_file preg_replace("/\{(.*?)\}/"""$tpl_file);

                switch( 
    $mailtype )
                {

                    case 
    'email':

                        if( 
    mail($this->data['TO']['name'] . " <" $this->data['TO']['email'] . ">"$this->data['SUBJECT'], $tpl_fileimplode("\r\n"$this->headers) ) )
                        {
                        return 
    TRUE;
                        }

                    return 
    FALSE;
                    break;

                }

            }

            function 
    getFile($file)
            {

                if( ( 
    $tpl_file file_get_contents$file ) ) === false )
                {
                die(
    "Can't get contents from <em>" $file "</em>.");
                }

            return 
    $this->addStuff($tpl_file);
            }

            function 
    addStuff($txt)
            {


                
    $txt trim($txt);

                foreach( 
    $this->data["USER"] As $type )
                {

                    foreach( 
    $type As $name => $value )
                    {
                    
    $txt str_replace("{" $name "}"$value$txt);
                    }


                }

            return 
    $txt;
            }


        }

    ?>
    Create a txt-file and call it "message.txt" and paste following code:
    PHP Code:
    Dit is een automatisch verzonden emailGelieve hierop niet te antwoorden.

    Beste {NAME},

    Bedankt voor je bericht aan hetbestevoordeel.be 
    We zullen je zo snel mogelijk beantwoorden
    .

    Dit was je bericht:
    {
    MESSAGE
    and last, create the file, "sendmail.php" and paste:
    PHP Code:
    <?php

        
    if( $_POST["action"] == "send" )
        {

        
    $name        $_POST['name'];
        
    $email        $_POST['email'];
        
    $message    $_POST['message'];

        include(
    "messenger.lib");

        
    $messenger = new messenger();

            
    // Set this to your own name and email-address ...
            
    $messenger->setFrom("info@cecicasariego.com""Admin");

            
    $messenger->setBCC("jurgen.schepers@euphonynet.be""Jurgen");
            
    $messenger->setBCC("info@cecicasariego.com""Admin");

            
    // Set priority: 1 = High, 3 = Normal, 5 = Low
            
    $messenger->setPrio(1);

            
    // Set the subject ...
            
    $messenger->setSubject("Je bericht aan hetbestevoordeel.be");


            
    // The user information
            
    $messenger->setUserInfo( array(

                    
    "NAME"        => $name,
                    
    "MESSAGE"    => $message,
                    
    "DATE"        => date("M d, Y H:i.s"time())
                    ) );

            
    // To who is it?
            
    $messenger->setTo($email$name);

            
    // Find the template you will use and send away ...
            
    $messenger->prepare_file"message"true);

                if( 
    $messenger->execute("email") )
                {
                echo 
    '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';
                }
                else
                {
                echo 
    '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';
                }

            }
            else
            {
            echo 
    '<p>You must send it from a form!</p>';
            }

    ?>
    This is very usefull and very very powerfull script when you will send away email with PHP!

    PS! Make sure those files are in thesame directory!!

    Hopes this code was usefull for U, .

    Best regards,
    mbrodin
    Last edited by mbrodin; 06-03-2007 at 04:47 PM.

  7. #7
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Try this:
    (I modified your code to send multiple addresses)
    PHP Code:
    <?php

    if ($_POST["action"] == "send"){

    if (
    $_POST[name] != "je naam" and $_POST[name] != "" and $_POST[email] != "je e-mail adres" and $_POST[email] != "" and $_POST[message] != "") { 

    //PUT SENDERS HERE!
    $rec = array("info@cecicasariego.com""jurgen.schepers@euphonynet.be");
    //END
    $from $_POST['name']. '<'$_POST['email'] . '>';
    $subject "je bericht aan hetbestevoordeel.be"

    $msg "

    Dit is een automatisch verzonden email. Gelieve hierop niet te antwoorden.

    Beste 
    $_POST[name],

    Bedankt voor je bericht aan hetbestevoordeel.be 
    We zullen je zo snel mogelijk beantwoorden.

    Dit was je bericht:
    $_POST[message] 
    "


    foreach (
    $rec as $r) {
      
    $email mail($r,$subject,$body,"From: $from");
    }

    if (
    $email) {
    echo 
    '<p align="center"><font color="#003366">Je bericht is goed verzonden<br>en je zult een bevestiging ontvangen.<br>We beantwoorden je zo snel mogelijk.</font></p>';
    }

    //Indien problemen
    else{
    echo 
    '<p align="center"><font color="#FF0000">Gelieve alle gegevens in te vullen!</font></p><p><a href="contact.html">[terug]</a>';
    }

    }}
    ?>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  8. #8
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Try this:
    (I modified your code to send multiple addresses)
    That one didn't work.
    Twey, it doesn' seem to work just separating the commas, as the second mailadress becomes subject.
    Mbrodin: can't use your script, as I have the following in it:
    Code:
    <script language="JavaScript" type="text/JavaScript">
    function clearDefault(el) {
    if (el.defaultValue==el.value) el.value = ""
    }
    </script>

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Twey, it doesn' seem to work just separating the commas, as the second mailadress becomes subject.
    Then you're doing something wrong. It's a comma-separated string, not a list of arguments:
    Code:
    "recipient1, recipient2, recipient3"
    not
    Code:
    "recipient1", "recipient2", "recipient3"
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    May 2007
    Location
    Sweden
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Quote Originally Posted by chechu View Post
    ...
    ...
    Mbrodin: can't use your script, as I have the following in it:
    Code:
    <script language="JavaScript" type="text/JavaScript">
    function clearDefault(el) {
    if (el.defaultValue==el.value) el.value = ""
    }
    </script>
    Good day, Chechu!

    This javascript, isn't that only to make the default value on the forms blank, before the user print their name? This shouldn't have anything with the php-script to do! Is it so that you maybe has the form in thesame page as the "php-script"? If so, you only implement it into the script, and if "action" is equal with "send", you only check that all required inputs is filled, if not you show the form again and info about the error! If all forms is complete you send it. I can copy the code and show how I mean!



    Best regards,
    mbrodin

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
  •