From f1f4e4386cf1f0e1127c3378037d261401e99246 Mon Sep 17 00:00:00 2001 From: rub Date: Tue, 25 Sep 2007 21:49:21 +0000 Subject: Resolved 0000580: Send mail by define smtp configuration git-svn-id: http://piwigo.org/svn/branches/branch-1_7@2105 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/class_smtp_mail.inc.php | 155 ++++++++++++ include/config_default.inc.php | 12 + include/functions_mail.inc.php | 505 +++++++++++++++++++++------------------- 3 files changed, 430 insertions(+), 242 deletions(-) create mode 100644 include/class_smtp_mail.inc.php (limited to 'include') diff --git a/include/class_smtp_mail.inc.php b/include/class_smtp_mail.inc.php new file mode 100644 index 000000000..450bcada9 --- /dev/null +++ b/include/class_smtp_mail.inc.php @@ -0,0 +1,155 @@ +host = $host; + $this->user = $user; + $this->password = $password; + $this->email_webmaster = $email_webmaster; + } + + // Adaptation of server_parse + function server_parse($expected_response) + { + if ($this->no_error) + { + $server_response = ''; + while (substr($server_response, 3, 1) != ' ') + { + if (!($server_response = fgets($this->socket, 256))) + { + trigger_error('Couldn\'t get mail server response codes.', E_USER_WARNING); + $this->no_error = false; + } + } + } + + if ($this->no_error) + { + if (!(substr($server_response, 0, 3) == $expected_response)) + { + trigger_error('Unable to send e-mail. Error message reported by the SMTP server: "'.$server_response.'"', E_USER_WARNING); + $this->no_error = false; + } + } + return $this->no_error; + } + + function server_write($s) + { + $this->no_error = $this->no_error && (fwrite($this->socket, $s) !== false); + return $this->no_error; + } + + // Adaptation of pun_mail + function mail($to, $subject, $message, $headers = '') + { + $this->no_error = true; + + $recipients = explode(',', $to); + + // Are we using port 25 or a custom port? + if (strpos($this->host, ':') !== false) + { + list($smtp_host, $smtp_port) = explode(':', $this->host); + } + else + { + $smtp_host = $this->host; + $smtp_port = 25; + } + + if ($this->socket = fsockopen($smtp_host, $smtp_port, $errno, $errstr, 15)) + { + $this->server_parse('220'); + + if (!empty($this->user) && !empty($this->password)) + { + $this->server_write('EHLO '.$smtp_host."\r\n"); + $this->server_parse('250'); + + $this->server_write('AUTH LOGIN'."\r\n"); + $this->server_parse('334'); + + $this->server_write(base64_encode($this->user)."\r\n"); + $this->no_error = $this->no_error && $this->no_error = $this->server_parse('334'); + + $this->server_write(base64_encode($this->password)."\r\n"); + $this->server_parse('235'); + } + else + { + $this->server_write('HELO '.$smtp_host."\r\n"); + $this->server_parse('250'); + } + + $this->server_write('MAIL FROM: <'.$this->email_webmaster.'>'."\r\n"); + $this->server_parse('250'); + + $to_header = 'To: '; + + @reset($recipients); + while (list(, $email) = @each($recipients)) + { + $this->server_write('RCPT TO: <'.$email.'>'."\r\n"); + $this->server_parse('250'); + + $to_header .= '<'.$email.'>, '; + } + + $this->server_write('DATA'."\r\n"); + $this->server_parse('354'); + + $this->server_write('Subject: '.$subject."\r\n".$to_header."\r\n".$headers."\r\n\r\n".$message."\r\n"); + $this->server_write('.'."\r\n"); + $this->server_parse('250'); + + $this->server_write('QUIT'."\r\n"); + fclose($this->socket); + } + else + { + trigger_error('Could not connect to smtp host "'.$this->host.'" ('.$errno.') ('.$errstr.')', E_USER_WARNING); + $this->no_error = false;; + } + + return $this->no_error; + } +} + +?> diff --git a/include/config_default.inc.php b/include/config_default.inc.php index cc949f7b2..815b6a06b 100644 --- a/include/config_default.inc.php +++ b/include/config_default.inc.php @@ -278,6 +278,18 @@ $conf['enabled_format_email'] = true; // Value could be text/plain or text/html $conf['default_email_format'] = 'text/html'; +// smtp configuration +// (work if fsockopen function is allowed for smtp port) +// smtp_host: smtp server host +// if null, regular mail function is used +// format: hoststring[:port] +// exemple: smtp.pwg.net:21 +// smtp_user/smtp_password: user & password for smtp identication +$conf['smtp_host'] = ''; +$conf['smtp_user'] = ''; +$conf['smtp_password'] = ''; + + // check_upgrade_feed: check if there are database upgrade required. Set to // true, a message will strongly encourage you to upgrade your database if // needed. diff --git a/include/functions_mail.inc.php b/include/functions_mail.inc.php index 55664e8a8..ee7b2dc7e 100644 --- a/include/functions_mail.inc.php +++ b/include/functions_mail.inc.php @@ -45,8 +45,12 @@ function get_mail_configuration() $conf_mail = array( 'mail_options' => $conf['mail_options'], - 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], - 'default_email_format' => $conf['default_email_format'] + 'send_bcc_mail_webmaster' => $conf['send_bcc_mail_webmaster'], + 'default_email_format' => $conf['default_email_format'], + 'use_smtp' => !empty($conf['smtp_host']), + 'smtp_host' => $conf['smtp_host'], + 'smtp_user' => $conf['smtp_user'], + 'smtp_password' => $conf['smtp_password'] ); // we have webmaster id among user list, what's his email address ? @@ -73,84 +77,88 @@ function format_email($name, $email) if ($conf['enabled_format_email']) { - $cvt7b_name = '"'.addslashes(str_translate_to_ascii7bits($name)).'"'; + // Spring cleaning + $cvt_name = trim(preg_replace('#[\n\r]+#s', '', $name)); + $cvt_email = trim(preg_replace('#[\n\r]+#s', '', $email)); + // Ascii convertion + $cvt_name = '"'.addslashes(str_translate_to_ascii7bits($cvt_name)).'"'; - if (strpos($email, '<') === false) + if (strpos($cvt_email, '<') === false) { - return $cvt7b_name.' <'.$email.'>'; + return $cvt_name.' <'.$cvt_email.'>'; } else { - return $cvt7b_name.$email; + return $cvt_name.$cvt_email; } } else { - return $email; + return $cvt_email; } } - -/** - * Returns an completed array template/theme - * completed with get_default_template() - * - * @params: - * - args: incompleted array of template/theme - * o template: template to use [default get_default_template()] - * o theme: template to use [default get_default_template()] - */ -function get_array_template_theme($args = array()) -{ - global $conf; - - $res = array(); - - if (empty($args['template']) or empty($args['theme'])) - { - list($res['template'], $res['theme']) = explode('/', get_default_template()); - } - - if (!empty($args['template'])) - { - $res['template'] = $args['template']; - } - - if (!empty($args['theme'])) - { - $res['theme'] = $args['theme']; - } - - return $res; -} - -/** - * Return an new mail template - * - * @params: - * - email_format: mail format - * - args: function params of mail function: - * o template: template to use [default get_default_template()] - * o theme: template to use [default get_default_template()] - */ -function get_mail_template($email_format, $args = array()) -{ - $args = get_array_template_theme($args); - - $mail_template = new Template(PHPWG_ROOT_PATH.'template/'.$args['template'], $args['theme']); - $mail_template->set_rootdir(PHPWG_ROOT_PATH.'template/'.$args['template'].'/mail/'.$email_format); - - return $mail_template; -} - -/** - * Return string email format (html or not) - * - * @param string format - */ -function get_str_email_format($is_html) -{ - return ($is_html ? 'text/html' : 'text/plain'); -} + +/** + * Returns an completed array template/theme + * completed with get_default_template() + * + * @params: + * - args: incompleted array of template/theme + * o template: template to use [default get_default_template()] + * o theme: template to use [default get_default_template()] + */ +function get_array_template_theme($args = array()) +{ + global $conf; + + $res = array(); + + if (empty($args['template']) or empty($args['theme'])) + { + list($res['template'], $res['theme']) = explode('/', get_default_template()); + } + + if (!empty($args['template'])) + { + $res['template'] = $args['template']; + } + + if (!empty($args['theme'])) + { + $res['theme'] = $args['theme']; + } + + return $res; +} + +/** + * Return an new mail template + * + * @params: + * - email_format: mail format + * - args: function params of mail function: + * o template: template to use [default get_default_template()] + * o theme: template to use [default get_default_template()] + */ +function get_mail_template($email_format, $args = array()) +{ + $args = get_array_template_theme($args); + + $mail_template = new Template(PHPWG_ROOT_PATH.'template/'.$args['template'], $args['theme']); + $mail_template->set_rootdir(PHPWG_ROOT_PATH.'template/'.$args['template'].'/mail/'.$email_format); + + return $mail_template; +} + +/** + * Return string email format (html or not) + * + * @param string format + */ +function get_str_email_format($is_html) +{ + return ($is_html ? 'text/html' : 'text/plain'); +} /* * Switch language to param language @@ -242,8 +250,8 @@ function switch_lang_back() * @return string */ /* - * send en notification email to all administrators - * if a administrator is doing action, + * send en notification email to all administrators + * if a administrator is doing action, * he's be removed to email list * * @param: @@ -270,7 +278,7 @@ where I.user_id = U.'.$conf['user_fields']['id'].' and I.status in (\'webmaster\', \'admin\') and I.adviser = \'false\' and - '.$conf['user_fields']['email'].' is not null and + '.$conf['user_fields']['email'].' is not null and I.user_id <> '.$user['id'].' order by username @@ -289,13 +297,13 @@ order by } if (count($admins) > 0) - { - $keyargs_content_admin_info = array - ( - get_l10n_args('Connected user: %s', $user['username']), - get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']), - get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT']) - ); + { + $keyargs_content_admin_info = array + ( + get_l10n_args('Connected user: %s', $user['username']), + get_l10n_args('IP: %s', $_SERVER['REMOTE_ADDR']), + get_l10n_args('Browser: %s', $_SERVER['HTTP_USER_AGENT']) + ); switch_lang_to(get_default_language()); @@ -303,11 +311,11 @@ order by ( '', array - ( - 'Bcc' => $admins, + ( + 'Bcc' => $admins, 'subject' => '['.$conf['gallery_title'].'] '.l10n_args($keyargs_subject), 'content' => - l10n_args($keyargs_content)."\n\n" + l10n_args($keyargs_content)."\n\n" .l10n_args($keyargs_content_admin_info)."\n", 'content_format' => 'text/plain' ) @@ -318,23 +326,23 @@ order by return $return; } - + /* * send en email to user's group * - * @param: - * - group_id: mail are sent to group with this Id - * - email_format: mail format - * - keyargs_subject: mail subject on l10n_args format + * @param: + * - group_id: mail are sent to group with this Id + * - email_format: mail format + * - keyargs_subject: mail subject on l10n_args format * - dirname: short name of directory including template * - tpl_shortname: short template name without extension - * - assign_vars: array used to assign_vars to mail template - * - language_selected: send mail only to user with this selected language + * - assign_vars: array used to assign_vars to mail template + * - language_selected: send mail only to user with this selected language * * @return boolean (Ok or not) */ -function pwg_mail_group( - $group_id, $email_format, $keyargs_subject, +function pwg_mail_group( + $group_id, $email_format, $keyargs_subject, $dirname, $tpl_shortname, $assign_vars = array(), $language_selected = '') { @@ -361,18 +369,18 @@ WHERE $query .= ' ;'; - $result = pwg_query($query); - - if (mysql_num_rows($result) > 0) - { - $list = array(); - while ($row = mysql_fetch_array($result)) - { - $row['template_theme'] = $row['template']; - list($row['template'], $row['theme']) = explode('/', $row['template_theme']); - $list[] = $row; - } - } + $result = pwg_query($query); + + if (mysql_num_rows($result) > 0) + { + $list = array(); + while ($row = mysql_fetch_array($result)) + { + $row['template_theme'] = $row['template']; + list($row['template'], $row['theme']) = explode('/', $row['template_theme']); + $list[] = $row; + } + } foreach ($list as $elem) { @@ -387,8 +395,8 @@ FROM WHERE '.$conf['user_fields']['email'].' IS NOT NULL AND group_id = '.$group_id.' - AND language = \''.$elem['language'].'\' - AND template = \''.$elem['template_theme'].'\' + AND language = \''.$elem['language'].'\' + AND template = \''.$elem['template_theme'].'\' ;'; $result = pwg_query($query); @@ -409,7 +417,7 @@ WHERE switch_lang_to($elem['language']); $mail_template = get_mail_template($email_format, $elem); - $mail_template->set_filename($tpl_shortname, + $mail_template->set_filename($tpl_shortname, (empty($dirname) ? '' : $dirname.'/').$tpl_shortname.'.tpl'); $mail_template->assign_vars($assign_vars); @@ -417,7 +425,7 @@ WHERE ( '', array - ( + ( 'Bcc' => $Bcc, 'subject' => l10n_args($keyargs_subject), 'email_format' => $email_format, @@ -436,53 +444,52 @@ WHERE return $return; } - /* * sends an email, using PhpWebGallery specific informations * * @param: * - to: Receiver, or receivers of the mail. * - args: function params of mail function: - * o from: sender [default value webmaster email] - * o Cc: array of carbon copy receivers of the mail. [default value empty] - * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] + * o from: sender [default value webmaster email] + * o Cc: array of carbon copy receivers of the mail. [default value empty] + * o Bcc: array of blind carbon copy receivers of the mail. [default value empty] * o subject [default value 'PhpWebGallery'] * o content: content of mail [default value ''] * o content_format: format of mail content [default value 'text/plain'] - * o email_format: global mail format [default value $conf_mail['default_email_format']] - * o template: template to use [default get_default_template()] + * o email_format: global mail format [default value $conf_mail['default_email_format']] + * o template: template to use [default get_default_template()] * o theme: template to use [default get_default_template()] * * @return boolean (Ok or not) - */ + */ function pwg_mail($to, $args = array()) { global $conf, $conf_mail, $lang_info, $page; - - if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) - { - return true; - } + + if (empty($to) and empty($args['Cc']) and empty($args['Bcc'])) + { + return true; + } if (!isset($conf_mail)) { $conf_mail = get_mail_configuration(); } - - if (empty($args['email_format'])) - { - $args['email_format'] = $conf_mail['default_email_format']; - } - + + if (empty($args['email_format'])) + { + $args['email_format'] = $conf_mail['default_email_format']; + } + // Compute root_path in order have complete path if ($args['email_format'] == 'text/html') { - set_make_full_url(); + set_make_full_url(); } - - if (!empty($to)) - { - $to = format_email('', $to); + + if (!empty($to)) + { + $to = format_email('', $to); } if (empty($args['from'])) @@ -498,7 +505,10 @@ function pwg_mail($to, $args = array()) { $args['subject'] = 'PhpWebGallery'; } - $cvt7b_subject = str_translate_to_ascii7bits($args['subject']); + // Spring cleaning + $cvt_subject = trim(preg_replace('#[\n\r]+#s', '', $args['subject'])); + // Ascii convertion + $cvt_subject = str_translate_to_ascii7bits($cvt_subject); if (!isset($args['content'])) { @@ -509,138 +519,139 @@ function pwg_mail($to, $args = array()) { $args['content_format'] = 'text/plain'; } - - if ($conf_mail['send_bcc_mail_webmaster']) - { - $args['Bcc'][] = $conf_mail['formated_email_webmaster']; - } - - if (($args['content_format'] == 'text/html') and ($args['email_format'] == 'text/plain')) - { - // Todo find function to convert html text to plain text - return false; - } - - $args = array_merge($args, get_array_template_theme($args)); - + + if ($conf_mail['send_bcc_mail_webmaster']) + { + $args['Bcc'][] = $conf_mail['formated_email_webmaster']; + } + + if (($args['content_format'] == 'text/html') and ($args['email_format'] == 'text/plain')) + { + // Todo find function to convert html text to plain text + return false; + } + + $args = array_merge($args, get_array_template_theme($args)); + $headers = 'From: '.$args['from']."\n"; - $headers.= 'Reply-To: '.$args['from']."\n"; - if (empty($to)) - { - $headers.= 'To: undisclosed-recipients: ;'."\n"; - } - - if (!empty($args['Cc'])) - { - $headers.= 'Cc: '.implode(',', $args['Cc'])."\n"; - } - - if (!empty($args['Bcc'])) - { - $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n"; - } + $headers.= 'Reply-To: '.$args['from']."\n"; + if (empty($to)) + { + $headers.= 'To: undisclosed-recipients: ;'."\n"; + } + + if (!empty($args['Cc'])) + { + $headers.= 'Cc: '.implode(',', $args['Cc'])."\n"; + } + + if (!empty($args['Bcc'])) + { + $headers.= 'Bcc: '.implode(',', $args['Bcc'])."\n"; + } $headers.= 'Content-Type: multipart/alternative;'."\n"; $headers.= ' boundary="---='.$conf_mail['boundary_key'].'";'."\n"; - $headers.= ' reply-type=original'."\n"; - $headers.= 'MIME-Version: 1.0'."\n"; - - $content = ''; - - if (!isset($conf_mail[$args['email_format']][$lang_info['charset']][$args['template']][$args['theme']])) - { - if (!isset($mail_template)) - { + $headers.= ' reply-type=original'."\n"; + $headers.= 'MIME-Version: 1.0'."\n"; + $headers.= 'X-Mailer: Piwigo Mailer'."\n"; + + $content = ''; + + if (!isset($conf_mail[$args['email_format']][$lang_info['charset']][$args['template']][$args['theme']])) + { + if (!isset($mail_template)) + { $mail_template = get_mail_template($args['email_format']); - } - - $mail_template->set_filename('mail_header', 'header.tpl'); - $mail_template->set_filename('mail_footer', 'footer.tpl'); - - $mail_template->assign_vars( - array( - //Header + } + + $mail_template->set_filename('mail_header', 'header.tpl'); + $mail_template->set_filename('mail_footer', 'footer.tpl'); + + $mail_template->assign_vars( + array( + //Header 'BOUNDARY_KEY' => $conf_mail['boundary_key'], 'CONTENT_TYPE' => $args['email_format'], 'CONTENT_ENCODING' => $lang_info['charset'], - 'LANG' => $lang_info['code'], - 'DIR' => $lang_info['direction'], - - // Footer - 'GALLERY_URL' => - isset($page['gallery_url']) ? - $page['gallery_url'] : $conf['gallery_url'], - 'GALLERY_TITLE' => - isset($page['gallery_title']) ? - $page['gallery_title'] : $conf['gallery_title'], - 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', - 'PHPWG_URL' => PHPWG_URL, - - 'TITLE_MAIL' => urlencode(l10n('title_send_mail')), - 'MAIL' => get_webmaster_mail_address() - )); - - if ($args['email_format'] == 'text/html') - { - $old_root = $mail_template->root; - - if (is_file($mail_template->root.'/global-mail-css.tpl')) - { - $mail_template->set_filename('global_mail_css', 'global-mail-css.tpl'); - $mail_template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global_mail_css'); - } - - $mail_template->root = PHPWG_ROOT_PATH.'template/'.$args['template'].'/theme/'.$args['theme']; - if (is_file($mail_template->root.'/mail-css.tpl')) - { - $mail_template->set_filename('mail_css', 'mail-css.tpl'); - $mail_template->assign_var_from_handle('MAIL_CSS', 'mail_css'); - } - - $mail_template->root = PHPWG_ROOT_PATH.'template-common'; - if (is_file($mail_template->root.'/local-mail-css.tpl')) - { - $mail_template->set_filename('local_mail_css', 'local-mail-css.tpl'); - $mail_template->assign_var_from_handle('LOCAL_MAIL_CSS', 'local_mail_css'); - } - - $mail_template->root = $old_root; - } - - // what are displayed on the header of each mail ? + 'LANG' => $lang_info['code'], + 'DIR' => $lang_info['direction'], + + // Footer + 'GALLERY_URL' => + isset($page['gallery_url']) ? + $page['gallery_url'] : $conf['gallery_url'], + 'GALLERY_TITLE' => + isset($page['gallery_title']) ? + $page['gallery_title'] : $conf['gallery_title'], + 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', + 'PHPWG_URL' => PHPWG_URL, + + 'TITLE_MAIL' => urlencode(l10n('title_send_mail')), + 'MAIL' => get_webmaster_mail_address() + )); + + if ($args['email_format'] == 'text/html') + { + $old_root = $mail_template->root; + + if (is_file($mail_template->root.'/global-mail-css.tpl')) + { + $mail_template->set_filename('global_mail_css', 'global-mail-css.tpl'); + $mail_template->assign_var_from_handle('GLOBAL_MAIL_CSS', 'global_mail_css'); + } + + $mail_template->root = PHPWG_ROOT_PATH.'template/'.$args['template'].'/theme/'.$args['theme']; + if (is_file($mail_template->root.'/mail-css.tpl')) + { + $mail_template->set_filename('mail_css', 'mail-css.tpl'); + $mail_template->assign_var_from_handle('MAIL_CSS', 'mail_css'); + } + + $mail_template->root = PHPWG_ROOT_PATH.'template-common'; + if (is_file($mail_template->root.'/local-mail-css.tpl')) + { + $mail_template->set_filename('local_mail_css', 'local-mail-css.tpl'); + $mail_template->assign_var_from_handle('LOCAL_MAIL_CSS', 'local_mail_css'); + } + + $mail_template->root = $old_root; + } + + // what are displayed on the header of each mail ? $conf_mail[$args['email_format']] [$lang_info['charset']] - [$args['template']][$args['theme']]['header'] = + [$args['template']][$args['theme']]['header'] = $mail_template->parse('mail_header', true); - // what are displayed on the footer of each mail ? + // what are displayed on the footer of each mail ? $conf_mail[$args['email_format']] [$lang_info['charset']] [$args['template']][$args['theme']]['footer'] = $mail_template->parse('mail_footer', true); - } + } // Header $content.= $conf_mail[$args['email_format']] [$lang_info['charset']] - [$args['template']][$args['theme']]['header']; + [$args['template']][$args['theme']]['header']; // Content - if (($args['content_format'] == 'text/plain') and ($args['email_format'] == 'text/html')) - { + if (($args['content_format'] == 'text/plain') and ($args['email_format'] == 'text/html')) + { $content.= '

