示例#1
0
 public static function SendEmail($fromaddr, $toaddr, $subject, $options = array())
 {
     $subject = str_replace("\r", " ", $subject);
     $subject = str_replace("\n", " ", $subject);
     if (!UTF8::IsASCII($subject)) {
         $subject = self::ConvertToRFC1342($subject);
     }
     $replytoaddr = isset($options["replytoaddr"]) ? $options["replytoaddr"] : "";
     $ccaddr = isset($options["ccaddr"]) ? $options["ccaddr"] : "";
     $bccaddr = isset($options["bccaddr"]) ? $options["bccaddr"] : "";
     $headers = isset($options["headers"]) ? $options["headers"] : "";
     $textmessage = isset($options["textmessage"]) ? $options["textmessage"] : "";
     $htmlmessage = isset($options["htmlmessage"]) ? $options["htmlmessage"] : "";
     $attachments = isset($options["attachments"]) ? $options["attachments"] : array();
     $messagetoaddr = self::EmailAddressesToEmailHeaders($toaddr, "To", true, false, $options);
     $replytoaddr = self::EmailAddressesToEmailHeaders($replytoaddr, "Reply-To", false, false, $options);
     if ($replytoaddr == "") {
         $replytoaddr = self::EmailAddressesToEmailHeaders($fromaddr, "Reply-To", false, false, $options);
     }
     $messagefromaddr = self::EmailAddressesToEmailHeaders($fromaddr, "From", false, false, $options);
     if ($messagefromaddr == "" && $replytoaddr == "") {
         return array("success" => false, "error" => self::SMTP_Translate("From address is invalid."), "errorcode" => "invalid_from_address", "info" => $fromaddr);
     }
     if ($ccaddr != "") {
         $toaddr .= ", " . $ccaddr;
     }
     $ccaddr = self::EmailAddressesToEmailHeaders($ccaddr, "Cc", true, false, $options);
     if ($bccaddr != "") {
         $toaddr .= ", " . $bccaddr;
     }
     $bccaddr = self::EmailAddressesToEmailHeaders($bccaddr, "Bcc", true, false, $options);
     if ($htmlmessage == "" && !count($attachments)) {
         // Plain-text e-mail.
         $destheaders = "";
         $destheaders .= $messagefromaddr;
         if ($headers != "") {
             $destheaders .= $headers;
         }
         $destheaders .= "MIME-Version: 1.0\r\n";
         if (!isset($options["usemail"]) || !$options["usemail"]) {
             $destheaders .= $messagetoaddr;
         }
         if ($replytoaddr != "") {
             $destheaders .= $replytoaddr;
         }
         if ($ccaddr != "") {
             $destheaders .= $ccaddr;
         }
         if ($bccaddr != "") {
             $destheaders .= $bccaddr;
         }
         if (!isset($options["usemail"]) || !$options["usemail"]) {
             $destheaders .= "Subject: " . $subject . "\r\n";
         }
         $destheaders .= "Content-Type: text/plain; charset=UTF-8\r\n";
         $destheaders .= "Content-Transfer-Encoding: quoted-printable\r\n";
         $message = self::ConvertEmailMessageToRFC1341($textmessage);
     } else {
         // MIME e-mail (HTML, text, attachments).
         $mimeboundary = "--------" . self::MIME_RandomString(25);
         $destheaders = "";
         $destheaders .= $messagefromaddr;
         if ($headers != "") {
             $destheaders .= $headers;
         }
         $destheaders .= "MIME-Version: 1.0\r\n";
         if (!isset($options["usemail"]) || !$options["usemail"]) {
             $destheaders .= $messagetoaddr;
         }
         if ($replytoaddr != "") {
             $destheaders .= $replytoaddr;
         }
         if ($ccaddr != "") {
             $destheaders .= $ccaddr;
         }
         if ($bccaddr != "") {
             $destheaders .= $bccaddr;
         }
         if (!isset($options["usemail"]) || !$options["usemail"]) {
             $destheaders .= "Subject: " . $subject . "\r\n";
         }
         if (count($attachments) && isset($options["inlineattachments"]) && $options["inlineattachments"]) {
             $destheaders .= "Content-Type: multipart/related; boundary=\"" . $mimeboundary . "\"; type=\"multipart/alternative\"\r\n";
         } else {
             if (count($attachments)) {
                 $destheaders .= "Content-Type: multipart/mixed; boundary=\"" . $mimeboundary . "\"\r\n";
             } else {
                 if ($textmessage != "" && $htmlmessage != "") {
                     $destheaders .= "Content-Type: multipart/alternative; boundary=\"" . $mimeboundary . "\"\r\n";
                 } else {
                     $mimeboundary = "";
                 }
             }
         }
         if ($mimeboundary != "") {
             $mimecontent = "This is a multi-part message in MIME format.\r\n";
         } else {
             $mimecontent = "";
         }
         if ($textmessage == "" || $htmlmessage == "" || !count($attachments)) {
             $mimeboundary2 = $mimeboundary;
         } else {
             $mimeboundary2 = "--------" . self::MIME_RandomString(25);
             $mimecontent .= "--" . $mimeboundary . "\r\n";
             $mimecontent .= "Content-Type: multipart/alternative; boundary=\"" . $mimeboundary2 . "\"\r\n";
             $mimecontent .= "\r\n";
         }
         if ($textmessage != "") {
             if ($mimeboundary2 != "") {
                 $mimecontent .= "--" . $mimeboundary2 . "\r\n";
                 $mimecontent .= "Content-Type: text/plain; charset=UTF-8\r\n";
                 $mimecontent .= "Content-Transfer-Encoding: quoted-printable\r\n";
                 $mimecontent .= "\r\n";
             } else {
                 $destheaders .= "Content-Type: text/plain; charset=UTF-8\r\n";
                 $destheaders .= "Content-Transfer-Encoding: quoted-printable\r\n";
             }
             $message = self::ConvertEmailMessageToRFC1341($textmessage);
             $mimecontent .= $message;
             $mimecontent .= "\r\n";
         }
         if ($htmlmessage != "") {
             if ($mimeboundary2 != "") {
                 $mimecontent .= "--" . $mimeboundary2 . "\r\n";
                 $mimecontent .= "Content-Type: text/html; charset=UTF-8\r\n";
                 $mimecontent .= "Content-Transfer-Encoding: quoted-printable\r\n";
                 $mimecontent .= "\r\n";
             } else {
                 $destheaders .= "Content-Type: text/html; charset=UTF-8\r\n";
                 $destheaders .= "Content-Transfer-Encoding: quoted-printable\r\n";
             }
             $message = self::ConvertEmailMessageToRFC1341($htmlmessage);
             $mimecontent .= $message;
             $mimecontent .= "\r\n";
         }
         if ($mimeboundary2 != "" && $mimeboundary != $mimeboundary2) {
             $mimecontent .= "--" . $mimeboundary2 . "--\r\n";
         }
         // Process the attachments.
         $y = count($attachments);
         for ($x = 0; $x < $y; $x++) {
             $mimecontent .= "--" . $mimeboundary . "\r\n";
             $type = str_replace("\r", "", $attachments[$x]["type"]);
             $type = str_replace("\n", "", $type);
             $type = UTF8::ConvertToASCII($type);
             if (!isset($attachments[$x]["name"])) {
                 $name = "";
             } else {
                 $name = str_replace("\r", "", $attachments[$x]["name"]);
                 $name = str_replace("\n", "", $name);
                 $name = self::FilenameSafe($name);
             }
             if (!isset($attachments[$x]["location"])) {
                 $location = "";
             } else {
                 $location = str_replace("\r", "", $attachments[$x]["location"]);
                 $location = str_replace("\n", "", $location);
                 $location = UTF8::ConvertToASCII($location);
             }
             if (!isset($attachments[$x]["cid"])) {
                 $cid = "";
             } else {
                 $cid = str_replace("\r", "", $attachments[$x]["cid"]);
                 $cid = str_replace("\n", "", $cid);
                 $cid = UTF8::ConvertToASCII($cid);
             }
             $mimecontent .= "Content-Type: " . $type . ($name != "" ? "; name=\"" . $name . "\"" : "") . "\r\n";
             if ($cid != "") {
                 $mimecontent .= "Content-ID: <" . $cid . ">\r\n";
             }
             if ($location != "") {
                 $mimecontent .= "Content-Location: " . $location . "\r\n";
             }
             $mimecontent .= "Content-Transfer-Encoding: base64\r\n";
             if ($name != "") {
                 $mimecontent .= "Content-Disposition: inline; filename=\"" . $name . "\"\r\n";
             }
             $mimecontent .= "\r\n";
             $mimecontent .= chunk_split(base64_encode($attachments[$x]["data"]));
             $mimecontent .= "\r\n";
         }
         if ($mimeboundary != "") {
             $mimecontent .= "--" . $mimeboundary . "--\r\n";
         }
         $message = $mimecontent;
     }
     if (isset($options["returnresults"]) && $options["returnresults"]) {
         return array("success" => true, "toaddr" => $toaddr, "fromaddr" => $fromaddr, "headers" => $destheaders, "subject" => $subject, "message" => $message);
     } else {
         if (isset($options["usemail"]) && $options["usemail"]) {
             $result = mail($toaddr, $subject, self::ReplaceNewlines("\n", $message), $destheaders);
             if (!$result) {
                 return array("success" => false, "error" => self::SMTP_Translate("PHP mail() call failed."), "errorcode" => "mail_call_failed");
             }
             return array("success" => true);
         } else {
             return self::SendSMTPEmail($toaddr, $fromaddr, $destheaders . "\r\n" . $message, $options);
         }
     }
 }