多次发送电子邮件而不重复电子邮件代码

时间:2014-08-27 02:03:35

标签: php email

我有6个基本数组,分为2组,每组3个,所以每个数组都包含名称,dob,id,email,主要的想法是根据生日发送和发送电子邮件,如果是用户生日是在2周内发送电子邮件1,如果在2天内发送电子邮件2 ...以下是我用来发送文件并为报告构建最终数组......

    foreach ($mno as $ko=>$vo) {
        $m_cump = date('m-d', strtotime($vo['dob']));
        // The birthday is TODAY!
        if($today == $m_cump) { $td_dob[]= array('nombre'=>$vo['name'],'resp_id'=>$vo['resp_id'],'email'=>$vo['email'],'dob'=>$vo['dob']);  
        // Here is the bulky code for the email, is huge since I use HTML for the emails
}
        // The birthday is in 2 days
        if($tds == $m_cump) {$dob_in_2_days[]= array('nombre'=>$vo['name'],'resp_id'=>$vo['resp_id'],'email'=>$vo['email'],'dob'=>$vo['dob']);
      // Here is the bulky code for the email, is huge since I use HTML for the emails
    }
        // The birthday is in 2 weeks
        if($tws == $m_cump) {$dob_in_2_weeks[]= array('nombre'=>$vo['name'],'resp_id'=>$vo['resp_id'],'email'=>$vo['email'],'dob'=>$vo['dob']); 
        // Here is the bulky code for the email, is huge since I use HTML for the emails
        }
    }

因为你可以看到我必须为FOREACH()中的每个IF重复电子邮件的代码......我不知道我是否可以调用函数并发送正确的电子邮件.. {{1或类似的方式我可以构建电子邮件,然后我可以重复使用它而无需重复整个代码......我不擅长构建函数,这就是我需要你帮助的原因。

2 个答案:

答案 0 :(得分:0)

为什么不在代码上使用else?因此,当条件出现时,它将从循环中退出。另外,如果你不需要循环内的$ko变量,我宁愿不使用它。

<?php 

foreach ($mno as $vo)
{
    $m_cump = date('m-d', strtotime($vo['dob']));
    // The birthday is TODAY!
    if ($today == $m_cump)
    {
        $td_dob[] = array('nombre' => $vo['name'], 'resp_id' => $vo['resp_id'], 'email' => $vo['email'], 'dob' => $vo['dob']);
    }
    // The birthday is in 2 days
    elseif ($tds == $m_cump)
    {
        $dob_in_2_days[] = array('nombre' => $vo['name'], 'resp_id' => $vo['resp_id'], 'email' => $vo['email'], 'dob' => $vo['dob']);   
    }
    // The birthday is in 2 weeks
    elseif ($tws == $m_cump)
    {
        $dob_in_2_weeks[] = array('nombre' => $vo['name'], 'resp_id' => $vo['resp_id'], 'email' => $vo['email'], 'dob' => $vo['dob']);
    }
}

function sending_email($data = array(), $type = '')
{
    switch ($type)
    {
        case 'today':
            //...
            break;
        case 'in_2_days' :
            //...
            break;
        case 'in_2_weeks':
            //...
            break;

        default:
            break;
    }   
}

答案 1 :(得分:0)

在该文件或包含文件中创建电子邮件功能,并将必要的变量传递给它。

基本示例:

function SendEmail($name, $email, $when){
$headers = ""; // your email headers
if ( $when == "TODAY!" ) {
$subject = "$name, happy birthday!!!";
$message = "Hey $name, \n\n We just wanted to say HAPPY BIRTHDAY!!!";
} else {
$subject = "$name, your birthday is in $when!";
$message = "$name, we just wanted to remind you, your birthday is in $when! Can't wait :)";
}
mail($email, $subject, $message, $headers);
}

然后,只要您需要发送电子邮件,只需执行以下操作:

SendEmail("$vo['name']", "$vo['email']", "TODAY!!");