'. nl2br( preg_replace("/(http:\/\/)([^\s,]*)/i", "$1$2", htmlentities($args['content']))). '

'; - } - else - { - $content.= $args['content']; - } + } + else + { + $content.= $args['content']; + } - // Footer + // Footer $content.= $conf_mail[$args['email_format']] [$lang_info['charset']] [$args['template']][$args['theme']]['footer']; @@ -650,11 +661,11 @@ function pwg_mail($to, $args = array()) // Undo Compute root_path in order have complete path if ($args['email_format'] == 'text/html') - { + { unset_make_full_url(); } - /*Testing block + /*Testing block { global $user; @mkdir(PHPWG_ROOT_PATH.'testmail'); @@ -671,17 +682,27 @@ function pwg_mail($to, $args = array()) fwrite($file, $content); fclose($file); return true; - }*/ + }*/ - if ($conf_mail['mail_options']) + if ($conf_mail['use_smtp']) { - $options = '-f '.$conf_mail['email_webmaster']; - - return mail($to, $cvt7b_subject, $content, $headers, $options); + include_once( PHPWG_ROOT_PATH.'include/class_smtp_mail.inc.php' ); + $smtp_mail = new smtp_mail( + $conf_mail['smtp_host'], $conf_mail['smtp_user'], $conf_mail['smtp_password'], + $conf_mail['email_webmaster']); + return $smtp_mail->mail($to, $cvt_subject, $content, $headers); } else { - return mail($to, $cvt7b_subject, $content, $headers); + if ($conf_mail['mail_options']) + { + $options = '-f '.$conf_mail['email_webmaster']; + return mail($to, $cvt_subject, $content, $headers, $options); + } + else + { + return mail($to, $cvt_subject, $content, $headers); + } } } -- cgit v1.2.3