You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mail.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. require_once 'class.phpmailer.php';
  14. class OC_Mail {
  15. /**
  16. * send an email
  17. *
  18. * @param string $toaddress
  19. * @param string $toname
  20. * @param string $subject
  21. * @param string $mailtext
  22. * @param string $fromaddress
  23. * @param string $fromname
  24. * @param bool|int $html
  25. * @param string $altbody
  26. * @param string $ccaddress
  27. * @param string $ccname
  28. * @param string $bcc
  29. * @throws Exception
  30. */
  31. public static function send($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
  32. $html=0, $altbody='', $ccaddress='', $ccname='', $bcc='') {
  33. $SMTPMODE = OC_Config::getValue( 'mail_smtpmode', 'sendmail' );
  34. $SMTPHOST = OC_Config::getValue( 'mail_smtphost', '127.0.0.1' );
  35. $SMTPPORT = OC_Config::getValue( 'mail_smtpport', 25 );
  36. $SMTPAUTH = OC_Config::getValue( 'mail_smtpauth', false );
  37. $SMTPAUTHTYPE = OC_Config::getValue( 'mail_smtpauthtype', 'LOGIN' );
  38. $SMTPUSERNAME = OC_Config::getValue( 'mail_smtpname', '' );
  39. $SMTPPASSWORD = OC_Config::getValue( 'mail_smtppassword', '' );
  40. $SMTPDEBUG = OC_Config::getValue( 'mail_smtpdebug', false );
  41. $SMTPTIMEOUT = OC_Config::getValue( 'mail_smtptimeout', 10 );
  42. $SMTPSECURE = OC_Config::getValue( 'mail_smtpsecure', '' );
  43. $mailo = new PHPMailer(true);
  44. if($SMTPMODE=='sendmail') {
  45. $mailo->IsSendmail();
  46. }elseif($SMTPMODE=='smtp') {
  47. $mailo->IsSMTP();
  48. }elseif($SMTPMODE=='qmail') {
  49. $mailo->IsQmail();
  50. }else{
  51. $mailo->IsMail();
  52. }
  53. $mailo->Host = $SMTPHOST;
  54. $mailo->Port = $SMTPPORT;
  55. $mailo->SMTPAuth = $SMTPAUTH;
  56. $mailo->SMTPDebug = $SMTPDEBUG;
  57. $mailo->SMTPSecure = $SMTPSECURE;
  58. $mailo->AuthType = $SMTPAUTHTYPE;
  59. $mailo->Username = $SMTPUSERNAME;
  60. $mailo->Password = $SMTPPASSWORD;
  61. $mailo->Timeout = $SMTPTIMEOUT;
  62. $mailo->From = $fromaddress;
  63. $mailo->FromName = $fromname;;
  64. $mailo->Sender = $fromaddress;
  65. $a=explode(' ', $toaddress);
  66. try {
  67. foreach($a as $ad) {
  68. $mailo->AddAddress($ad, $toname);
  69. }
  70. if($ccaddress<>'') $mailo->AddCC($ccaddress, $ccname);
  71. if($bcc<>'') $mailo->AddBCC($bcc);
  72. $mailo->AddReplyTo($fromaddress, $fromname);
  73. $mailo->WordWrap = 50;
  74. if($html==1) $mailo->IsHTML(true); else $mailo->IsHTML(false);
  75. $mailo->Subject = $subject;
  76. if($altbody=='') {
  77. $mailo->Body = $mailtext.OC_MAIL::getfooter();
  78. $mailo->AltBody = '';
  79. }else{
  80. $mailo->Body = $mailtext;
  81. $mailo->AltBody = $altbody;
  82. }
  83. $mailo->CharSet = 'UTF-8';
  84. $mailo->Send();
  85. unset($mailo);
  86. OC_Log::write('mail',
  87. 'Mail from '.$fromname.' ('.$fromaddress.')'.' to: '.$toname.'('.$toaddress.')'.' subject: '.$subject,
  88. OC_Log::DEBUG);
  89. } catch (Exception $exception) {
  90. OC_Log::write('mail', $exception->getMessage(), OC_Log::ERROR);
  91. throw($exception);
  92. }
  93. }
  94. /**
  95. * return the footer for a mail
  96. *
  97. */
  98. public static function getfooter() {
  99. $defaults = new OC_Defaults();
  100. $txt="\n--\n";
  101. $txt.=$defaults->getName() . "\n";
  102. $txt.=$defaults->getSlogan() . "\n";
  103. return($txt);
  104. }
  105. /**
  106. * @param string $emailAddress a given email address to be validated
  107. * @return bool
  108. */
  109. public static function ValidateAddress($emailAddress) {
  110. return PHPMailer::ValidateAddress($emailAddress);
  111. }
  112. }