簡體   English   中英

PHP 聯系表 - 向多個收件人發送電子郵件

[英]PHP Contact Form - Sending email to multiple recipients

我將如何使此表單能夠發送給多個收件人? 目前它只允許我將它發送到 1 個電子郵件,當我嘗試輸入多個(例如“user1@example.com,user2@example.com”)時,它會返回無效的消息。 我需要做什么來解決這個問題?

編輯:用戶必須輸入它想要發送到的電子郵件地址,但它只適用於 1,這就是為什么我要求幫助我如何編輯代碼以處理多個電子郵件/收件人並分開用逗號和空格。

這是代碼

<?php

if(isset($_POST['email'])) {




$email_to = array($_GET["celebrant_emails"]);

$email_subject = "Email from website Contact Form";





function died($error) {

    // your error code can go here

    echo "We are very sorry, but there were error(s) found with the email you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go <a href='http://celebrantsaustralia.asn.au/celebrants-trial.htm'>back</a> and fix these errors.<br /><br />";

    die();

}



// validation expected data exists

if(!isset($_POST['first_name']) ||

    !isset($_POST['last_name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['comments']) ||

    !isset($_POST['celebrant_emails'])) {

    died('We are sorry, but there appears to be a problem with the email you submitted.');       

}



$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$comments = $_POST['comments']; // required

$celebrant_emails = $_POST['celebrant_emails']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$first_name)) {

$error_message .= 'The First Name you entered does not appear to be valid.<br />';

}

if(!preg_match($string_exp,$last_name)) {

$error_message .= 'The Last Name you entered does not appear to be valid.<br />';

}

if(strlen($comments) < 2) {

$error_message .= 'The Message you entered does not appear to be valid.<br />';

}

if(!preg_match($email_exp,$celebrant_emails)) {

$error_message .= 'The Celebrant Email(s) you entered does not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$email_message = "Email details below.\n\n";



function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}



$email_message .= "First Name: ".clean_string($first_name)."\n";

$email_message .= "Last Name: ".clean_string($last_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Message: ".clean_string($comments)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- include your own success html here -->

<html>
    <head>
        <title>Email sent!</title>
    </head>

    <body>
        <b>Thank you for contacting us. We will be in touch with you shortly.</b>
        <p>(Page will auto-direct in a moment, if it doesn't click <a href="http://website.com">here</a>)</p>
    </body>

</html>

<?php

}

?>

有很多方法可以做到這一點。

$email_to = "jhewitt@amleo.com,some@other.com,yet@another.net";

$email_to = 'Mary <mary@example.com>, Kelly <kelly@example.com>';

如果您需要將電子郵件添加為抄送或密送,請在用作標題的變量中添加以下部分:

$headers .= "CC: sombodyelse@noplace.com".PHP_EOL;
$headers .= "BCC: hidden@special.com".PHP_EOL;

來源: PHP 表單向多個收件人發送電子郵件

還可以在這里查看: 示例 #1 發送郵件。

 $email=$_POST['email'];   // write multiple emails with comma's    
 $emails=explode(',', $email); // explode email with comma's
     foreach($emails as $one_email)
     {
         $touser=$one_email;
            $subjectAdmin= "subject";
            $headersAdmin = "From: noreply@talentswype.com\r\n";
            $headersAdmin .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $messageuser ='msg here'; 
     $emailSenduser = mail($touser,$subjectAdmin,$messageuser,$headersAdmin);
}

每封電子郵件編寫一個郵件功能。

mail('example1@example.com', 'My Subject', $message);
mail('example2@example.com', 'My Subject', $message);
mail('example3@example.com', 'My Subject', $message);
mail('example4@example.com', 'My Subject', $message);
mail('example5@example.com', 'My Subject', $message);

或者

foreach($mail as $mails ){
     mail($mail, 'My Subject', $message);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM