/[drupal]/contributions/modules/smtp/smtp.module
ViewVC logotype

Diff of /contributions/modules/smtp/smtp.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.15.2.17, Wed Mar 19 20:13:49 2008 UTC revision 1.15.2.18, Tue Jun 17 05:27:03 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: smtp.module,v 1.15.2.15 2008/03/07 19:46:18 oadaeh Exp $  // $Id: smtp.module,v 1.15.2.17 2008/03/19 20:13:49 oadaeh Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 48  function smtp_admin_settings() { Line 48  function smtp_admin_settings() {
48    //Override the smtp_library variable.    //Override the smtp_library variable.
49    if (variable_get('smtp_on', 0)) {    if (variable_get('smtp_on', 0)) {
50      $smtp_path = drupal_get_filename('module', 'smtp');      $smtp_path = drupal_get_filename('module', 'smtp');
51      if ($smtp_path){      if ($smtp_path) {
52        variable_set('smtp_library', $smtp_path);        if (!module_exists('mimemail') || variable_get('mimemail_alter', 0) == 0) {
53            variable_set('smtp_library', $smtp_path);
54          }
55    
56        drupal_set_message(t('SMTP.module is active.'));        drupal_set_message(t('SMTP.module is active.'));
57      }      }
58      else { //If drupal can't find the path to the module.      else { //If drupal can't find the path to the module.
# Line 157  function smtp_admin_settings() { Line 160  function smtp_admin_settings() {
160   *   string subject of email.   *   string subject of email.
161   * @param $body   * @param $body
162   *   string body of message.   *   string body of message.
163   * @param $header   * @param $headers
164   *   string of header lines seperated by "\n".   *   array of headers.
165   */   */
166  function drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $header) {  function _drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $headers) {
167          $mail = new phpmailer(); //Create a new phpmailer object.          $mail = new phpmailer(); //Create a new phpmailer object.
168          $username = variable_get('smtp_username', '');          $username = variable_get('smtp_username', '');
169          $password = variable_get('smtp_password', '');          $password = variable_get('smtp_password', '');
# Line 185  function drupal_mail_wrapper($mailkey, $ Line 188  function drupal_mail_wrapper($mailkey, $
188      }      }
189    }    }
190    
191      $from_name = preg_replace("/\"(.*)\"(.*)/i", "$1", $from); // It gives: Name
192      $from      = preg_replace("/(.*)\<(.*)\>/i", "$2", $from); // It gives: name@domain.tld
193    
194    // Defines the From value to what we expect    // Defines the From value to what we expect
195    $mail->From     = "\"".$from_name ."\" <".$from .">" ;    $mail->From     = "\"".$from_name ."\" <".$from .">" ;
196    $mail->FromName = $from_name;    $mail->FromName = $from_name;
# Line 199  function drupal_mail_wrapper($mailkey, $ Line 205  function drupal_mail_wrapper($mailkey, $
205    
206    
207          //Take care of the email headers.          //Take care of the email headers.
208    foreach ($header as $key => $value) {    foreach ($headers as $key => $value) {
209      //watchdog('error', 'Key: ' . $key . ' Value: ' . $value);        $lkey = strtolower($key);
210      if (strtolower($key) == 'from') {        $lvalue = strtolower($value);
211        if ($from == NULL or $from == '') {  
212          // If a from value was already given then set based on header.        switch( $lkey ) {
213          // Should be the most common situation since drupal_mail moves the from to headers           case 'from':
214          $from           = $value;              if ($from == NULL or $from == '') {
215          $mail->From     = $value;                 // If a from value was already given then set based on header.
216          $mail->FromName = ''; // then from can be out of sync with from_name !                 // Should be the most common situation since drupal_mail moves the from to headers
217          $mail->Sender   = $value;                 $from           = $value;
218        }                 $mail->From     = $value;
219      }                 $mail->FromName = ''; // then from can be out of sync with from_name !
220      else if (strtolower($key) == 'content-type' && strpos(strtolower($value),'text/html') !== FALSE) {                 $mail->Sender   = $value;
221        $mail->IsHTML(TRUE);              }
222      }              break;
223      else if (strtolower($key) == 'content-type' && strpos(strtolower($value), 'multipart/mixed') !== FALSE) {  
224        //$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone           case 'content-type':
225        $mail->AddCustomHeader($key . ": " . $value);              if (strpos($lvalue, 'text/html') !== FALSE) {
226        $mail->message_type = "pre";                 $mail->IsHTML(TRUE);
227      }              }
228      else if (strtolower($key) == 'reply-to') {              else if ((strpos($lvalue, 'multipart/mixed') !== FALSE) || (strpos($lvalue, 'multipart/alternative') !== FALSE)) {
229        // Only add a "reply-to" if it's not the same as "return-path".                 //$body passed to smtp should already be formatted. add multipart header and tell phpmailer to leave it alone
230        if ($value != $header['Return-Path']) {                 $mail->AddCustomHeader($key . ": " . $value);
231          $mail->AddReplyTo($value);                 $mail->message_type = "pre";
232        }              }
233      }              break;
234      else if (strtolower($key) == 'return-path') {  
235        if (trim($value) !=  '') {           case 'reply-to':
236  // This is be set by SmtpSend()              // Only add a "reply-to" if it's not the same as "return-path".
237  //        $mail->Sender = $value;              if ($value != $header['Return-Path']) {
238        }                 $mail->AddReplyTo($value);
239      }              }
240      else if (strtolower($key) == 'content-transfer-encoding') {              break;
241        $mail->Encoding = $value;  
242      }           case 'return-path':
243      else if (strtolower($key) == 'mime-version') {              if (trim($value) !=  '') {
244        // just ommit MIME-Version it since it will be set by PHP-Mailer                 // This is be set by SmtpSend()
245      }                 // $mail->Sender = $value;
246      else if (strtolower($key) == 'x-mailer') {              }
247        // just ommit X-Mailer it since it will be set by PHP-Mailer              break;
248      }  
249      else if (strtolower($key) == 'errors-to') {           case 'content-transfer-encoding':
250        // just prefer to keep control on this one, it will be set by PHP-Mailer              $mail->Encoding = $value;
251      }              break;
252      else if (strtolower($key) == 'bcc') {  
253        $bccrecipients = split(",", $value);           case 'mime-version':
254        foreach ($bccrecipients as $bccrecipient) {           case 'x-mailer':
255          if ( strpos($bccrecipient, '<') !== false ) {           case 'errors-to':
256            $bccparts = explode(" <", $bccrecipient);              // ommit since it will be set by PHP-Mailer
257            $bccname = $bccparts[0];              break;
258            $bccaddr = rtrim($bccparts[1], ">");  
259          }           case 'bcc':
260          else {              $bcc_addresses = _smtp_recipients_to_address_list($value);
261            $bccname = "";  
262            $bccaddr = $bccrecipient;              foreach ($bcc_addresses as $bcc_address) {
263          }                 $mail->AddBCC($bcc_address['addr'], $bcc_address['name']);
264          $mail->AddBCC($bccaddr, $bccname);              }
265                break;
266    
267             default:
268                $mail->AddCustomHeader($key . ": " . $value);
269        }        }
270      }     }
     else { //Else the header key is not special, just add it.  
       $mail->AddCustomHeader($key . ": " . $value); //Add header line.  
     }  
   }  
