2012-04-20 22:49:35 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-26 13:37:37 +03:00
|
|
|
* Copyright (c) 2012 Frank Karlitschek <frank@owncloud.org>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OC_Mail
|
2012-04-20 22:49:35 +04:00
|
|
|
*
|
2015-02-26 13:37:37 +03:00
|
|
|
* A class to handle mail sending.
|
2012-04-20 22:49:35 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2012-04-20 22:49:35 +04:00
|
|
|
class OC_Mail {
|
|
|
|
|
|
|
|
/**
|
2012-08-29 10:38:33 +04:00
|
|
|
* send an email
|
2012-04-20 22:49:35 +04:00
|
|
|
*
|
|
|
|
* @param string $toaddress
|
|
|
|
* @param string $toname
|
|
|
|
* @param string $subject
|
|
|
|
* @param string $mailtext
|
|
|
|
* @param string $fromaddress
|
|
|
|
* @param string $fromname
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param integer $html
|
2013-01-02 22:04:08 +04:00
|
|
|
* @param string $altbody
|
|
|
|
* @param string $ccaddress
|
|
|
|
* @param string $ccname
|
|
|
|
* @param string $bcc
|
|
|
|
* @throws Exception
|
2012-04-20 22:49:35 +04:00
|
|
|
*/
|
2013-02-11 20:44:02 +04:00
|
|
|
public static function send($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
|
|
|
|
$html=0, $altbody='', $ccaddress='', $ccname='', $bcc='') {
|
2012-04-20 22:49:35 +04:00
|
|
|
|
|
|
|
$SMTPMODE = OC_Config::getValue( 'mail_smtpmode', 'sendmail' );
|
|
|
|
$SMTPHOST = OC_Config::getValue( 'mail_smtphost', '127.0.0.1' );
|
2013-01-02 22:04:08 +04:00
|
|
|
$SMTPPORT = OC_Config::getValue( 'mail_smtpport', 25 );
|
2012-08-29 10:38:33 +04:00
|
|
|
$SMTPAUTH = OC_Config::getValue( 'mail_smtpauth', false );
|
2013-01-26 15:09:54 +04:00
|
|
|
$SMTPAUTHTYPE = OC_Config::getValue( 'mail_smtpauthtype', 'LOGIN' );
|
2012-08-29 10:38:33 +04:00
|
|
|
$SMTPUSERNAME = OC_Config::getValue( 'mail_smtpname', '' );
|
|
|
|
$SMTPPASSWORD = OC_Config::getValue( 'mail_smtppassword', '' );
|
2013-01-22 17:13:24 +04:00
|
|
|
$SMTPDEBUG = OC_Config::getValue( 'mail_smtpdebug', false );
|
|
|
|
$SMTPTIMEOUT = OC_Config::getValue( 'mail_smtptimeout', 10 );
|
|
|
|
$SMTPSECURE = OC_Config::getValue( 'mail_smtpsecure', '' );
|
2012-04-20 22:49:35 +04:00
|
|
|
|
|
|
|
|
2012-07-11 23:52:51 +04:00
|
|
|
$mailo = new PHPMailer(true);
|
2012-04-20 22:49:35 +04:00
|
|
|
if($SMTPMODE=='sendmail') {
|
|
|
|
$mailo->IsSendmail();
|
2012-09-07 17:22:01 +04:00
|
|
|
}elseif($SMTPMODE=='smtp') {
|
2012-04-20 22:49:35 +04:00
|
|
|
$mailo->IsSMTP();
|
2012-09-07 17:22:01 +04:00
|
|
|
}elseif($SMTPMODE=='qmail') {
|
2012-04-20 22:49:35 +04:00
|
|
|
$mailo->IsQmail();
|
|
|
|
}else{
|
|
|
|
$mailo->IsMail();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$mailo->Host = $SMTPHOST;
|
2013-01-02 22:04:08 +04:00
|
|
|
$mailo->Port = $SMTPPORT;
|
2012-04-20 22:49:35 +04:00
|
|
|
$mailo->SMTPAuth = $SMTPAUTH;
|
2013-01-22 17:13:24 +04:00
|
|
|
$mailo->SMTPDebug = $SMTPDEBUG;
|
2015-01-21 16:31:24 +03:00
|
|
|
$mailo->Debugoutput = 'error_log';
|
2013-01-22 17:13:24 +04:00
|
|
|
$mailo->SMTPSecure = $SMTPSECURE;
|
2013-01-26 15:09:54 +04:00
|
|
|
$mailo->AuthType = $SMTPAUTHTYPE;
|
2012-04-20 22:49:35 +04:00
|
|
|
$mailo->Username = $SMTPUSERNAME;
|
|
|
|
$mailo->Password = $SMTPPASSWORD;
|
2013-01-22 17:13:24 +04:00
|
|
|
$mailo->Timeout = $SMTPTIMEOUT;
|
2012-04-20 22:49:35 +04:00
|
|
|
|
2013-01-22 17:13:24 +04:00
|
|
|
$mailo->From = $fromaddress;
|
2012-04-20 22:49:35 +04:00
|
|
|
$mailo->FromName = $fromname;;
|
2013-01-22 17:13:24 +04:00
|
|
|
$mailo->Sender = $fromaddress;
|
2014-08-17 20:55:13 +04:00
|
|
|
$mailo->XMailer = ' ';
|
2012-07-11 23:36:27 +04:00
|
|
|
try {
|
2014-03-24 16:46:31 +04:00
|
|
|
$toaddress = self::buildAsciiEmail($toaddress);
|
2014-03-11 00:56:37 +04:00
|
|
|
$mailo->AddAddress($toaddress, $toname);
|
2012-07-11 23:36:27 +04:00
|
|
|
|
2014-08-04 18:05:09 +04:00
|
|
|
if($ccaddress != '') $mailo->AddCC($ccaddress, $ccname);
|
|
|
|
if($bcc != '') $mailo->AddBCC($bcc);
|
2012-07-11 23:36:27 +04:00
|
|
|
|
|
|
|
$mailo->AddReplyTo($fromaddress, $fromname);
|
|
|
|
|
2014-08-04 18:05:09 +04:00
|
|
|
$mailo->WordWrap = 78;
|
|
|
|
$mailo->IsHTML($html == 1);
|
2012-07-11 23:36:27 +04:00
|
|
|
|
|
|
|
$mailo->Subject = $subject;
|
2014-08-04 18:05:09 +04:00
|
|
|
if($altbody == '') {
|
2012-07-11 23:36:27 +04:00
|
|
|
$mailo->Body = $mailtext.OC_MAIL::getfooter();
|
|
|
|
$mailo->AltBody = '';
|
|
|
|
}else{
|
|
|
|
$mailo->Body = $mailtext;
|
|
|
|
$mailo->AltBody = $altbody;
|
|
|
|
}
|
|
|
|
$mailo->CharSet = 'UTF-8';
|
|
|
|
|
|
|
|
$mailo->Send();
|
|
|
|
unset($mailo);
|
2013-02-11 20:44:02 +04:00
|
|
|
OC_Log::write('mail',
|
|
|
|
'Mail from '.$fromname.' ('.$fromaddress.')'.' to: '.$toname.'('.$toaddress.')'.' subject: '.$subject,
|
|
|
|
OC_Log::DEBUG);
|
2012-07-11 23:36:27 +04:00
|
|
|
} catch (Exception $exception) {
|
2012-07-30 02:01:43 +04:00
|
|
|
OC_Log::write('mail', $exception->getMessage(), OC_Log::ERROR);
|
|
|
|
throw($exception);
|
2012-04-20 22:49:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-06 16:18:30 +04:00
|
|
|
* return the footer for a mail
|
2012-04-20 22:49:35 +04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getfooter() {
|
|
|
|
|
2013-07-03 16:21:51 +04:00
|
|
|
$defaults = new OC_Defaults();
|
2013-07-03 14:38:20 +04:00
|
|
|
|
2013-06-27 20:28:53 +04:00
|
|
|
$txt="\n--\n";
|
2013-07-03 16:21:51 +04:00
|
|
|
$txt.=$defaults->getName() . "\n";
|
|
|
|
$txt.=$defaults->getSlogan() . "\n";
|
2013-06-27 20:28:53 +04:00
|
|
|
|
2012-04-20 22:49:35 +04:00
|
|
|
return($txt);
|
|
|
|
|
|
|
|
}
|
2013-03-05 00:10:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $emailAddress a given email address to be validated
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-03-24 18:35:07 +04:00
|
|
|
public static function validateAddress($emailAddress) {
|
2015-01-22 17:17:29 +03:00
|
|
|
if (strpos($emailAddress, '@') === false) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-24 16:46:31 +04:00
|
|
|
$emailAddress = self::buildAsciiEmail($emailAddress);
|
2013-03-05 00:10:18 +04:00
|
|
|
return PHPMailer::ValidateAddress($emailAddress);
|
|
|
|
}
|
2014-03-24 16:46:31 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* IDN domains will be properly converted to ascii domains.
|
|
|
|
*
|
|
|
|
* @param string $emailAddress
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function buildAsciiEmail($emailAddress) {
|
2014-03-26 14:17:23 +04:00
|
|
|
if (!function_exists('idn_to_ascii')) {
|
|
|
|
return $emailAddress;
|
|
|
|
}
|
2014-03-24 16:46:31 +04:00
|
|
|
|
|
|
|
list($name, $domain) = explode('@', $emailAddress, 2);
|
|
|
|
$domain = idn_to_ascii($domain);
|
|
|
|
|
|
|
|
return "$name@$domain";
|
|
|
|
}
|
|
|
|
|
2012-04-20 22:49:35 +04:00
|
|
|
}
|