2014-10-27 155 views
0

我希望將訪問者填寫的數據副本發送給我自己,並將其他副本發送給訪問者。請指教!將ajaxform數據發送到多個電子郵件地址

我的電子郵件地址是[email protected]而訪問者的電子郵件是$email

此代碼如下所示:

// Set the recipient email address. 
    // FIXME: Update this to your desired email address. 
    $recipient = "[email protected], .$email"; 

我不打提交按鈕後得到任何結果!

我將不勝感激!

<?php 
// My modifications to mailer script from: 
// http://blog.teamtreehouse.com/create-ajax-contact-form 
// Added input sanitizing to prevent injection 

// Only process POST reqeusts. 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    // Get the form fields and remove whitespace. 
    $name = strip_tags(trim($_POST["name"])); 
      $name = str_replace(array("\r","\n"),array(" "," "),$name); 
    $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL); 
    $message = trim($_POST["message"]); 

    // Check that data was sent to the mailer. 
    if (empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     // Set a 400 (bad request) response code and exit. 
     http_response_code(400); 
     echo "Oops! There was a problem with your submission. Please complete the form and try again."; 
     exit; 
    } 

    // Set the recipient email address. 
    // FIXME: Update this to your desired email address. 
    $recipient = "[email protected], .$email"; 

    // Set the email subject. 
    $subject = "New contact from $name"; 

    // Build the email content. 
    $email_content = "Name: $name\n"; 
    $email_content .= "Email: $email\n\n"; 
    $email_content .= "Message:\n$message\n"; 

    // Build the email headers. 
    $email_headers = "From: $name <$email>"; 

    // Send the email. 
    if (mail($recipient, $subject, $email_content, $email_headers)) { 
     // Set a 200 (okay) response code. 
     http_response_code(200); 
     echo "Thank You! Your message has been sent."; 
    } else { 
     // Set a 500 (internal server error) response code. 
     http_response_code(500); 
     echo "Oops! Something went wrong and we couldn't send your message."; 
    } 

} else { 
    // Not a POST request, set a 403 (forbidden) response code. 
    http_response_code(403); 
    echo "There was a problem with your submission, please try again."; 
} 

?>

+3

改用'$收件人= 「[email protected],$電子郵件」;'(去掉點'.' ) – andrew 2014-10-27 11:53:31

回答

0

你必須把myemail和遊客在電子郵件一個數組,然後運行一個循環,相同的消息會被髮送到所有的陣列。 例如

$aEmails = array(); 
$aEmails[] = '[email protected]'; 
$aEmails[] = '[email protected]'; 

foreach(aEmails AS aEmail){ 
mail($aEmail, $subject, $email_content, $email_headers); 
} 
+0

謝謝。但我需要'[email protected]'作爲一個變量。這意味着它應該是'$ email',因爲每個訪客都會有一個唯一的電子郵件。我如何使用$ email而不是[email protected]來實現? – JMWalker 2014-10-27 13:56:56

+0

然後只是提供一個表格填寫並提交或登錄該訪問者從任何其他網站如臉譜,微博等。這是唯一你可以得到他/她的電子郵件。 – 2014-10-27 14:40:35

+0

非常感謝。我剛剛取代了「$ aEmails [] ='[email protected]';」用「$ aEmails [] = $ email';」它的工作! – JMWalker 2014-10-27 14:51:32

0

如果你沒有得到任何結果,那麼你必須檢查你的HTML表單的標籤它應該有method =「POST」屬性。

然後在這裏取出點

$recipient = "[email protected], .$email"; 

這樣

$recipient = "[email protected], $email"; 
相關問題