2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
16 require(dirname(__FILE__) . '/class.phpmailer.php');
19 * ATutorMailer extends PHPMailer and sets all the default values
20 * that are common for ATutor.
22 * @see include/classes/phpmailer/class.phpmailer.php
24 * @author Joel Kronenberg
26 class ATutorMailer extends PHPMailer {
29 * The constructor sets whether to use SMTP or Sendmail depending
30 * on the value of MAIL_USE_SMTP defined in the config.inc.php file.
33 * @author Joel Kronenberg
35 function ATutorMailer() {
37 $this->IsSMTP(); // set mailer to use SMTP
38 $this->Host = ini_get('SMTP'); // specify main and backup server
40 $this->IsSendmail(); // use sendmail
41 $this->Sendmail = ini_get('sendmail_path');
44 $this->SMTPAuth = false; // turn on SMTP authentication
47 // send the email in the current encoding:
49 $this->CharSet = $myLang->getCharacterSet();
53 * Appends a custom ATutor footer to all outgoing email then sends the email.
54 * If mail_queue is enabled then instead of sending the mail out right away, it
55 * places it in the database and waits for the cron to send it using SendQueue().
56 * The mail queue does not support reply-to, or attachments, and converts all BCCs
57 * to regular To emails.
59 * @return boolean whether or not the mail was sent (or queued) successfully.
62 * @author Joel Kronenberg
67 // attach the ATutor footer to the body first:
68 $this->Body .= "\n\n".'----------------------------------------------'."\n";
69 $this->Body .= _AT('sent_via_atutor', AT_BASE_HREF);
70 if ($_SESSION['course_id'] > 0) {
71 $this->Body .= 'login.php?course='.$_SESSION['course_id'].' | ' . $_SESSION['course_title'];
74 $this->Body .= "\n"._AT('atutor_home').': http://atutor.ca';
76 // if this email has been queued then don't send it. instead insert it in the db
77 // for each bcc or to or cc
78 if ($_config['enable_mail_queue'] && !$this->attachment) {
80 for ($i = 0; $i < count($this->to); $i++) {
81 $this->QueueMail(addslashes($this->to[$i][0]), addslashes($this->to[$i][1]), addslashes($this->From), addslashes($this->FromName), addslashes($this->Subject), addslashes($this->Body));
83 for($i = 0; $i < count($this->cc); $i++) {
84 $this->QueueMail(addslashes($this->cc[$i][0]), addslashes($this->cc[$i][1]), addslashes($this->From), addslashes($this->FromName), addslashes($this->Subject), addslashes($this->Body));
86 for($i = 0; $i < count($this->bcc); $i++) {
87 $this->QueueMail(addslashes($this->bcc[$i][0]), addslashes($this->bcc[$i][1]), addslashes($this->From), addslashes($this->FromName), addslashes($this->Subject), addslashes($this->Body));
91 return parent::Send();
96 * Adds the mail to the queue.
98 * @return boolean whether the mail was queued successfully.
100 * @author Joel Kronenberg
102 function QueueMail($to_email, $to_name, $from_email, $from_name, $subject, $body) {
104 $sql = "INSERT INTO ".TABLE_PREFIX."mail_queue VALUES (NULL, '$to_email', '$to_name', '$from_email', '$from_name', '".addslashes($this->CharSet)."', '$subject', '$body')";
105 return mysql_query($sql, $db);
109 * Sends all the queued mail. Called by ./admin/cron.php.
112 * @since ATutor 1.5.3
113 * @author Joel Kronenberg
115 function SendQueue() {
119 $sql = "SELECT * FROM ".TABLE_PREFIX."mail_queue";
120 $result = mysql_query($sql, $db);
121 while ($row = mysql_fetch_assoc($result)) {
122 $this->ClearAllRecipients();
124 $this->AddAddress($row['to_email'], $row['to_name']);
125 $this->From = $row['from_email'];
126 $this->FromName = $row['from_name'];
127 $this->CharSet = $row['char_set'];
128 $this->Subject = $row['subject'];
129 $this->Body = $row['body'];
133 $mail_ids .= $row['mail_id'].',';
136 $mail_ids = substr($mail_ids, 0, -1); // remove the last comma
137 $sql = "DELETE FROM ".TABLE_PREFIX."mail_queue WHERE mail_id IN ($mail_ids)";
138 mysql_query($sql, $db);