271    
272  //  TODO: Delete the following commented 4 lines, as this is now done at the beginning of the function.  //  TODO: Delete the following commented 4 lines, as this is now done at the beginning of the function.
273          //If no from address has been set than use a default.          //If no from address has been set than use a default.
# Line 294  function drupal_mail_wrapper($mailkey, $ Line 300  function drupal_mail_wrapper($mailkey, $
300  //      $mail->From = $from;  //      $mail->From = $from;
301  //      $mail->FromName = $from_name;  //      $mail->FromName = $from_name;
302    
303    $torecipients = split(",", $to);     $to_addresses = _smtp_recipients_to_address_list($to);
304    foreach ($torecipients as $torecipient) {  
305      if (strpos($torecipient, '<') !== false) {     foreach ($to_addresses as $to_address) {
306        $toparts = explode(" <", $torecipient);        $mail->AddAddress($to_address['addr'], $to_address['name']);
307        $toname = $toparts[0];     }
       $toaddr = rtrim($toparts[1], ">");  
     }  
     else {  
       $toname = "";  
       $toaddr = $torecipient;  
     }  
     $mail->AddAddress($toaddr, $toname);  
   }  
308    
309          $mail->Subject = $subject;     $mail->Subject = $subject;
310          $mail->Body = $body;     $mail->Body = $body;
311    
312    watchdog('smtp', t('Sending mail to: !to', array('!to' => $to)));    watchdog('smtp', t('Sending mail to: !to', array('!to' => $to)));
313    
# Line 319  function drupal_mail_wrapper($mailkey, $ Line 317  function drupal_mail_wrapper($mailkey, $
317                  return false;                  return false;
318          }          }
319    
320          $mail->SmtpClose();     $mail->SmtpClose();
321          return true;     return true;
322    }
323    
324    if (!module_exists('mimemail')) {
325       function drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $headers) {
326          return _drupal_mail_wrapper($mailkey, $to, $subject, $body, $from, $headers);
327       }
328  }  }
329    
330    if (module_exists('mimemail')) {
331       function smtp_mailengine($op, $message = array()) {
332          //default values
333          $message = array_merge( array(
334              'address' => '',
335              'subject' => '',
336              'body' => '',
337              'sender' => '',
338              'headers' => '',
339              ), $message);
340    
341          switch ($op) {
342            case 'name':
343              return t('SMTP');
344    
345            case 'description':
346              return t("SMTP mailing engine using SMTP module.");
347    
348            case 'settings': //not implemented
349              return false;
350    
351            case 'multiple':
352            case 'single':
353            case 'send':
354              if (!is_array($message['address'])) {
355                $message['address'] = array($message['address']);
356              }
357              $status = true;
358              foreach ($message['address'] as $to) {
359                 $status = _drupal_mail_wrapper(
360                   '',
361                   $to,
362                   $message['subject'],
363                   $message['body'],
364                   $message['headers']['From'],
365                   $message['headers']) && $status;
366              }
367              return $status;
368          }
369    
370          return false;
371       }
372    }
373    
374    function _smtp_recipients_to_address_list($recipients) {
375       $address_list = array();
376    
377       $addresses = split(",", $recipients);
378    
379       foreach ($addresses as $address) {
380          if (strpos($address, '<') !== false) {
381             $parts = explode(" <", $address);
382             $name = $parts[0];
383             $addr = rtrim($parts[1], ">");
384          }
385          else {
386             $name = "";
387             $addr = $address;
388          }
389    
390          $address_list[] = array('addr'=>$addr, 'name'=>$name);
391       }
392    
393       return $address_list;
394    }
395    
396  /**  /**
397   * PHPMailer language settings.   * PHPMailer language settings.

Legend:
Removed from v.1.15.2.17  
changed lines
  Added in v.1.15.2.18

  ViewVC Help
Powered by ViewVC 1.1.2