Last active
August 29, 2015 14:04
-
-
Save outflux3/530b8adb1a6b7019d262 to your computer and use it in GitHub Desktop.
Email with multi-sending schedule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // messages | |
// Set Defaults | |
$base_path = '/server/path/to/pwsite'; | |
$base_url = 'base-http-url-here'; | |
$now = date("U"); // current date | |
//Setup Time Range, Send all messages in range: | |
$minus4 = date('U', strtotime('-4 minutes')); | |
$plus4 = date('U', strtotime('+4 minutes')); | |
$emailsRecurring = $pages->find("template=email-recurring"); | |
foreach($emailsRecurring as $message) { | |
if($message->sending_schedule->count()) { | |
foreach($message->sending_schedule as $row) { | |
// the date of the row | |
$row->send_time = date('U', strtotime($row->date)); | |
// if it has an offset: | |
if($row->offset) { | |
$row->send_time = date ('U', strtotime ( '-' . $row->offset . 'day' . $row->date ) ); | |
} | |
################## | |
# TESTING BLOCK | |
################## | |
//echo date('Y-m-d h:i', $row->send_time); | |
//echo $minus4 . ' -> ' . $row->send_time . ' <- ' . $plus4; | |
// check to see if the message is in range | |
if ( ($row->send_time > $minus4) && ($row->send_time < $plus4) ) { | |
// mail logic here | |
$placeholders = array('{{duedate}}', '{{custom}}'); | |
$dueDate = date('m-d-Y', strtotime($row->date) ); | |
$replacements = array($dueDate, $row->custom); | |
$body = str_replace($placeholders, $replacements, $message->body); | |
$signature = str_replace('/site/assets/files/', $base_url . '/site/assets/files/', $message->identity_select->signature); | |
$body .= $signature; | |
$title = $row->offset ? str_replace('offset', $row->offset, $message->title) : $message->title; | |
$mail = wireMail(); | |
$mail->from($message->identity_select->identity_name . ' <' . $message->identity_select->email . '>'); | |
// Recipients | |
if($message->recipients->count()) { | |
$recipients = array(); | |
foreach($message->recipients as $recipient) { | |
$recipients[] = $recipient->title . ' <' . $recipient->email . '>'; | |
} | |
$mail->to($recipients); | |
} | |
// Subject & Body | |
//$mail->subject($message->title); | |
$mail->subject($title); | |
$bodyHtml = '<html>' . $body . '</html>'; | |
$mail->bodyHTML($bodyHtml); | |
if($message->attachments->count()) { | |
foreach($message->attachments as $att) { | |
$mail->attachment($base_path . $att->url); | |
} | |
} | |
if($message->attached_documents->count()) { | |
foreach($message->attached_documents as $att) { | |
$mail->attachment($base_path . $att->file->url); | |
} | |
} | |
// Send | |
$sendLog = 'Number Sent: '; | |
$sendLog .= $mail->send(); | |
// Logging | |
$email_log = $message->email_log; | |
$email_log .= "\n". $sendLog; | |
$message->email_log = $email_log; | |
$message->of(false); | |
$message->save(); | |
} // end if message is in range | |
} // end foreach rows | |
} // end if count sending_schedule | |
} // end foreach $email |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment