我想创建一个复选框并获取所有选中的复选框并使用 phpmailer 将它们通过电子邮件发送到我的电子邮件。显然,我只能从所有选定的复选框中选择 1 个值。对不起,如果我从这些缩进中让你的大脑自杀了 xd 这是我的 html 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Send Mail</title>
</head>
<body>
<h3> Send Email using SMTP </h3>
<form action="mail.php" method="post">
<label for="cfselection-0">
<input name="cfselection" id="cfselection-0" value="website design type="checkbox">
Website Design
</label>
</div>
<div class="checkbox">
<label for="cfselection-1">
<input name="cfselection" id="cfselection-1" value="search engines" type="checkbox">
Search Engines and Ranking
</label>
</div>
<div class="checkbox">
<label for="cfselection-2">
<input name="cfselection" id="cfselection-2" value="media marketing" type="checkbox">
Social Media Marketing and Campaigns
</label>
</div>
<div class="checkbox">
<label for="cfselection-3">
<input name="cfselection" id="cfselection-3" value="other" type="checkbox">
Other
</label>
</div>
</div>
</div>
<button type="submit"> Dërgo </button>
</form>
</body>
</html>
这是我的 php 文件:
<?php
//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 'vendor/autoload.php';
//get data from form
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$checkbox = $_POST['cfselection'];
// preparing mail content
$messagecontent ="Name = ". $name . "<br>Email = " . $email . "<br>Message =" . $message . "<br>Checkbox =" . $checkbox;
//Create an instance; 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 = '[email protected]'; //SMTP username
$mail->Password = 'xxxxxxxxxxxxxxx'; //SMTP password
$mail->SMTPSecure = "tls"; //Enable implicit TLS encryption
$mail->Port = 587;
//Recipients
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]' ); //Add a recipient
//$mail->addAddress('[email protected]'); //Name is optional
//$mail->addReplyTo('[email protected]', 'Information');
//$mail->addCC('[email protected]');
//$mail->addBCC('[email protected]');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
// $mail->addAttachment('photo.jpeg', 'photo.jpeg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = "blabla";
$mail->Body = $messagecontent;
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
// echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
您需要name
在表单上的复选框属性中使用数组语法:
name="cfselection[]"
这将允许以相同的名称提交多个值。(如果不这样做,只需单独提交最后一个,其他的都会被覆盖。)
然后在 PHP 中,您将拥有一个数组$_POST["checkbox"]
,您可以在其中处理以查看所有选定的项目。
对于您的情况,将其放入电子邮件的最简单方法可能是将数组内爆为逗号分隔的字符串:
$checkbox = implode(", ", $_POST['cfselection']);
因此,例如,如果我勾选“搜索引擎和排名”和“社交媒体营销和活动”并提交表单,value
每个相关复选框$_POST['cfselection']
中的电子邮件将是:
search engines, media marketing
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句