/** * Processes (un)subscription requests to multiple lists * @param string $address The email address * @param array $list_ids The ids of the lists * @param bool $subscribe TRUE if addresss should be subscribed to the lists, FALSE if it should be unsubscribed * @return bool TRUE if operation was successful, else FALSE */ function gu_subscription_process($address, &$list_ids, $subscribe) { if (!check_email($address)) { return gu_error(t("Invalid email address")); } $succ_list_names = array(); $fail_list_names = array(); // For each list we need to load it with all addresses foreach ($list_ids as $list_id) { $list = gu_list::get($list_id, TRUE); // Don't allow subscriptions to private lists if ($list->is_private()) { $res = FALSE; } else { if ($subscribe) { $res = $list->add($address, TRUE); } else { $res = $list->remove($address, TRUE); } } if ($res) { $succ_list_names[] = $list->get_name(); } else { $fail_list_names[] = $list->get_name(); } } // Check if there were any successful if (count($succ_list_names) < 1) { return FALSE; } // Work out if we need to send any emails now, and if so create a sender if (gu_config::get('list_send_welcome') || gu_config::get('list_send_goodbye') || gu_config::get('list_subscribe_notify') || gu_config::get('list_unsubscribe_notify')) { $mailer = new gu_mailer(); if ($mailer->init()) { $subject_prefix = count($succ_list_names) == 1 ? $succ_list_names[0] : gu_config::get('collective_name'); // Send welcome / goodbye message if ($subscribe && gu_config::get('list_send_welcome') || !$subscribe && gu_config::get('list_send_goodbye')) { $subject = '[' . $subject_prefix . '] ' . ($subscribe ? t('Subscription') : t('Unsubscription')) . t(' confirmation'); $action = $subscribe ? t('subscribed to') : t('unsubscribed from'); $text = t("This is an automated message to confirm that you have been % the following lists:", array($action)) . "\n\n* " . implode("\n* ", $succ_list_names) . "\n\n"; $text .= t('To change your subscriptions visit: ') . absolute_url('subscribe.php') . '?addr=' . $address . "\n\n"; $text .= t('Please do not reply to this message. Thank you.'); $mailer->send_mail($address, $subject, $text); } // Send admin notifications if ($subscribe && gu_config::get('list_subscribe_notify') || !$subscribe && gu_config::get('list_unsubscribe_notify')) { $subject = '[' . $subject_prefix . '] ' . ($subscribe ? t('Subscription') : t('Unsubscription')) . t(' notification'); $action = $subscribe ? t('subscribed to') : t('unsubscribed from'); $text = t("This is an automated message to notify you that % has been % the following lists:", array($address, $action)) . "\n\n* " . implode("\n* ", $succ_list_names) . "\n\n"; $mailer->send_admin_mail($subject, $text); } } } $action = $subscribe ? t('subscribed to') : t('unsubscribed from'); return gu_success(t('You have been % lists: <i>%</i>', array($action, implode('</i>, <i>', $succ_list_names)))); }
*/ /* Gutama plugin package * @version 1.6 * @date 01/10/2013 * @author Cyril MAGUIRE */ include_once 'inc/gutuma.php'; include_once 'inc/newsletter.php'; include_once 'inc/mailer.php'; // Initialize Gutuma without validation or redirection gu_init(FALSE, FALSE); // Get all newsletters in the outbox $mailbox = gu_newsletter::get_mailbox(); if ($mailbox == FALSE || !isset($mailbox['outbox'])) { die(utf8_decode(t('Unable to access mailbox'))); } // Create mailer $mailer = new gu_mailer(); if (!$mailer->init()) { die(utf8_decode(t('Unable to initialize mailer'))); } // Start timer $start_time = time(); // Process outbox foreach ($mailbox['outbox'] as $newsletter) { $newsletter->send_batch($mailer, $start_time); // Check batch time limit if (time() - $start_time > (int) gu_config::get('batch_time_limit')) { break; } }
function gu_sender_test() { // Get current settings, which may not have been saved $use_smtp = is_post_var('use_smtp'); $smtp_server = get_post_var('smtp_server'); $smtp_port = (int) get_post_var('smtp_port'); $smtp_encryption = get_post_var('smtp_encryption'); $smtp_username = get_post_var('smtp_username'); $smtp_password = get_post_var('smtp_password'); $use_sendmail = is_post_var('use_sendmail'); $use_phpmail = is_post_var('use_phpmail'); if (!($use_smtp || $use_sendmail || $use_phpmail)) { return gu_error(t('No method of mail transportation has been configured')); } $test_msg = t('If you have received this email then your settings clearly work!'); // Test SMTP settings first if ($use_smtp) { $mailer = new gu_mailer(); if ($mailer->init(TRUE, $smtp_server, $smtp_port, $smtp_encryption, $smtp_username, $smtp_password, FALSE, FALSE)) { if (!$mailer->send_admin_mail('[' . gu_config::get('collective_name') . '] Testing SMTP', $test_msg)) { return gu_error(t('Unable to send test message using SMTP')); } } else { return gu_error(t('Unable to initialize mailer with the SMTP settings')); } $mailer->disconnect(); } // Test Sendmail next if ($use_sendmail) { $mailer = new gu_mailer(); if ($mailer->init(FALSE, '', '', '', '', '', FALSE, FALSE)) { if (!$mailer->send_admin_mail('[' . gu_config::get('collective_name') . '] Testing Sendmail', $test_msg)) { return gu_error(t('Unable to send test message using Sendmail')); } } else { return gu_error(t('Unable to initialize mailer with Sendmail')); } $mailer->disconnect(); } // Test PHP mail next if ($use_phpmail) { $mailer = new gu_mailer(); if ($mailer->init(FALSE, '', '', '', '', '', FALSE, TRUE)) { if (!$mailer->send_admin_mail('[' . gu_config::get('collective_name') . '] Testing PHP mail', $test_msg)) { return gu_error(t('Unable to send test message using PHP mail')); } } else { return gu_error(t('Unable to initialize mailer with PHP mail')); } $mailer->disconnect(); } gu_success(t('Test messages sent to <br><i>%</i></b>', array(gu_config::get('admin_email')))); }
if ($newsletter->get_recipients() == '' && is_get_var('list')) { foreach ($lists as $list) { $list_id = (int) get_get_var('list'); if ($list->get_id() == $list_id) { $newsletter->set_recipients($list->get_name()); break; } } } // Preview the newsletter $preview_mode = is_post_var('preview_submit'); // Send the newsletter if (is_post_var('send_submit')) { // Saves newsletter to outbox if ($newsletter->send_prepare()) { $mailer = new gu_mailer(); if ($mailer->init()) { if ($newsletter->send_batch($mailer)) { if ($newsletter->is_sending()) { gu_success(t('Newsletter sent to first batch of recipients')); } else { gu_success(t('Newsletter sent to all recipients')); } } } $newsletter = new gu_newsletter(); $is_modified = FALSE; } } elseif (is_post_var('attach_submit') && $_FILES['attach_file']['name'] != '') { if ($newsletter->store_attachment($_FILES['attach_file']['tmp_name'], $_FILES['attach_file']['name'])) { gu_success(t('Attachment <b><i>%</i></b> added', array($_FILES['attach_file']['name'])));
/** * Newsletters often can't be sent to all recipients in one batch, so this function * picks up where it left off last, and sends as much as permitted by the batch settings. * @param gu_mailer $mailer The mailer to use to send * @param int $init_start_time If this isn't the first call to send_batch in this script execution * then this should be the start time of the first call, else NULL * @return TRUE if operation was successful, else FALSE */ public function send_batch(gu_mailer $mailer, $init_start_time = NULL) { $this->acquire_lock(); $dir = $this->get_dir(); // Newsletter may have been deleted by the process that blocked this process, or may not be ready for sending if (!file_exists($dir . '/' . RECIPIENTS_FILE)) { $this->release_lock(); return TRUE; } // Open recipient list file $fh = @fopen($dir . '/' . RECIPIENTS_FILE, 'r+'); if ($fh == FALSE) { return gu_error(t('Unable to open newsletter recipient file'), ERROR_EXTRA); } if (!flock($fh, LOCK_EX)) { return gu_error(t('Unable to lock newsletter recipient list'), ERROR_EXTRA); } fgets($fh); // Read file marker $header = explode('|', fgets($fh)); // Read header $remaining = $header[0]; $total = $header[1]; // Start the timer - use the passed start time value if there was one $start_time = isset($init_start_time) ? $init_start_time : time(); // Collect failed recipients $failed_recipients = array(); $total_sent = 0; // Start sending to recipients while (!feof($fh)) { $line = trim(fgets($fh)); if (strlen($line) == 0) { break; } $tokens = explode('|', $line); $address = $tokens[0]; $list = $tokens[1]; $res = $mailer->send_newsletter($address, $this, $list); if ($res === FALSE) { return FALSE; } elseif ($res === -1) { $failed_recipients[] = $address . ($list != '' ? ' (' . $list . ')' : ''); } $total_sent++; if (time() - $start_time > (int) gu_config::get('batch_time_limit') || $total_sent >= gu_config::get('batch_max_size')) { break; } } // Read remaining recipients $remaining_recipients = array(); while (!feof($fh)) { $line = trim(fgets($fh)); if (strlen($line) > 0) { $remaining_recipients[] = explode('|', $line); } } // Update recipient list file fseek($fh, 0); ftruncate($fh, 0); fwrite($fh, FILE_MARKER); fwrite($fh, count($remaining_recipients) . '|' . $total); foreach ($remaining_recipients as $recip) { fwrite($fh, implode('|', $recip) . "\n"); } fclose($fh); if (count($remaining_recipients) == 0) { // Delete recipients file so when we unlock, waiting processes will detect its gone and not try sending @unlink($dir . '/' . RECIPIENTS_FILE); // Wakeup waiting processes $this->release_lock(); if (!$this->delete()) { return FALSE; } } else { $this->release_lock(); } if (count($failed_recipients) > 0) { $extra = t('Unable to deliver to:<br /><br />') . implode('<br />', $failed_recipients); return gu_error(t('Message could not be sent to all recipients'), $extra); } return TRUE; }