根据选项选择值发送电子邮件
我想限制只向几个地址发送电子邮件。我的想法是在下拉列表中显示您可以向其发送邮件的人员的姓名,然后在 .php 文件中,所选的姓名将与电子邮件地址相匹配(以保护他们免受垃圾邮件的侵害)并发送。如果没有必要,下拉列表的值可以只是电子邮件地址。目前脚本的设置方式是,您只能发送到您事先指定的 1 个地址。
我在这里使用 jquery 和 php: 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 => "[email protected]", 2 => "[email protected]");
$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 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 Smith</option>
</select>
<br />
<input type="submit" id="submit" value="Send" />
</form>
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 => "[email protected]", 2 => "[email protected]");
$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 Smith</option>
</select>
<br />
<input type="submit" id="submit" value="Send" />
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想保护地址免受垃圾邮件的侵害,最好不要将它们放在 html 中。您可以仅使用名称或数字作为值,并在 php 中进行查找以获取地址(从数据库或从固定列表)。
示例:
html:
php:
通常您也会使用数组来填充
select
,以便它们始终匹配并且可以轻松添加新地址。编辑:根据您的评论和编辑,您似乎忘记命名您的
代理
选择,它有一个id
但没有名称
所以你需要将其更改为: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:
php:
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 anid
but noname
so you need to change it to:听起来你知道该怎么做。不要发送电子邮件地址,而是传递名称或某种不同的标识符以在服务器端进行匹配。您可以使用关联数组或 switch 语句将名称映射到电子邮件地址。 这就是您寻求帮助的原因吗...?
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...?
您需要一个包含用于选择名称的电子邮件的数组,然后只需为选择中的值添加 id 并将这些 id 用作数组中的索引。
或者您可以运行查询来按 ID 选择电子邮件。如果你有一个包含这些人的 ID/姓名的表。
在 .php 文件中,您创建一个新变量(可能是 $agents)并为其分配 $_POST['agents'] 值。如果您要使用 db,也请使用 mysql_real_escape_string()。
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().
在 mail.php 文件中,您必须从 POST 变量中检索“agent”,就像处理“msg”和“email”一样。
使用 Firebug 之类的工具来检查通过 POST 发送的内容。
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.
使用这个: http://sourceforge.net/projects/phpmailer/files/phpmailer %20for%20php5_6/
phpmailer 是一个类,它允许您将邮件发送到多个接收者,其功能如下:
$phpmailer->receiver('receiver1'), $phpmailer->receiver('receiver2')
等等,然后对于您的多个列表,如上所述,将 id 放入您的下拉列表中然后在您的 mail.php 代码中将 id 与邮件地址相关联,并使用 phpmailer 发送邮件,它以非常简单的方式发送邮件,您可以将其用于您的邮件如果你已经认识他们
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 wayyou can have this for your mails if ou already know them