dmBridge PHP API
DMMailer.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 # Copyright © 2009, 2010 Board of Regents of the Nevada System of Higher
00005 # Education, on behalf of the University of Nevada, Las Vegas
00006 #
00007 
00014 class DMMailer {
00015 
00025    public static function isValidEmail($email) {
00026       // Control characters are not allowed
00027       if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $email)) {
00028           return false;
00029       }
00030 
00031       // Split it into sections using last instance of "@"
00032       $intAtSymbol = strrpos($email, '@');
00033       if ($intAtSymbol === false) {
00034           // No "@" symbol in email.
00035           return false;
00036       }
00037       $arrEmailAddress[0] = substr($email, 0, $intAtSymbol);
00038       $arrEmailAddress[1] = substr($email, $intAtSymbol + 1);
00039 
00040       // Count the "@" symbols. Only one is allowed, except where
00041       // contained in quote marks in the local part. Quickest way to
00042       // check this is to remove anything in quotes.
00043       $arrTempAddress[0] = preg_replace('/"[^"]+"/'
00044                                        ,''
00045                                        ,$arrEmailAddress[0]);
00046       $arrTempAddress[1] = $arrEmailAddress[1];
00047       $strTempAddress = $arrTempAddress[0] . $arrTempAddress[1];
00048       // Then check - should be no "@" symbols.
00049       if (strrpos($strTempAddress, '@') !== false) {
00050           // "@" symbol found
00051           return false;
00052       }
00053 
00054       // Check local portion
00055       if (!self::isValidEmailUser($arrEmailAddress[0])) {
00056           return false;
00057       }
00058 
00059       // Check domain portion
00060       if (!DMString::isValidHostname($arrEmailAddress[1])) {
00061           return false;
00062       }
00063 
00064       // If we're still here, all checks above passed. Email is valid.
00065       return true;
00066    }
00067 
00078    private static final function isValidEmailUser($str) {
00079       // Local portion can only be from 1 to 64 characters, inclusive.
00080       // Please note that servers are encouraged to accept longer local
00081       // parts than 64 characters.
00082       if ((strlen($str) < 1) || (strlen($str) > 64)) {
00083           return false;
00084       }
00085       // Local portion must be:
00086       // 1) a dot-atom (strings separated by periods)
00087       // 2) a quoted string
00088       // 3) an obsolete format string (combination of the above)
00089       $arrLocalPortion = explode('.', $str);
00090       for ($i = 0, $max = sizeof($arrLocalPortion); $i < $max; $i++) {
00091            if (!preg_match('.^('
00092                           .    '([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]'
00093                           .    '[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]{0,63})'
00094                           .'|'
00095                           .    '("[^\\\"]{0,62}")'
00096                           .')$.'
00097                           ,$arrLocalPortion[$i])) {
00098               return false;
00099           }
00100       }
00101       return true;
00102    }
00103 
00110    public function send(DMMailable $mailable) {
00111       if (!self::isValidEmail($mailable->getEmailRecipientEmail())) {
00112          throw new DMInvalidEmailException($mailable);
00113       }
00114       return mail(
00115             $mailable->getEmailRecipientEmail(),
00116             $mailable->getEmailSubject(),
00117             wordwrap($mailable->getEmailBody(), 75),
00118             $this->getHeaders($mailable));
00119    }
00120 
00121    private function getHeaders(DMMailable $mailable) {
00122       $from = null;
00123       $headers = null;
00124       if ($mailable->getEmailFromEmail()) {
00125          $from = $mailable->getEmailFromEmail();
00126       }
00127       if ($from) {
00128          if ($mailable->getEmailFromName()) {
00129             $from = sprintf('"%s" <%s>',
00130                   $mailable->getEmailFromName(), $from);
00131          }
00132          $headers = sprintf("From: %s \r\n", $from);
00133       }
00134       return $headers;
00135    }
00136 
00141    public function sendNotificationOfNewComment(DMComment $comment) {
00142       $e = new DMEmailMessage();
00143       $e->setEmailRecipientEmail(
00144          DMConfigXML::getInstance()->getCommentNotificationEmail());
00145       $body = sprintf(
00146          "From: %s%s\n\n"
00147          . "%s\n\n"
00148          . "Log into the Control Panel to moderate this comment:\n"
00149          . "%s\n",
00150          DMString::websafe($comment->getName()),
00151          (DMString::websafe($comment->getEmail()))
00152             ? " (" . DMString::websafe($comment->getEmail()) . ")" : "",
00153          DMString::websafe($comment->getValue()),
00154          DMInternalURI::getURIWithParams("admin")
00155       );
00156       $e->setEmailBody($body);
00157       $e->setEmailSubject(
00158             DMLocalizedString::getString('EMAIL_COMMENT_SUBJECT'));
00159       return $this->send($e);
00160    }
00161 
00162 }
00163 
 All Data Structures Functions Variables