2012-08-11 63 views
1

我想調整下面的代碼,以便電子郵件發送到2個地址,而不是一個。我試過這個「sendto =」[email protected][email protected]「」沒有成功(msg不發送)。任何想法?我的託管公司1 & 1(只認購所以假設運行最新的PHP版本)適應php發送郵件腳本發送消息到多個地址

這裏是原單碼:

<?php 
$sendto = "[email protected]"; 
$usermail = $_POST['email']; 
$username = $_POST['name']; 
$content = nl2br($_POST['msg']); 

$subject = "New Message from domain.com"; 
$headers = "From: " . strip_tags($usermail) . "\r\n"; 
$headers .= "Reply-To: ". strip_tags($usermail) . "\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: text/html;charset=utf-8 \r\n"; 

$msg = "<html><body style='font-family:Arial,sans-serif;'>"; 
$msg .= "<h2 style='font-weight:bold;border-bottom:1px dotted #ccc;'>New message</h2>\r\n"; 
$msg .= "<p><strong>From:</strong> ".$username."</p>\r\n"; 
$msg .= "<p><strong>E-mail:</strong> ".$usermail."</p>\r\n"; 
$msg .= "<p><strong>Message:</strong> ".$content."</p>\r\n"; 
$msg .= "</body></html>"; 


if(@mail($sendto, $subject, $msg, $headers)) { 
    echo "true"; 
} else { 
    echo "false"; 
} 

?> 

和JavaScript:

// Contact form 
function validateEmail(email) { 
     var reg = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
     return reg.test(email); 
    } 

    $(document).ready(function() { 
     $(".modalbox").fancybox(); 
     $("#contact").submit(function() { return false; }); 


     $("#send").on("click", function(){ 
      var emailval = $("#email").val(); 
      var msgval = $("#msg").val(); 
      var msglen = msgval.length; 
      var mailvalid = validateEmail(emailval); 
      var nameval = $("#name").val(); 

      if(mailvalid == false) { 
       $("#email").addClass("error"); 
      } 
      else if(mailvalid == true){ 
       $("#email").removeClass("error"); 
      } 

      if(msglen < 4) { 
       $("#msg").addClass("error"); 
      } 
      else if(msglen >= 4){ 
       $("#msg").removeClass("error"); 
      } 

      if(nameval < 2) { 
      //name must be at least 2 characters 
        $("#name").addClass("error"); 
      } 
      else if(nameval >= 2){ 
        $("#name").removeClass("error"); 
      } 

      if(mailvalid == true && msglen >= 4) { 
       // if both validate we attempt to send the e-mail 
       // first we hide the submit btn so the user doesnt click twice 
       $("#send").replaceWith("<em>sending...</em>"); 

       $.ajax({ 
        type: 'POST', 
        url: '../sendmessage.php', 
        data: $("#contact").serialize(), 
        success: function(data) { 
         if(data == "true") { 
          $("#contact").fadeOut("fast", function(){ 
           $(this).before("<p><strong>Success! Your message has been sent, thank you.</strong></p>"); 
           setTimeout("$.fancybox.close()", 1000); 
          }); 
         } 
        } 
       }); 
      } 
     }); 
    }); 

回答

0

創建array與地址和使用foreach loop發送郵件給每一個。

增加: 創建地址數組並循環:

$addresses = array([email protected],[email protected],[email protected]); 
foreach($addresses as $address) { 
if(@mail($address, $subject, $msg, $headers)) { 
    echo "true"; 
} else { 
    echo "false"; 
} 
} 

或更短:

$addresses = array([email protected],[email protected],[email protected]); 
foreach($addresses as $address) { 
    echo (@mail($address, $subject, $msg, $headers)) ? 'true' : 'false'; 
} 
+0

嗨 - 我如何適應當前的代碼來做到這一點? (我的PHP水平非常差)?非常感謝 – Greg 2012-08-12 09:29:20

+0

@Greg回答更新; – 2012-08-12 13:14:57

1

逗號應根據文檔[http://php.net/manual/工作en/function.mail.php],所以其他的東西一定是錯的。嘗試刪除@並查看是否收到錯誤消息。你使用的是什麼版本的PHP?

+0

嗨 - 試圖刪除@並獲得與2個地址相同的結果...消息「發送...」在我的表單下,但沒有發送。這是一個新的1&1帳戶,所以我假設我正在運行最新版本的PHP。 – Greg 2012-08-11 21:21:49