Blame view

sources/lib/private/mail.php 3.76 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
  <?php
  /**
   * 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
   *
   * A class to handle mail sending.
   */
03e52840d   Kload   Init
14
15
16
17
18
19
20
21
22
23
24
  class OC_Mail {
  
  	/**
  	 * send an email
  	 *
  	 * @param string $toaddress
  	 * @param string $toname
  	 * @param string $subject
  	 * @param string $mailtext
  	 * @param string $fromaddress
  	 * @param string $fromname
6d9380f96   Cédric Dupont   Update sources OC...
25
  	 * @param integer $html
03e52840d   Kload   Init
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  	 * @param string $altbody
  	 * @param string $ccaddress
  	 * @param string $ccname
  	 * @param string $bcc
  	 * @throws Exception
  	 */
  	public static function send($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
  		$html=0, $altbody='', $ccaddress='', $ccname='', $bcc='') {
  
  		$SMTPMODE = OC_Config::getValue( 'mail_smtpmode', 'sendmail' );
  		$SMTPHOST = OC_Config::getValue( 'mail_smtphost', '127.0.0.1' );
  		$SMTPPORT = OC_Config::getValue( 'mail_smtpport', 25 );
  		$SMTPAUTH = OC_Config::getValue( 'mail_smtpauth', false );
  		$SMTPAUTHTYPE = OC_Config::getValue( 'mail_smtpauthtype', 'LOGIN' );
  		$SMTPUSERNAME = OC_Config::getValue( 'mail_smtpname', '' );
  		$SMTPPASSWORD = OC_Config::getValue( 'mail_smtppassword', '' );
  		$SMTPDEBUG    = OC_Config::getValue( 'mail_smtpdebug', false );
  		$SMTPTIMEOUT  = OC_Config::getValue( 'mail_smtptimeout', 10 );
  		$SMTPSECURE   = OC_Config::getValue( 'mail_smtpsecure', '' );
  
  
  		$mailo = new PHPMailer(true);
  		if($SMTPMODE=='sendmail') {
  			$mailo->IsSendmail();
  		}elseif($SMTPMODE=='smtp') {
  			$mailo->IsSMTP();
  		}elseif($SMTPMODE=='qmail') {
  			$mailo->IsQmail();
  		}else{
  			$mailo->IsMail();
  		}
  
  
  		$mailo->Host = $SMTPHOST;
  		$mailo->Port = $SMTPPORT;
  		$mailo->SMTPAuth = $SMTPAUTH;
  		$mailo->SMTPDebug = $SMTPDEBUG;
  		$mailo->SMTPSecure = $SMTPSECURE;
  		$mailo->AuthType = $SMTPAUTHTYPE;
  		$mailo->Username = $SMTPUSERNAME;
  		$mailo->Password = $SMTPPASSWORD;
  		$mailo->Timeout  = $SMTPTIMEOUT;
  
  		$mailo->From = $fromaddress;
  		$mailo->FromName = $fromname;;
  		$mailo->Sender = $fromaddress;
03e52840d   Kload   Init
72
  		try {
6d9380f96   Cédric Dupont   Update sources OC...
73
  			$toaddress = self::buildAsciiEmail($toaddress);
837968727   Kload   [enh] Upgrade to ...
74
  			$mailo->AddAddress($toaddress, $toname);
03e52840d   Kload   Init
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  
  			if($ccaddress<>'') $mailo->AddCC($ccaddress, $ccname);
  			if($bcc<>'') $mailo->AddBCC($bcc);
  
  			$mailo->AddReplyTo($fromaddress, $fromname);
  
  			$mailo->WordWrap = 50;
  			if($html==1) $mailo->IsHTML(true); else $mailo->IsHTML(false);
  
  			$mailo->Subject = $subject;
  			if($altbody=='') {
  				$mailo->Body    = $mailtext.OC_MAIL::getfooter();
  				$mailo->AltBody = '';
  			}else{
  				$mailo->Body    = $mailtext;
  				$mailo->AltBody = $altbody;
  			}
  			$mailo->CharSet = 'UTF-8';
  
  			$mailo->Send();
  			unset($mailo);
  			OC_Log::write('mail',
  				'Mail from '.$fromname.' ('.$fromaddress.')'.' to: '.$toname.'('.$toaddress.')'.' subject: '.$subject,
  				OC_Log::DEBUG);
  		} catch (Exception $exception) {
  			OC_Log::write('mail', $exception->getMessage(), OC_Log::ERROR);
  			throw($exception);
  		}
  	}
  
  	/**
  	 * return the footer for a mail
  	 *
  	 */
  	public static function getfooter() {
  
  		$defaults = new OC_Defaults();
  
  		$txt="
  --
  ";
  		$txt.=$defaults->getName() . "
  ";
  		$txt.=$defaults->getSlogan() . "
  ";
31b7f2792   Kload   Upgrade to ownclo...
120

03e52840d   Kload   Init
121
122
123
124
125
126
127
128
  		return($txt);
  
  	}
  
  	/**
  	 * @param string $emailAddress a given email address to be validated
  	 * @return bool
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
129
130
  	public static function validateAddress($emailAddress) {
  		$emailAddress = self::buildAsciiEmail($emailAddress);
03e52840d   Kload   Init
131
132
  		return PHPMailer::ValidateAddress($emailAddress);
  	}
6d9380f96   Cédric Dupont   Update sources OC...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  
  	/**
  	 * IDN domains will be properly converted to ascii domains.
  	 *
  	 * @param string $emailAddress
  	 * @return string
  	 */
  	public static function buildAsciiEmail($emailAddress) {
  		if (!function_exists('idn_to_ascii')) {
  			return $emailAddress;
  		}
  
  		list($name, $domain) = explode('@', $emailAddress, 2);
  		$domain = idn_to_ascii($domain);
  
  		return "$name@$domain";
  	}
03e52840d   Kload   Init
150
  }