PHPmailer:如果发件人选中复选框,则允许将电子邮件副本发送给发件人


PHPmailer: allow copy of email to be sent to sender if they check a box

我希望用户能够在完成表格后选择一个写着"给我发送电子邮件副本"的框。我使用phpMailer将表格发送给自己,我不知道如何在用户选择该框的情况下添加第二个电子邮件地址。现在我有:

if (isset($_POST["send"])) {
    $senderEmail = $email;
}   

<li>
<h4>Send me a copy of this form</h4>
<input type="radio" id="send" name="send">
<label for="send">Yes please</label>
<input type="radio" id="nosend" name="nosend">
<label for="nosend">No, thank you</label>
</li>

最后是

include("includes/class.phpmailer.php");
$mail = new PHPMailer;
$mail->setFrom($email, $firstname);
$mail->addAddress('email@address.com'); 
$mail->addAddress($senderEmail);
$mail->addReplyTo($email, $name);
$mail->isHTML(true);
$mail->Subject = 'New email from ' . $name;
$mail->Body    = "
<h2>Email from your Website</h2>
<p>Name: $firstname $lastname </p>
<p>Email: $email </p>
<p>Phone: $phone </p>
<p>Country: $
<p>Message: $comments </p>

";

此外,当我们查看它时,在上面列出的电子邮件正文中,我想在下拉选择菜单中获得国家的信息。如果有人能告诉我如何做到这一点,以及如何从发送者从无线电检查表中做出的任何选择中做到同样的事情,我将不胜感激。

您发送两个参数nosendsend,而您只需要一个:send。这一个将是truefalse,具体取决于所选择的无线电。请注意,它们的名称现在都是send,并且我添加了value属性。

<input type="radio" id="send" name="send" value="true">
<input type="radio" id="nosend" name="send" value="false">

由于只有两个选项,因此使用复选框而不是收音机可能更有意义。当有两个以上选项时,无线电输入更合适。

<input type="checkbox" id="send" name="send" value="true">

然后,在您的邮件逻辑中,如果send参数设置为适当的值,则只添加附加地址。

$mail->addAddress('email@address.com'); 
if (isset($_POST['send']) && $_POST['send'] == 'true') {
    $mail->addAddress($senderEmail);
}