mail.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Frank Karlitschek <frank@owncloud.org>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * OC_Mail
  10. *
  11. * A class to handle mail sending.
  12. */
  13. class OC_Mail {
  14. /**
  15. * send an email
  16. *
  17. * @param string $toaddress
  18. * @param string $toname
  19. * @param string $subject
  20. * @param string $mailtext
  21. * @param string $fromaddress
  22. * @param string $fromname
  23. * @param integer $html
  24. * @param string $altbody
  25. * @param string $ccaddress
  26. * @param string $ccname
  27. * @param string $bcc
  28. * @throws Exception
  29. */
  30. public static function send($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
  31. $html=0, $altbody='', $ccaddress='', $ccname='', $bcc='') {
  32. $SMTPMODE = OC_Config::getValue( 'mail_smtpmode', 'sendmail' );
  33. $SMTPHOST = OC_Config::getValue( 'mail_smtphost', '127.0.0.1' );
  34. $SMTPPORT = OC_Config::getValue( 'mail_smtpport', 25 );
  35. $SMTPAUTH = OC_Config::getValue( 'mail_smtpauth', false );
  36. $SMTPAUTHTYPE = OC_Config::getValue( 'mail_smtpauthtype', 'LOGIN' );
  37. $SMTPUSERNAME = OC_Config::getValue( 'mail_smtpname', '' );
  38. $SMTPPASSWORD = OC_Config::getValue( 'mail_smtppassword', '' );
  39. $SMTPDEBUG = OC_Config::getValue( 'mail_smtpdebug', false );
  40. $SMTPTIMEOUT = OC_Config::getValue( 'mail_smtptimeout', 10 );
  41. $SMTPSECURE = OC_Config::getValue( 'mail_smtpsecure', '' );
  42. $mailo = new PHPMailer(true);
  43. if($SMTPMODE=='sendmail') {
  44. $mailo->IsSendmail();
  45. }elseif($SMTPMODE=='smtp') {
  46. $mailo->IsSMTP();
  47. }elseif($SMTPMODE=='qmail') {
  48. $mailo->IsQmail();
  49. }else{
  50. $mailo->IsMail();
  51. }
  52. $mailo->Host = $SMTPHOST;
  53. $mailo->Port = $SMTPPORT;
  54. $mailo->SMTPAuth = $SMTPAUTH;
  55. $mailo->SMTPDebug = $SMTPDEBUG;
  56. $mailo->SMTPSecure = $SMTPSECURE;
  57. $mailo->AuthType = $SMTPAUTHTYPE;
  58. $mailo->Username = $SMTPUSERNAME;
  59. $mailo->Password = $SMTPPASSWORD;
  60. $mailo->Timeout = $SMTPTIMEOUT;
  61. $mailo->From = $fromaddress;
  62. $mailo->FromName = $fromname;;
  63. $mailo->Sender = $fromaddress;
  64. try {
  65. $toaddress = self::buildAsciiEmail($toaddress);
  66. $mailo->AddAddress($toaddress, $toname);
  67. if($ccaddress<>'') $mailo->AddCC($ccaddress, $ccname);
  68. if($bcc<>'') $mailo->AddBCC($bcc);
  69. $mailo->AddReplyTo($fromaddress, $fromname);
  70. $mailo->WordWrap = 50;
  71. if($html==1) $mailo->IsHTML(true); else $mailo->IsHTML(false);
  72. $mailo->Subject = $subject;
  73. if($altbody=='') {
  74. $mailo->Body = $mailtext.OC_MAIL::getfooter();
  75. $mailo->AltBody = '';
  76. }else{
  77. $mailo->Body = $mailtext;
  78. $mailo->AltBody = $altbody;
  79. }
  80. $mailo->CharSet = 'UTF-8';
  81. $mailo->Send();
  82. unset($mailo);
  83. OC_Log::write('mail',
  84. 'Mail from '.$fromname.' ('.$fromaddress.')'.' to: '.$toname.'('.$toaddress.')'.' subject: '.$subject,
  85. OC_Log::DEBUG);
  86. } catch (Exception $exception) {
  87. OC_Log::write('mail', $exception->getMessage(), OC_Log::ERROR);
  88. throw($exception);
  89. }
  90. }
  91. /**
  92. * return the footer for a mail
  93. *
  94. */
  95. public static function getfooter() {
  96. $defaults = new OC_Defaults();
  97. $txt="\n--\n";
  98. $txt.=$defaults->getName() . "\n";
  99. $txt.=$defaults->getSlogan() . "\n";
  100. return($txt);
  101. }
  102. /**
  103. * @param string $emailAddress a given email address to be validated
  104. * @return bool
  105. */
  106. public static function validateAddress($emailAddress) {
  107. $emailAddress = self::buildAsciiEmail($emailAddress);
  108. return PHPMailer::ValidateAddress($emailAddress);
  109. }
  110. /**
  111. * IDN domains will be properly converted to ascii domains.
  112. *
  113. * @param string $emailAddress
  114. * @return string
  115. */
  116. public static function buildAsciiEmail($emailAddress) {
  117. if (!function_exists('idn_to_ascii')) {
  118. return $emailAddress;
  119. }
  120. list($name, $domain) = explode('@', $emailAddress, 2);
  121. $domain = idn_to_ascii($domain);
  122. return "$name@$domain";
  123. }
  124. }