簡體   English   中英

php電子郵件表單:發送到多個地址並從輸入中排除數據

[英]php Email Form: Send to multiple addresses and exclude data from input

我有一個非常簡單的電子郵件聯系表單,我想要A)表單字段中的輸入要發送到多個地址,和B)一些數據要從所有收件人中排除,除了一個。

我已經解決了A部分; 然而,B部分被證明是困難的,因為我只是一個中級網頁設計師而且絕不是編碼專家。 找到以下源代碼:

<?php
if(isset($_POST['email'])) {

$email_to = "firstemail@email.com,secondemail@email.com,thirdemail@email.com";
$email_subject = "Your email subject line";

function died($error) {
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

if(!isset($_POST['first_name']) ||
    !isset($_POST['last_name']) ||
    !isset($_POST['business_name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['telephone']) ||
    !isset($_POST['comments'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}



$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$business_name = $_POST['business_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // 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 Comments you entered do not appear to be valid.<br />';
}

if(strlen($error_message) > 0) {
died($error_message);
}

$email_message = "Form 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 .= "Business Name: ".clean_string($business_name). "\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

Thank you for contacting us. We will be in touch with you very soon.


<?php

}
?>

再次,發送到多個地址工作,但我基本上希望讓第一封電子郵件接收表單中的所有數據,以及所有其他電子郵件地址,以接收除電子郵件中的電子郵件地址行之外的所有內容。

謝謝!

在','處爆發$ email_to,然后遍歷數組以構建內容並發送。

foreach($aMails as $sMail) {
    if($sMail == 'important@example.com') {
        //do the content stuff here
    }
    @mail($sMail, $email_subject, $email_message, $headers);
}

暫無
暫無

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

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