简体   繁体   English

如何将电子邮件发送到多个地址?

[英]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.现在这个 index.php 页面是在线托管的,它连接到一个 send.php 页面,我想添加另一个电子邮件文本字段,该字段也用于向其中输入的地址发送电子邮件。

Currently it sends an email to one email address that is entered in the email field in the form.目前,它会向在表单的电子邮件字段中输入的一个电子邮件地址发送一封电子邮件。

Index.php索引.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

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

?>

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM