Current oav website
This commit is contained in:
109
dotclear._no/inc/libs/clearbricks/mail/class.mail.php
Normal file
109
dotclear._no/inc/libs/clearbricks/mail/class.mail.php
Normal file
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* @class mail
|
||||
* @brief Email utilities
|
||||
*
|
||||
* @package Clearbricks
|
||||
* @subpackage Mail
|
||||
*
|
||||
* @copyright Olivier Meunier & Association Dotclear
|
||||
* @copyright GPL-2.0-only
|
||||
*/
|
||||
|
||||
class mail
|
||||
{
|
||||
/**
|
||||
* Send email
|
||||
*
|
||||
* Sends email to destination. If a function called _mail() exists it will
|
||||
* be used instead of PHP mail() function. _mail() function should have the
|
||||
* same signature. Headers could be provided as a string or an array.
|
||||
*
|
||||
* @param string $to Email destination
|
||||
* @param string $subject Email subject
|
||||
* @param string $message Email message
|
||||
* @param string|array $headers Email headers
|
||||
* @param string $p UNIX mail additionnal parameters
|
||||
* @return boolean true on success
|
||||
*/
|
||||
public static function sendMail($to, $subject, $message, $headers = null, $p = null)
|
||||
{
|
||||
$f = function_exists('_mail') ? '_mail' : null;
|
||||
$eol = trim(ini_get('sendmail_path')) ? "\n" : "\r\n";
|
||||
|
||||
if (is_array($headers)) {
|
||||
$headers = implode($eol, $headers);
|
||||
}
|
||||
|
||||
if ($f == null) {
|
||||
if (!@mail($to, $subject, $message, $headers, $p)) {
|
||||
throw new Exception('Unable to send email');
|
||||
}
|
||||
} else {
|
||||
call_user_func($f, $to, $subject, $message, $headers, $p);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Host MX
|
||||
*
|
||||
* Returns MX records sorted by weight for a given host.
|
||||
*
|
||||
* @param string $host Hostname
|
||||
* @return array
|
||||
*/
|
||||
public static function getMX($host)
|
||||
{
|
||||
if (!getmxrr($host, $mx_h, $mx_w) || count($mx_h) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$res = [];
|
||||
|
||||
for ($i = 0; $i < count($mx_h); $i++) {
|
||||
$res[$mx_h[$i]] = $mx_w[$i];
|
||||
}
|
||||
|
||||
asort($res);
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Quoted printable header
|
||||
*
|
||||
* Encodes given string as a quoted printable mail header.
|
||||
*
|
||||
* @param string $str String to encode
|
||||
* @param string $charset Charset (default UTF-8)
|
||||
* @return string
|
||||
*/
|
||||
public static function QPHeader($str, $charset = 'UTF-8')
|
||||
{
|
||||
if (!preg_match('/[^\x00-\x3C\x3E-\x7E]/', $str)) {
|
||||
return $str;
|
||||
}
|
||||
|
||||
return '=?' . $charset . '?Q?' . text::QPEncode($str) . '?=';
|
||||
}
|
||||
|
||||
/**
|
||||
* B64 header
|
||||
*
|
||||
* Encodes given string as a base64 mail header.
|
||||
*
|
||||
* @param string $str String to encode
|
||||
* @param string $charset Charset (default UTF-8)
|
||||
* @return string
|
||||
*/
|
||||
public static function B64Header($str, $charset = 'UTF-8')
|
||||
{
|
||||
if (!preg_match('/[^\x00-\x3C\x3E-\x7E]/', $str)) {
|
||||
return $str;
|
||||
}
|
||||
|
||||
return '=?' . $charset . '?B?' . base64_encode($str) . '?=';
|
||||
}
|
||||
}
|
||||
167
dotclear._no/inc/libs/clearbricks/mail/class.socket.mail.php
Normal file
167
dotclear._no/inc/libs/clearbricks/mail/class.socket.mail.php
Normal file
@ -0,0 +1,167 @@
|
||||
<?php
|
||||
/**
|
||||
* @class socketMail
|
||||
* @brief Send email through socket
|
||||
*
|
||||
* @package Clearbricks
|
||||
* @subpackage Mail
|
||||
*
|
||||
* @copyright Olivier Meunier & Association Dotclear
|
||||
* @copyright GPL-2.0-only
|
||||
*/
|
||||
|
||||
class socketMail
|
||||
{
|
||||
public static $fp;
|
||||
public static $timeout = 10; ///< integer: Socket timeout
|
||||
public static $smtp_relay = null; ///< string: SMTP Relay to user
|
||||
|
||||
/**
|
||||
* Send email through socket
|
||||
*
|
||||
* This static method sends an email through a simple socket connection.
|
||||
* If {@link $smtp_relay} is set, it will be used as a relay to send the
|
||||
* email. Instead, email is sent directly to MX host of domain.
|
||||
*
|
||||
* @param string $to Email destination
|
||||
* @param string $subject Email subject
|
||||
* @param string $message Email message
|
||||
* @param string|array $headers Email headers
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function mail($to, $subject, $message, $headers = null)
|
||||
{
|
||||
$from = self::getFrom($headers);
|
||||
|
||||
$H = 'Return-Path: <' . $from . ">\r\n";
|
||||
|
||||
$from_host = explode('@', $from);
|
||||
$from_host = $from_host[1];
|
||||
|
||||
$to_host = explode('@', $to);
|
||||
$to_host = $to_host[1];
|
||||
|
||||
if (self::$smtp_relay != null) {
|
||||
$mx = [gethostbyname(self::$smtp_relay) => 1];
|
||||
} else {
|
||||
$mx = mail::getMX($to_host);
|
||||
}
|
||||
|
||||
foreach ($mx as $h => $w) {
|
||||
self::$fp = @fsockopen($h, 25, $errno, $errstr, self::$timeout);
|
||||
|
||||
if (self::$fp !== false) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_resource(self::$fp)) {
|
||||
self::$fp = null;
|
||||
throw new Exception('Unable to open socket');
|
||||
}
|
||||
|
||||
# We need to read the first line
|
||||
fgets(self::$fp);
|
||||
|
||||
$data = '';
|
||||
# HELO cmd
|
||||
if (!self::cmd('HELO ' . $from_host, $data)) {
|
||||
self::quit();
|
||||
throw new Exception($data);
|
||||
}
|
||||
|
||||
# MAIL FROM: <...>
|
||||
if (!self::cmd('MAIL FROM: <' . $from . '>', $data)) {
|
||||
self::quit();
|
||||
throw new Exception($data);
|
||||
}
|
||||
|
||||
# RCPT TO: <...>
|
||||
if (!self::cmd('RCPT TO: <' . $to . '>', $data)) {
|
||||
self::quit();
|
||||
throw new Exception($data);
|
||||
}
|
||||
|
||||
# Compose mail and send it with DATA
|
||||
$H = 'Return-Path: <' . $from . ">\r\n";
|
||||
$H .= 'To: <' . $to . ">\r\n";
|
||||
$H .= 'Subject: ' . $subject . "\r\n";
|
||||
$H .= $headers . "\r\n";
|
||||
|
||||
$message = $H . "\r\n\r\n" . $message;
|
||||
|
||||
if (!self::sendMessage($message, $data)) {
|
||||
self::quit();
|
||||
throw new Exception($data);
|
||||
}
|
||||
|
||||
self::quit();
|
||||
}
|
||||
|
||||
private static function getFrom($headers)
|
||||
{
|
||||
$f = '';
|
||||
|
||||
if (preg_match('/^from: (.+?)$/msi', $headers, $m)) {
|
||||
$f = trim($m[1]);
|
||||
}
|
||||
|
||||
if (preg_match('/(?:<)(.+?)(?:$|>)/si', $f, $m)) {
|
||||
$f = trim($m[1]);
|
||||
} elseif (preg_match('/^(.+?)\(/si', $f, $m)) {
|
||||
$f = trim($m[1]);
|
||||
} elseif (!text::isEmail($f)) {
|
||||
$f = trim(ini_get('sendmail_from'));
|
||||
}
|
||||
|
||||
if (!$f) {
|
||||
throw new Exception('No valid from e-mail address');
|
||||
}
|
||||
|
||||
return $f;
|
||||
}
|
||||
|
||||
private static function cmd($out, &$data = '')
|
||||
{
|
||||
fwrite(self::$fp, $out . "\r\n");
|
||||
$data = self::data();
|
||||
|
||||
if (substr($data, 0, 3) != '250') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static function data()
|
||||
{
|
||||
$s = '';
|
||||
stream_set_timeout(self::$fp, 2);
|
||||
|
||||
for ($i = 0; $i < 2; $i++) {
|
||||
$s .= fgets(self::$fp, 1024);
|
||||
}
|
||||
|
||||
return $s;
|
||||
}
|
||||
|
||||
private static function sendMessage($msg, &$data)
|
||||
{
|
||||
$msg .= "\r\n.";
|
||||
|
||||
self::cmd('DATA', $data);
|
||||
|
||||
if (substr($data, 0, 3) != '354') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::cmd($msg, $data);
|
||||
}
|
||||
|
||||
private static function quit()
|
||||
{
|
||||
self::cmd('QUIT');
|
||||
fclose(self::$fp);
|
||||
self::$fp = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user