我想调整下面的代码,以便将电子邮件发送到2个地址而不是1个地址。我试过这个“sendto =”myaddress1 @ gmail.com,myaddress2 @ gmail.com“”没有成功(msg没有收到)。任何的想法?我的托管公司是1& 1(只是订阅所以假设运行最新版本的PHP)
以下是原始代码:
<?php
$sendto = "myaddress@gmail.com";
$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 = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[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 :(得分:1)
逗号应该根据文档[http://php.net/manual/en/function.mail.php]工作,所以其他东西一定是错的。尝试删除@并查看是否收到错误消息。您使用的是哪个版本的PHP?
答案 1 :(得分:0)
使用地址创建array并使用foreach loop将邮件发送给每个地址。
增加: 创建带地址的数组并执行循环:
$addresses = array(user1@example.com,user2@example.com,user3@example.com);
foreach($addresses as $address) {
if(@mail($address, $subject, $msg, $headers)) {
echo "true";
} else {
echo "false";
}
}
或更短:
$addresses = array(user1@example.com,user2@example.com,user3@example.com);
foreach($addresses as $address) {
echo (@mail($address, $subject, $msg, $headers)) ? 'true' : 'false';
}