All All - 7 months ago 27
PHP Question

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

I want users to be able to select a box that says "Send me a copy of the email" after completing a form. I'm using phpMailer to send the form to myself, and I don't know how to add a second email address under the condition that the person selected this box. Right now I have:

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


and

<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>


and finally

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>


";


Also, while we're looking at it, in the Body of the email as listed above, I want to get the information for Country which is in a dropdown selection menu. If anyone can also tell me how to do this, as well as how to do the same from whichever selection a sender makes from a radio checklist, I'd be very grateful.

Answer

You're sending two parameters nosend and send when you only need one: send. That one will be either true or false depending on which radio is selected. Note that both their names are send now and I added value attributes.

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

Since there are only two options, it might make more sense to use a checkbox instead of radios. Radio inputs are more appropriate when there are more than two options.

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

Then, in you're mailing logic, only add the additional address if the send parameter is set to the appropriate value.

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