2 /** @package verysimple::Email */
5 * import supporting libraries
7 require_once ("phpmailer/class.phpmailer.php");
8 require_once ("EmailMessage.php");
9 require_once ("Recipient.php");
11 define ( "MAILER_RESULT_FAIL", 0 );
12 define ( "MAILER_RESULT_OK", 1 );
14 define ( "MAILER_METHOD_SENDMAIL", "SENDMAIL" );
15 define ( "MAILER_METHOD_SMTP", "SMTP" );
16 define ( "MAILER_METHOD_MAIL", "MAIL" );
19 * Generic interface for sending Email
21 * @package verysimple::Email
22 * @author VerySimple Inc.
23 * @copyright 1997-2007 VerySimple, Inc.
24 * @license http://www.gnu.org/licenses/lgpl.html LGPL
38 * Constructor initializes the mailer object and prepares it for mailing
40 * If path is a SMTP connection string it may be entered in the format:
41 * host.domain.com -or- username:password@host.domain.com
46 * (either file path to sendmail or SMTP connection string)
48 function __construct($method = MAILER_METHOD_SENDMAIL
, $path = "/usr/sbin/sendmail") {
49 $pair = explode ( "@", $path );
51 if (count ( $pair ) > 1) {
52 $this->Path
= $pair [1];
53 $userpass = explode ( ":", $pair [0], 2 );
54 $this->AuthUsername
= $userpass [0];
55 $this->AuthPassword
= count ( $userpass ) > 1 ?
$userpass [1] : '';
60 $this->Method
= $method;
63 $this->LangPath
= $this->_GetLangPath ();
67 * Bare line feeds do not play nicely with email.
72 function FixBareLB($str) {
73 $str = str_replace ( "\r\n", "\n", $str );
74 return str_replace ( "\r", "\n", $str );
78 * This function attempts to locate the language file path for
79 * PHPMailer because it's a whiney-ass bitch about finding it's
80 * language file during unit testing
84 private function _GetLangPath() {
86 $paths = explode ( PATH_SEPARATOR
, get_include_path () );
88 foreach ( $paths as $path ) {
89 if (file_exists ( $path . '/language/phpmailer.lang-en.php' )) {
90 $lang_path = $path . '/language/';
98 * If MAILER_RESULT_FAIL is returned, use GetErrors() to
99 * determine the problem.
101 * @param EmailMessage $message
102 * @return int results of operation (MAILER_RESULT_OK | MAILER_RESULT_FAIL)
104 function Send($message) {
105 $mailer = new PHPMailer ();
107 // this prevents problems with phpmailer not being able to locate the language path
108 $mailer->SetLanguage ( "en", $this->LangPath
);
110 $mailer->From
= $message->From
->Email
;
111 $mailer->FromName
= $message->From
->RealName
;
112 if ($message->ReplyTo
)
113 $mailer->AddReplyTo ( $message->ReplyTo
->Email
, $message->ReplyTo
->RealName
);
114 $mailer->Subject
= $message->GetSubject ();
115 $mailer->Body
= $this->FixBareLB ( $message->GetBody () );
116 $mailer->ContentType
= ($message->Format
== MESSAGE_FORMAT_TEXT
) ?
"text/plain" : "text/html";
117 $mailer->Mailer
= strtolower ( $this->Method
);
118 $mailer->Host
= $this->Path
;
119 $mailer->Sendmail
= $this->Path
;
121 // use authentication if necessary
122 if ($this->AuthUsername
) {
123 $mailer->SMTPAuth
= true;
124 $mailer->Username
= $this->AuthUsername
;
125 $mailer->Password
= $this->AuthPassword
;
128 // if custom headers are to be provided, include them in the message
129 foreach ( $message->Headers
as $header_key => $header_val ) {
130 $mailer->AddCustomHeader ( $header_key . ': ' . $header_val );
133 if ($message->Sender
) {
134 $this->_log
[] = "Adding Sender " . $message->Sender
;
136 // phpmailer accepts this but it seems to not work consistently..?
137 // $mailer->Sender = $message->Sender;
139 // instead add the dang headers ourselves
140 $mailer->AddCustomHeader ( "Sender: " . $message->Sender
);
141 $mailer->AddCustomHeader ( "Return-Path: " . $message->Sender
);
144 if (! $this->IsValid ( $mailer->From
)) {
145 $this->_errors
[] = "Sender '" . $mailer->From
. "' is not a valid email address.";
146 return MAILER_RESULT_FAIL
;
149 // add the recipients
150 foreach ( $message->Recipients
as $recipient ) {
151 $this->_log
[] = "Adding Recipient " . $recipient->RealName
. " [" . $recipient->Email
. "]";
153 if (! $this->IsValid ( $recipient->Email
)) {
154 $this->_errors
[] = "Recipient '" . $recipient->Email
. "' is not a valid email address.";
155 return MAILER_RESULT_FAIL
;
158 $mailer->AddAddress ( $recipient->Email
, $recipient->RealName
);
161 foreach ( $message->CCRecipients
as $recipient ) {
162 $this->_log
[] = "Adding CC Recipient " . $recipient->RealName
. " [" . $recipient->Email
. "]";
164 if (! $this->IsValid ( $recipient->Email
)) {
165 $this->_errors
[] = "CC Recipient '" . $recipient->Email
. "' is not a valid email address.";
166 return MAILER_RESULT_FAIL
;
169 $mailer->AddCC ( $recipient->Email
, $recipient->RealName
);
172 foreach ( $message->BCCRecipients
as $recipient ) {
173 $this->_log
[] = "Adding BCC Recipient " . $recipient->RealName
. " [" . $recipient->Email
. "]";
175 if (! $this->IsValid ( $recipient->Email
)) {
176 $this->_errors
[] = "BCC Recipient '" . $recipient->Email
. "' is not a valid email address.";
177 return MAILER_RESULT_FAIL
;
180 $mailer->AddBCC ( $recipient->Email
, $recipient->RealName
);
183 $result = MAILER_RESULT_OK
;
185 $this->_log
[] = "Sending message using " . $mailer->Mailer
;
187 ob_start (); // buffer output because class.phpmailer.php Send() is chatty and writes to stdout
189 $fail = ! $mailer->Send ();
191 ob_end_clean (); // clear the buffer
193 if ($fail ||
$mailer->ErrorInfo
) {
194 $result = MAILER_RESULT_FAIL
;
195 $this->_errors
[] = trim ( str_replace ( array (
201 ), $mailer->ErrorInfo
) );
208 * returns true if the provided email appears to be valid
212 function IsValid($email) {
213 return Recipient
::IsEmailInValidFormat ( $email );
217 * Clears log and error
220 $this->_errors
= array ();
221 $this->_log
= array ();
225 * Utility method to send a simple text email message
228 * @param string $from
229 * @param string $subject
230 * @param string $body
232 function QuickSend($to, $from, $subject, $body, $format = MESSAGE_FORMAT_TEXT
) {
233 $message = new EmailMessage ();
234 $message->SetFrom ( $from );
235 $message->AddRecipient ( $to );
236 $message->Subject
= $subject;
237 $message->Body
= $body;
238 $message->Format
= $format;
240 return $this->Send ( $message );
244 * Returns an array of errors that occured during the last attempt
249 function GetErrors() {
250 return $this->_errors
;
254 * Returns a log of the last email transaction in array format