0

How can I send an email to multiple email addresses? Right now this index.php page is being hosted online, and it connects to a send.php page, and I'd like to add another email text field that would be used to also send an email to the address entered in it.

Currently it sends an email to one email address that is entered in the email field in the form.

Index.php

<form id="contactus" method="post" action="send.php">

<input type="name" name="name" class="form-control" placeholder="Name" required>.   <br/><br/>

<input type="email" name="email" class="form-control" placeholder="Email" required>.   <br/><br/>

<input type="name" name="name2" class="form-control" placeholder="Player 2's Email Address" required><br/><br/>

<textarea name="message" class="form-control" placeholder="What Is Your Question?" required></textarea><br/><br/>

<input type="hidden" name="phone2">

<button type="submit" class="btn btn-success">Send Email</button>

<br><br>

</form>

Send.php

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); //sanitize data
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);

/*added line of code*/
if(empty($name)){
header("location: index.php?nouser");
exit();
}

if(empty($email)){
header("location: index.php?noemail");
exit();
}

if(empty($message)){
header("location: index.php?noemail");
exit();
}

/*if(empty($message)){
header("location: index.php?nomessage");
exit();
}//end validation*/

//added line; 'honeypot'
if(empty($_POST['phone2'])){


//Information that needs to be filled out to be able to send an email through phpmailer
//Will use an SMTP service. SMTP is basically like a mail server to send mail
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);

try {

//Server settings
$mail->SMTPDebug = 1;                                       
//$mail->IsSMTP();
$mail->SMTPDebug = 1;                 
$mail->SMTPAuth = false;                
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = "25";
$mail->Username = "#Email_Placeholder";
$mail->Password = "Password";
$mail->SetFrom("#Email_Placeholder");

$mail->setFrom('#Email_Placeholder', 'Greeting');

// Add a recipient : used to also send to name
$mail->addAddress($email);     

$mail->addReplyTo('info@example.com', 'Information');

$mail->addCC('email@gmail.com'); 

/*$mail->addBCC('bcc@example.com');*/

//Body content
$body = "<p><strong>Hello, </strong>" . $email . " How are you? 
    
    <br>
 
   <b> Please Notice: </b><br>
      

<br> <br> Your message was Delivered: " . $message . ". Hello" . $name2 . 
"</p>"; 



$mail->isHTML(true);                                  
//subject line of email
$mail->Subject = $name;
//for html email
$mail->Body    = $body;

$mail->AltBody = strip_tags($body);
//the email gets sent
header("Location: http://www.test-domain.com"); 
$mail->send();

} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}

?>
programcode
  • 104
  • 1
  • 14

2 Answers2

1

Fore readability sake in the code use an array and implode it to a comma separated string:-

$recipients = array(
  "youremailaddress@yourdomain.com",
  // more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page
Julien Baldy
  • 397
  • 2
  • 4
  • 14
1

This is pretty simple - if you follow the code, you just need to duplicate some lines.

Duplicate your form field

<input type="email" name="email" class="form-control" placeholder="Email" required>
<input type="email" name="email_2" class="form-control" placeholder="Email" required>

Duplicate your validation

$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$email_2 = filter_var($_POST['email_2'], FILTER_SANITIZE_STRING);

Duplicate the 'Add Address'

$mail->addAddress($email);
$mail->addAddress($email_2);
MaggsWeb
  • 3,018
  • 1
  • 13
  • 23
  • Thank you for your answer. I remember doing this a while back but just forgot it. Can you help me figure out why the `email_2` email goes to the spam folder now? – programcode Nov 08 '21 at 14:12
  • Constant [FILTER_SANITIZE_STRING](https://stackoverflow.com/questions/69207368/constant-filter-sanitize-string-is-deprecated) is deprecated. Please stop using it. – Dharman Nov 15 '21 at 12:37