programcode
programcode

Reputation: 102

How to send an email to multiple addresses?

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('[email protected]', 'Information');

$mail->addCC('[email protected]'); 

/*$mail->addBCC('[email protected]');*/

//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}";
}
}

?>

Upvotes: 0

Views: 89

Answers (2)

MaggsWeb
MaggsWeb

Reputation: 3017

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

Upvotes: 1

Julien Baldy
Julien Baldy

Reputation: 426

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

$recipients = array(
  "[email protected]",
  // more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page

Upvotes: 1

Related Questions