Javascript 如何使用PHPmailer发送选择了下拉列表的电子邮件?
我试图找出如何检索所选的下拉列表,并将其发送到邮件使用PHPmailer,但我无法实现它,它只是需要一些调整!!还要注意的是,我正在使用Bulma作为我项目的一部分Javascript 如何使用PHPmailer发送选择了下拉列表的电子邮件?,javascript,php,html,phpmailer,Javascript,Php,Html,Phpmailer,我试图找出如何检索所选的下拉列表,并将其发送到邮件使用PHPmailer,但我无法实现它,它只是需要一些调整!!还要注意的是,我正在使用Bulma作为我项目的一部分 <form action="bug.php" method="POST" class="" enctype="multipart/form-data"> <div class="section">
<form action="bug.php" method="POST" class="" enctype="multipart/form-data">
<div class="section">
<div class="container">
<div class="columns">
<div class="column"></div>
<div class="column is-5">
<div class="field">
<label class="label">Name</label>
<div class="control">
<input
class="input"
type="text"
id="name"
name="vname"
placeholder="e.g Alex Smith"
/>
</div>
</div>
<div class="field">
<label class="label">Email</label>
<div class="control">
<input
class="input"
type="email"
name="vemail"
placeholder="e.g. alexsmith@gmail.com"
/>
</div>
</div>
<div class="control">
<div class="select">
<select id="selection" onchange="showradio(this)">
<option value="0">Select dropdown</option>
<option value="1">Best</option>
<option value="2">Bestest</option>
<option value="3">Exceptional</option>
</select>
</div>
</div>
<div class="buttons" >
<button name="btnsubmit" id="btnsend" class="button is-primary is-light">Submit</button>
</div>
</div>
<div class="column"></div>
</div>
</div>
</div>
</form>
名称
电子邮件
选择下拉列表
最好的
最好的
异常
提交
现在是我配置的PHPMailer
<?php
$name = $_POST['vname'];
$email = $_POST['vemail'];
$msg = $_POST['vmsg'];
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'PHPmailer/vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.gmail.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'from24@gmail.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = 587; // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
//Recipients
$mail->setFrom('messagefrom24@gmail.com', 'Tools');
$mail->addAddress('messageto@gmail.com', 'To'); // Add a recipient
$mail->AddAddress("$_POST['emailreceiver']");
// // Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'New Bug Submission !';
$mail->Body = $msg;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>
正如所建议的,您缺少select元素的“name”属性,因此,您需要输入name=“my\u dropdown”。此外,还需要将选项的值从0,1,2更改为实际文本。
如果不想更改选项值,则可以创建一个简单的帮助器函数,如下所示:
function get_dropwown_text() {
return ["Select dropdown", "Best", "Bestest", "Exceptional"];
}
在您的视图(HTML)中,您可以通过以下方式使用helper函数准备下拉列表:
<select id="selection" onchange="showradio(this)">
<?php
foreach(get_dropwown_text() as $k => $v) {
echo sprintf('<option value="%d">%s</option>', $k, $v);
}
?>
</select>
您的代码中存在一个主要漏洞。
这些行的作用是形成一个垃圾邮件网关:
$msg = $_POST['vmsg'];
$mail->AddAddress("$_POST['emailreceiver']");
$mail->Body = $msg;
任何人都可以编写一个脚本,使用您的服务器将他们选择的任何内容发送给他们喜欢的任何人。这是一个非常危险的事情,允许这样的洞经常被发现和大规模开发
要解决此问题,您需要更改电子邮件地址的使用或消息内容。前者更简单,方法是不使用提交的地址发送,而是使用它的代理
在表单中,您可能有如下选项:
function get_dropwown_text() {
return ["Select dropdown", "Best", "Bestest", "Exceptional"];
}
销售额
技术支持
账户
然后在脚本中,将这些值映射到固定地址:
$addresses=[
'销售'=>'sales@example.com',
'支持'=>'support@example.com',
'帐户'=>'accounts@example.com',
];
//在尝试使用之前验证地址选择
如果(array_key_存在('emailreceiver',$_POST)&&array_key_存在($_POST['emailreceiver',$addresses)){
$mail->addAddress($addresses[$\u POST['emailreceiver']]);
}否则{
//如果选择无效,请返回固定地址
$mail->addAddress('support@example.com');
}
//验证提交的任何电子邮件地址,并将其用作对的答复
如果(!$mail->addReplyTo($\u POST['vemail'],$\u POST['vname'])){
die(“提供的电子邮件地址无效”);
}
这样,就没有外部控制来控制消息发送给谁。有关如何安全地构建联系人表单的指导,请参阅
另一方面,这是可疑的,这表明您可能不了解composer的工作原理:
require 'PHPmailer/vendor/autoload.php';
composer.json
文件作为一个整体应该位于项目的顶层,而不是特定于PHPMailer的文件。搜索有关如何使用composer的教程。在选择标记中添加name=“dropdown”,然后通过$\u POST['dropdown']获取,而不是value=“0,1,2”,将其更改为要发送的字符串