Last active
August 29, 2015 14:03
-
-
Save outflux3/797a18f5d2eaf0a87efb to your computer and use it in GitHub Desktop.
scheduled mail with processwire
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; +-4 minutes seems to work well...cron runs every 5 min | |
$minus4 = date('U', strtotime('-4 minutes')); | |
$plus4 = date('U', strtotime('+4 minutes')); | |
$messages = $pages->find("template=email, send_date>$minus4, send_date<$plus4"); | |
foreach($messages as $message) { | |
$placeholders = array('class="align_left"', '/site/assets/files/'); | |
$replacements = array('style="float:left;margin-right:10px;"', $base_url . '/site/assets/files/'); | |
$body = str_replace($placeholders, $replacements, $message->body); | |
if($message->boilerplate_select->count()) { | |
foreach($message->boilerplate_select as $bp) { | |
$body .= $bp->body; | |
} | |
} | |
$signature = str_replace('/site/assets/files/', $base_url . '/site/assets/files/', $message->identity_select->signature); | |
$body .= $signature; | |
$bodyPlain = $message->body_plain; | |
$bodyPlain .= $message->identity_select->signature_plain; | |
if( $message->attachments->count() || $message->attached_documents->count() ) { | |
$body .= '<p>This message contains the following attachments:</p><ul>'; | |
if($message->attachments->count()) { | |
foreach($message->attachments as $att) { | |
$body .= '<li>' . $att->name . '</li>'; | |
} | |
} | |
if($message->attached_documents->count()) { | |
foreach($message->attached_documents as $att) { | |
$body .= '<li>' . $att->title . '</li>'; | |
} | |
} | |
$body .= '</ul>'; | |
} // end if attachments, for visual list. | |
$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->body($bodyPlain); | |
$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 foreach |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment