2012-10-03 11 views
24

मेरे पास कुछ PHP कोड है जिसका उपयोग मैं एक विशिष्ट ई-मेल पते पर एक फॉर्म भेजने के लिए कर रहा हूं। हालांकि, जब मैं इसे भेजता हूं तो PHP में कुछ और ई-मेल पते शामिल करना चाहता हूं। मैं उसे कैसे कर सकता हूँ?PHP फॉर्म एकाधिक प्राप्तकर्ताओं को ईमेल भेजता है

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

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "MVP Nomination"; 


    function died($error) { 
     // your error code can go here 
     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(); 
    } 

    // validation expected data exists 
    if(!isset($_POST['username']) || 
     !isset($_POST['body'])|| 
     !isset($_POST['email'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 

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

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][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,$username)) { 
    $error_message .= 'The Username you entered does 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 .= "Name: ".clean_string($username)."\n"; 
    $email_message .= "Comments: ".clean_string($body)."\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); 
} 
header("Location: ThankYou.html"); //Redirect to Thank You HTML page after email is sent 
?>​ 

धन्यवाद।

+0

उपयोग http://codepad.viper-7.com के लिए एक foreach चलाते हैं। – Leri

+1

@PLB - StackOverflow की कोड विशेषताएं इसके लिए बिल्कुल सही हैं। बाहरी साइट का उपयोग करने का कोई कारण नहीं है। – Buggabill

+0

क्या आप पूछने से पहले खोज करने की कोशिश की? http://stackoverflow.com/search?q=php+multiple+email+addresses+ – Buggabill

उत्तर

66

यह काम करेगा:

$email_to = "[email protected],[email protected],[email protected]"; 
+0

अन्य उत्तरों मैंने देखा है कि अल्पविराम के बाद ईमेल के बीच एक जगह डाल दी गई है। यह केवल एकमात्र है जिसे मैंने बिना देखा है। मुझे लगता है कि यह बेहतर है? क्या 2 तरीकों के बीच मतभेद हैं? – Jon

+0

हेडर.आईट में अन्य ईमेल कैसे छिपाने के लिए रिसीवर ईमेल पते – smehsoud

+0

@smehsoud पर सभी ईमेल पते दिखाएंगे यह एक अलग सवाल है लेकिन आप हेडर में बस एक बीसीसी लाइन जोड़ सकते हैं। –

8

आप प्रतिलिपि या गुप्त प्रतिलिपि के रूप में ईमेल जोड़ने की जरूरत है, तो जोड़ने की कोशिश वेरिएबल में निम्न भाग जो आप अपने हेडर के लिए उपयोग करते हैं:

$headers .= "CC: [email protected]".PHP_EOL; 
$headers .= "BCC: [email protected]".PHP_EOL; 

सादर

+1

इष्टतम क्रॉस-प्लेटफ़ॉर्म संगतता के लिए, आप \ r \ n के बजाय PHP_EOL का उपयोग करना चाह सकते हैं। –

+0

मैंने '\ r \ n' को' PHP_EOL' द्वारा बदल दिया है क्योंकि इवान डोनोवन ने वास्तव में ध्यान दिया था। – MTranchant

4

आप उन्हें अल्पविराम (,) से अलग करते हुए $email_to चर करने के लिए अपने receipients जोड़ सकते हैं। या आप हेडर में नए फ़ील्ड जोड़ सकते हैं, अर्थात् CC: या BCC: और अपने प्राप्तकर्ता वहां रखें। BCC सबसे अधिक अनुशंसित

7

नीचे दिए गए अल्पविराम से अलग मूल्यों का उपयोग करें।

$email_to = 'Mary <[email protected]>, Kelly <[email protected]>'; 
@mail($email_to, $email_subject, $email_message, $headers); 

या php कोड के लिए ईमेल पता

//list of emails in array format and each one will see their own to email address 
$arrEmail = array('Mary <[email protected]>', 'Kelly <[email protected]>'); 

foreach($arrEmail as $key => $email_to) 
    @mail($email_to, $email_subject, $email_message, $headers); 
संबंधित मुद्दे

 संबंधित मुद्दे