开发者

Send email according to option select value

I would like to restrict sending email to only a few addresses. My idea was to have a drop down with the name of the people you could mail to and then in the .php file, the selected names would be matched up to email addresses (to protect them from spam) and sent. If this isn't necessary, the values of the drop downs could just be the email addresses. The way the script is set up right now, you can only send to the 1 address that you specify before hand.

I'm using the jquery and php from here: http://www.twostepmedia.co.uk/send-html-form-results-in-an-email-from-php-using-jquery-ajax/

PHP

<?php
$email = $_POST['email'];
$msg = $_POST['msg'];
$agents = array(1 => "jill@smith.com", 2 => "gjack@smith.com");
$agent = $agents[(int) $_POST['agents']];
// $agent will have your selected e-mail address
$message = "From: " . $email . " Message: " . $msg;
$message = wordwrap($message, 70);
mail($agent, 'The Subject', $message);
?>

jQuery

    <script type="text/javascript" charset="utf-8">
    $(document).ready(function() {
                    $("#submit").click(function(){
            var data = $("#contact").serialize();
                $.ajax
                    ({
                    type: "POST",
                    url: "mail.php",
                    data: data,
                    cache: false,
                    success: function()
                        {
                            alert("Thank you");
                        }
                    });
                return false;
        });
    });
</script>

Form

    <form id="contact">
<p>From</p><input type="text" name="email" />
<p>Message</p><textarea name="msg"></textarea>
<br />
<select id="agents">
    <option value="1">Jill Smith</option>
    <option value="2">Jack Smi开发者_运维百科th</option>
</select>
<br />
<input type="submit" id="submit" value="Send" />
</form>


If you want to protect the addresses from spam, you´d better not have them in the html. You can use just the name or a number as a value and do a lookup in php to get the address (either from a database or from a fixed list).

An example:

html:

<select name="agents" id="agents">
    <option value="1">Jill Smith</option>
    <option value="2">Jack Smith</option>
</select>

php:

$agents = array(1 => "jill@smith.com", 2 => "gjack@smith.com");
$agent = $agents[(int) $_POST['agents']];
// $agent will have your selected e-mail address

Normally you would use your array as well to fill the select so that they always match and it´s easy to add new addresses.

Edit: Based on your comments and edits, it seems you forgot to name your agents select, it has an id but no name so you need to change it to:

<select name="agents" id="agents">


It sounds like you know what to do. Instead of sending the email address, pass a name, or some sort of distinct identifier to match up on the server side. You could use an associative array or a switch statement to map a name to an email address. Is that what you are asking for help for...?


You'd need an array with emails for select names, then just add ids for values in the select and use those ids as indexes in the array. Or you can run a query to select an email by id. That's if you have a table with those people id's/names. In .php file you create a new variable ($agents perhaps) and assign value of $_POST['agents'] to it. If you're to use db, also use mysql_real_escape_string().


In your mail.php file, you have to retrieve the "agent" from the POST variables, as you do with "msg" and "email".

Use something like Firebug to check what is being sent via POST.


use this : http://sourceforge.net/projects/phpmailer/files/phpmailer%20for%20php5_6/

phpmailer is a class that will allow you sending mails to more than on receiver with a function like : $phpmailer->receiver('receiver1'), $phpmailer->receiver('receiver2') and so on then for your multiple list, as said above, put id's in your drop down list and then in your mail.php code you associate the id's to the mail adresses and send your mail with phpmailer that sends mail in a very simple way

you can have this for your mails if ou already know them

$my_allowed_mails = array (
'id1' => 'mail@mail1.com',
'id2' => 'mail@mail2.com',
'id3' => 'mail@mail3.com'
);

   $receiver = $my_allowed_mails[$_POST['mail_choosed']];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