Example #1
0
function email_send($format, $subject, $to = array(), $data = array())
{
    //sends an email
    /*
    $format: register, account, etc. This is combined to make a file name. ie. register.html
    $to: array of who you are sending this to - array('email' => 'first last name')
    $data: array of data to replace - array('{{%REPLACE_ME%}}' => 'Replacing with this')
    */
    $mail = email_init();
    //init email
    //format variables
    if (empty($format)) {
        return false;
    }
    //no email format specified
    if (!isset($subject)) {
        $subject = 'Hello from Planling!';
    }
    if (empty($to)) {
        return false;
    }
    //no to set
    //load html email base
    $tbase = file_get_contents(ROOT_PATH . '/includes/emails/base.html');
    //load html email content base
    $tcontent = file_get_contents(ROOT_PATH . '/includes/emails/' . $format . '.html');
    //swap keywords in content file
    foreach ($data as $x => $y) {
        $tcontent = str_replace($x, $y, $tcontent);
    }
    //swap keywords in base file
    $body = $tbase;
    $body = str_replace('{{%CONTENT%}}', $tcontent, $body);
    //send email
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->WordWrap = 50;
    $mail->MsgHTML($body);
    $mail->IsHTML(true);
    //send to each address
    foreach ($to as $e => $n) {
        //add address
        $mail->AddAddress($e, $n);
        //send mail
        if (!$mail->Send()) {
            //fail
        } else {
            //success
        }
        //clear addresses
        $mail->ClearAddresses();
    }
}
function send_daily_birthday_alert(Member $member)
{
    $template = file_get_contents(getcwd() . '/scripts/email_templates/cron_daily.php');
    $mail = email_init();
    $mail->Subject = "Happy Birthday " . $member->first_name . " - Online Birthday Manager";
    $mail->addAddress($member->email);
    foreach ($member->birthday_members as $member_id) {
        $mail->addCC(get_team_member_email_by_id($member_id));
    }
    $body = str_replace("{first_name}", $member->first_name, $template);
    $mail->Body = $body;
    if ($mail->send()) {
        error_log("email sent");
    } else {
        error_log("mail not sent");
        error_log($mail->ErrorInfo);
    }
}