仅发送邮件到其中一个勾选的复选框

时间:2015-06-30 09:19:56

标签: php email checkbox phpmailer

我正在尝试通过phpmailer发送邮件,具体取决于用户勾选的复选框的值。

这是复选框值的代码:

<div class="Container">
    <div class="col-md-8">
        <div class="panel-heading">
            <p>Please select departments who should give confirmation</p>
        </div>
        <div class="form-inline clearfix" class="Check1">   
            <div class="col-md-5 checkbox">
                <input type="checkbox" name="chmail[]" value="xxx@xxx.com"?> />Head of Operations
            </div>
        </div>
        <div class="form-inline clearfix" class="Check1">   
            <div class="col-md-5 checkbox">     
                <input type="checkbox" name="chmail[]" value="xxx@xxx.com"?> />Head of Credit
            </div>
        </div>
        <div class="form-inline clearfix" class="Check1">   
            <div class="col-md-5 checkbox">     
                <input type="checkbox" name="chmail[]" value="xxx@xxx.com"/>Head of CMU
            </div>
        </div>
        <div class="form-inline clearfix" class="Check1">
            <div class="col-md-5 checkbox">     
                <input type="checkbox" name="chmail[]" value="xxx@xxx.com"/>Head of Finance
            </div><br/>
        </div>
        <div class="form-inline clearfix" class="Check1">
            <div class="col-md-5 checkbox">     
                <input type="checkbox" name="chmail[]" value="xxx@xxx.com"/>Head of Risk/Compliance
            </div>      
        </div><br/><br/>
    </div>
</div>

这是发送电子邮件的PHP代码

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{           
    $email_body = $email_body . "<Strong>Form has been sent</strong>";  
    require_once("class.phpmailer.php");
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->SMTPAuth   = true;                
    $mail->Host       = "xxxx"; 
    $mail->SetFrom('xxxx');
    $mail->Subject    = "xxxx";
    $mail->MsgHTML($email_body);

    if(!empty($_POST['chmail'])){
        foreach($_POST['chmail'] as $selected){
            echo $selected."</br>";
        }
    }
    $mail -> AddAddress($selected); 

    if($mail->Send()) {
        echo "Your form has been submitted succesfully";
    }
    else echo "ERROR IN SENDING MAILS";

    exit;
}   

但是邮件只会转到已选中复选框的最后一个值,而不是所有选中的值。我哪里出错/我该怎么办才能发送邮件到所有检查的值?

欣赏任何建议:)提前致谢:)

1 个答案:

答案 0 :(得分:0)

$mail->AddAddress($selected);

$mail->Send();
在你的foreach里面。这应该会为您的帖子阵列上的每封电子邮件发送一封电子邮件。