3c06ecd842092830b35f4a5649acc3d5780ca272
[atutor.git] / docs / include / classes / phpmailer / atutormailer.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* http://atutor.ca                                                     */
7 /*                                                                      */
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 /************************************************************************/
12 // $Id$
13
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
15
16 require(dirname(__FILE__) . '/class.phpmailer.php');
17
18 /**
19 * ATutorMailer extends PHPMailer and sets all the default values
20 * that are common for ATutor.
21 * @access  public
22 * @see     include/classes/phpmailer/class.phpmailer.php
23 * @since   ATutor 1.4.1
24 * @author  Joel Kronenberg
25 */
26 class ATutorMailer extends PHPMailer {
27
28         /**
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.
31         * @access  public
32         * @since   ATutor 1.4.1
33         * @author  Joel Kronenberg
34         */
35         function ATutorMailer() {
36                 if (MAIL_USE_SMTP) {
37                         $this->IsSMTP(); // set mailer to use SMTP
38                         $this->Host = ini_get('SMTP');  // specify main and backup server
39                 } else {
40                         $this->IsSendmail(); // use sendmail
41                         $this->Sendmail = ini_get('sendmail_path');
42                 }
43
44                 $this->SMTPAuth = false;  // turn on SMTP authentication
45                 $this->IsHTML(false);
46
47                 // send the email in the current encoding:
48                 global $myLang;
49                 $this->CharSet = $myLang->getCharacterSet();
50         }
51
52         /**
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.
58         * @access  public
59         * @return  boolean      whether or not the mail was sent (or queued) successfully.
60         * @see     parent::send()
61         * @since   ATutor 1.4.1
62         * @author  Joel Kronenberg
63         */
64         function Send() {
65                 global $_config;
66
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'];
72                 }
73
74                 $this->Body .= "\n"._AT('atutor_home').': http://atutor.ca';
75
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) {
79                         global $db;
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));
82                         }
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));
85                         }
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));
88                         }
89                         return true;
90                 } else {
91                         return parent::Send();
92                 }
93         }
94
95         /**
96         * Adds the mail to the queue.
97         * @access private
98         * @return boolean whether the mail was queued successfully.
99         * @since  ATutor 1.5.3
100         * @author Joel Kronenberg
101         */
102         function QueueMail($to_email, $to_name, $from_email, $from_name, $subject, $body) {
103                 global $db;
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);
106         }
107
108         /**
109         * Sends all the queued mail. Called by ./admin/cron.php.
110         * @access public
111         * @return void
112         * @since ATutor 1.5.3
113         * @author Joel Kronenberg
114         */
115         function SendQueue() {
116                 global $db;
117
118                 $mail_ids = '';
119                 $sql = "SELECT * FROM ".TABLE_PREFIX."mail_queue";
120                 $result = mysql_query($sql, $db);
121                 while ($row = mysql_fetch_assoc($result)) {
122                         $this->ClearAllRecipients();
123
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'];
130
131                         parent::Send();
132
133                         $mail_ids .= $row['mail_id'].',';
134                 }
135                 if ($mail_ids) {
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);
139                 }
140         }
141
142 }
143
144 ?>