.

MailMessage Class Reference
[Lib]

Encapsulates an e-mail message, allowing attachments. More...

List of all members.

Public Member Functions

  __construct ($subject, $message, $to, $from= '', $content_type= '')
  constructor
  add_attachment ($file_name, $name= '')
  Append a file to attach.
  preprocess_header ()
  Clears from, subject, cc and to data to avoid header injection http://www.anders.com/projects/sysadmin/formPostHijacking/.
  send ()
  Sends email.
  set_alt_message ($msg)
  Set alternative message.

Public Attributes

const  MIME_HTML = 'text/html; charset="%charset"'
  HTML mime content type.
const  MIME_TEXT_PLAIN = 'text/plain; charset="%charset"'
  Plain text mime content type.

Protected Member Functions

  create_builder ()
  Return builder suited for config.
  encode_headers ($headers)

Detailed Description

Encapsulates an e-mail message, allowing attachments.

Note:
The class relies on the PEAR classes Mail and Mail_Mime
Author:
Gerd Riesselmann

Definition at line 12 of file mailmessage.cls.php.


Constructor & Destructor Documentation

MailMessage::__construct ( subject,
message,
to,
from = '',
content_type = ''  
)

constructor

Definition at line 67 of file mailmessage.cls.php.

00067                                                                                              {
00068                 $this->subject = trim(Config::get_value(Config::MAIL_SUBJECT) . ' ' . $subject);
00069                 $this->message = $message;
00070                 $this->to = $to;
00071                 $this->from = $from;
00072                 if (empty($content_type)) {
00073                         $this->content_type = self::MIME_TEXT_PLAIN; 
00074                 }
00075                 else {
00076                         $this->content_type = $content_type;
00077                 }
00078         }

Member Function Documentation

MailMessage::add_attachment ( file_name,
name = ''  
)

Append a file to attach.

Definition at line 144 of file mailmessage.cls.php.

00144                                                                {
00145                 if ($name == '') {
00146                         $name = $file_name;
00147                 }
00148                 $this->files_to_attach[$name] = $file_name;
00149         }
MailMessage::create_builder (  )  [protected]

Return builder suited for config.

Returns:
IMailMessageBuilder

Definition at line 126 of file mailmessage.cls.php.

00126                                             {
00127                 $ret = false;
00128                 Load::directories('lib/components/mailmessagebuilder');
00129                 $msg_builder = ($this->message_alt) 
00130                         ? new AlternativeMessageBuilder($this->message, $this->content_type, $this->message_alt)
00131                         : new SingleMessageBuilder($this->message, $this->content_type);
00132                 if (count($this->files_to_attach)) {
00133                         $ret = new AttachmentsBuilder($msg_builder, $this->files_to_attach);
00134                 }
00135                 else {
00136                         $ret = $msg_builder;
00137                 }
00138                 return $ret;             
00139         }
MailMessage::encode_headers ( headers  )  [protected]

Definition at line 113 of file mailmessage.cls.php.

00113                                                     {
00114                 $ret = array();
00115                 foreach($headers as $name => $value) {
00116                         $ret[$name] = ConverterFactory::encode($value, ConverterFactory::MIMEHEADER);
00117                 }
00118                 return $ret;
00119         }
MailMessage::preprocess_header (  ) 

Clears from, subject, cc and to data to avoid header injection http://www.anders.com/projects/sysadmin/formPostHijacking/.

Definition at line 155 of file mailmessage.cls.php.

00155                                             {
00156                 $this->to = $this->safety_preprocess_header_field($this->to);
00157                 $this->from = $this->safety_preprocess_header_field($this->from);
00158                 $this->subject = $this->safety_preprocess_header_field($this->subject);
00159                 $this->cc = $this->safety_preprocess_header_field($this->cc);
00160         }
MailMessage::send (  ) 

Sends email.

Returns:
Status

Definition at line 85 of file mailmessage.cls.php.

00085                                {
00086                 // Check for injection attack;
00087                 $ret = $this->safety_validate_header();
00088                 if ($ret->is_error()) {
00089                         return $ret;
00090                 }
00091                 
00092                 $headers = array(
00093                         'From' => empty($this->from) ? Config::get_value(Config::MAIL_SENDER, true) : $this->from,
00094                 );
00095                 if ($this->cc != '') {
00096                         $headers['Bcc'] = $this->cc;
00097                 }
00098                 
00099                 $builder = $this->create_builder();
00100                 $headers['Content-Type'] = $builder->get_mail_mime();
00101                 $headers = array_merge($headers, $builder->get_additional_headers());
00102                 $body = $builder->get_body();
00103                 
00104                 $headers = $this->encode_headers($headers);
00105                 $subject = ConverterFactory::encode($this->subject, ConverterFactory::MIMEHEADER);
00106                 if (!mail($this->to, $subject, $body, Arr::implode("\n", $headers, ': '))) {
00107                         $ret->append(tr('Could not send mail', 'core'));
00108                 }
00109 
00110                 return $ret;
00111         }
MailMessage::set_alt_message ( msg  ) 

Set alternative message.

Definition at line 165 of file mailmessage.cls.php.

00165                                               {
00166                 $this->message_alt = $msg;
00167         }

Member Data Documentation

const MailMessage::MIME_HTML = 'text/html; charset="%charset"'

HTML mime content type.

Definition at line 16 of file mailmessage.cls.php.

const MailMessage::MIME_TEXT_PLAIN = 'text/plain; charset="%charset"'

Plain text mime content type.

Definition at line 14 of file mailmessage.cls.php.


The documentation for this class was generated from the following file: