1

I'd like to adapt the code below so that the e-mail are sent to 2 addresses instead of one. I tried this "sendto = "myaddress1@gmail.com, myaddress2@gmail.com"" without success (msg don't get sent). Any idea? My hosting company is 1&1 (just subscribed so assuming running a recent version of php)

Here is the orginal code:

<?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";
}

?>

And the 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);
                            });
                        }
                    }
                });
            }
        });
    });
4

2 回答 2

1

Commas should work according to the documentation [http://php.net/manual/en/function.mail.php], so something else must be wrong. Try removing the @ and see if you get an error message. What version of PHP are you using?

于 2012-08-11T20:28:25.463 回答
0

Create array with addresses and use foreach loop to send the mail to each one.

Addition: Create array with addresses and do 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";
}
}

Or shorter:

$addresses = array(user1@example.com,user2@example.com,user3@example.com);
foreach($addresses as $address) {
    echo (@mail($address, $subject, $msg, $headers)) ? 'true' : 'false';
}
于 2012-08-11T20:24:42.377 回答