如何在 PHPmailer 中显示所有选中的复选框
How to display all selected Checkboxes in PHPmailer
我想创建一个复选框并获取所有选中的复选框,然后使用 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 = 'xxxxxxxxxxxxx@gmail.com'; //SMTP username
$mail->Password = 'xxxxxxxxxxxxxxx'; //SMTP password
$mail->SMTPSecure = "tls"; //Enable implicit TLS encryption
$mail->Port = 587;
//Recipients
$mail->setFrom('xxxxxxxxx@gmail.com');
$mail->addAddress('xxxxxxxx@gmail.com' ); //Add a recipient
//$mail->addAddress('ellen@example.com'); //Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//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"]
中有一个数组,您可以对其进行处理以查看所有选定的项目。
对于您的情况,将其放入电子邮件的最简单方法可能是将数组 implode 转换为 comma-separated 字符串:
$checkbox = implode(", ", $_POST['cfselection']);
例如,如果我勾选“搜索引擎和排名”和“社交媒体营销和活动”并提交表单,每个相关复选框中的 value
将进入 $_POST['cfselection']
变量,您在电子邮件中看到的内爆结果将是:
search engines, media marketing
我想创建一个复选框并获取所有选中的复选框,然后使用 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 = 'xxxxxxxxxxxxx@gmail.com'; //SMTP username
$mail->Password = 'xxxxxxxxxxxxxxx'; //SMTP password
$mail->SMTPSecure = "tls"; //Enable implicit TLS encryption
$mail->Port = 587;
//Recipients
$mail->setFrom('xxxxxxxxx@gmail.com');
$mail->addAddress('xxxxxxxx@gmail.com' ); //Add a recipient
//$mail->addAddress('ellen@example.com'); //Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//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"]
中有一个数组,您可以对其进行处理以查看所有选定的项目。
对于您的情况,将其放入电子邮件的最简单方法可能是将数组 implode 转换为 comma-separated 字符串:
$checkbox = implode(", ", $_POST['cfselection']);
例如,如果我勾选“搜索引擎和排名”和“社交媒体营销和活动”并提交表单,每个相关复选框中的 value
将进入 $_POST['cfselection']
变量,您在电子邮件中看到的内爆结果将是:
search engines, media marketing