根据选择发送电子邮件(带文件上传) [英] Send email based on selection (with file upload)
问题描述
我正在尝试为我的网站创建一个页面,学生必须在大约 10 个选项(多选)中选择 3 个或更少.他们还需要在此下方上传文件.当他们最终点击提交时,它会根据他们的选择(10 个选项中的 3 个或更少)将他们上传的文件发送到多个电子邮件地址(甚至可以使用特定的单个电子邮件地址).
I'm trying to create a page for my website, where students have to select 3 or less out of about 10 choices(multiple selection). They also need to upload a file below this. When they click submit finally, it would send the file they uploaded to multiple email addresses (or even a specific single email address would work)based on their selection (3 or less out of 10 choices).
有人可以帮我吗?我对 HTML 有一定的了解,但我不知道 JavaScript,因此我在弄清楚它时遇到了很多麻烦.我在互联网上查找了各种帖子,但找不到非常相似且有效的内容.
Can someone please help me out? I have a okay knowledge of HTML, but I don't know JavaScript and hence I'm having a lot of trouble figuring it out. I have looked up various posts on the internet, but I couldn't find something that was pretty similar and worked.
另外,我使用的是 WordPress,因为我需要向页面添加 Javascript,所以我安装了一个名为 Exec-PHP Plugin 的插件.看起来它有效,但是请告诉我您的意见.
Also, I am using WordPress, and since I need to add Javascript to the page, I installed a plugin called Exec-PHP Plugin. It seems like it works, however please do let me know your opinion.
非常感谢,非常感谢您的帮助!
Thanks so much, really appreciate any help!
这是我试过的代码.这是我浏览各种帖子时发现的.它不完全是我在这个问题中提到的,因为就像我之前提到的那样,我之前对 JavaScript 没有任何了解.在这里,它基本上应该基于单个下拉选择向电子邮件发送消息.我试过了,但没有用(我更改了目标电子邮件).谢谢!
This is the code I tried. Its what I found when I looked through various posts. Its not exactly what I mentioned in this question, since like I mentioned before I do not have any previous knowledge of JavaScript. Here, its basically supposed to send a message to the email based on a single drop-down selection. I tried it, but it did not work (I changed the destination emails). Thanks!
<form id="contactForm" method="post" accept-charset="utf-8">
<input id="name" type="text" name="name" minlength="2" placeholder="Your Name…" required>
<input id="email" type="email" name="email" placeholder="Your Email…" required>
<input id="name2" type="text" name="name2" placeholder="Your Last Name…" required>
<select id="department" type="text" name="department">
<option value="customer" name="customer">I am a customer</option>
<option value="distribution" name="distribution">department in distribution</option>
<option value="press" name="press">I am with the press</option>
<option value="career" name="career">department in a career position</option>
<option value="other" name="other">Other</option>
</select>
<textarea name="message" placeholder="Your Message…" required></textarea>
<input type="submit" value="Send away!">
</form>
<?php
// We use the name2 field as bait for spambots. It's display is off,
// so humans wouldn't know to enter anything into it. Robots would,
// so we ignore submissions with info in name2.
$mail_sent = false;
if(sizeof($_POST) && $_POST["name2"] == "") // receiving a submission
{
define("SUBJECT", "Visitor Message from Website");
// production recipient:
define("RECIPIENT", ".$department.");
// prep our data from the form info
$senderName = $_POST['name'];
$senderEmail = $_POST['email'];
$department = $_POST['department'];
$subject = SUBJECT;
$messageBody = $senderName . ' ('.$senderEmail.') wrote in '.$department.':
' . $_POST['message'];
if($department == 'customer') { //if customer was selected
$to = 'customer@gmail.com';
}
else if($department == 'distribution') { //if distribution was selected
$to = 'distribution@email.com';
}
else if($department == 'press') { //if press was selected
$to = 'press@email.com';
}
else if($department == 'career') { //if career was selected
$to = 'career@email.com';
}
else if($department == 'other') { //if other was selected
$to = 'other@email.com';
}
// From
$header = "from: $senderName <$senderEmail>";
// To
$to = RECIPIENT;
// Send it!
$send_contact = mail($to, $subject, $messageBody, $header);
// Check success of send attempt
if($send_contact){
// show thankyou screen
$mail_sent = true;
}
else {
// send failed.
echo "ERROR";
}
}
?>
推荐答案
我没有示例代码给你,但大致上,它会是这样的:
I dont have a sample code to give you, but roughly, it would be something like this:
<html>
<head>
<script type="text/javascript">
function my_js_submit()
{
// check the email input fields that you care about.
if(document.getElementById("email1").value != "" &&
document.getElementById("email2").value != "" &&
document.getElementById("email3").value != "")
{
document.forms["my_form"].submit(); //this will call the php where you do
//emailing
}
else
{
alert("Please fill in all three emails");
}
}
</script>
</head>
<body>
<!-- submit_handler.php is where you send emails -->
<form id="my_form: method="post" action="submit_handler.php">
<input type="text" id="email1" />
<input type="text" id="email2" />
<input type="text" id="email3" />
<input type="text" id="message" />
<button onclick="my_js_submit();"> Submit </button>
</form>
</body>
</html>
PHP 最基本的代码(将此文件命名为 submit_handler.php):
PHP most basic code (name this file submit_handler.php):
<?php
$to = $_POST['email1'] . "; " . $_POST['email2'] . "; " . $_POST['email3'];
$subject = "Email subject";
$message = $_POST['message'];
$from = "From: your_email@yourDomain.com";
mail($to,$subject,$message,$from); // The Mail function at work
// Redirect your user to a page of your choice
header("Location: http://www.some_page.com");
?>
这篇关于根据选择发送电子邮件(带文件上传)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!