使用foreach的数组向用户发送电子邮件

时间:2011-08-09 12:26:45

标签: php

我之前问了一个关于此的问题,但它已经改变了,因为我想从db查询'$to'创建一个数组,并在email_to_user()函数中使用它。希望能够通过$entry->dept阵列向所有用户发送电子邮件。

$entry->dept = 1有效时,会通过department #1向用户发送电子邮件 当$entry->dept = 1,30电子邮件功能失败时。

$divisions = explode(",", $entry->dept);

foreach($divisions as $division) {
    $divs=get_record('induction_emails','id',$division);
    $useremail = get_record('user', 'email', $divs->email);
    $to = get_record('user', 'id', $useremail->id);
}

if (email_to_user($to, $from, $subject, $body)){
     redirect('thanks.php');
     die;
} 
else {}
?>

1 个答案:

答案 0 :(得分:2)

Sending mails with PHP

function email_to_user($toemail, $from, $subject, $body){
    $headers = "From: $from\r\n" .
        "Reply-To: $from\r\n" .
        "X-Mailer: PHP/" . phpversion();
    $result = true;
    foreach ($toemail as $email){
         if (!mail($email, $subject, $body, $headers)){
            $result=false;
            break;
        }
    }
    return $result;
}