Email.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. class Application_Model_Email
  3. {
  4. /**
  5. * Send email
  6. *
  7. * @param string $subject
  8. * @param string $message
  9. * @param mixed $tos
  10. * @return void
  11. */
  12. public static function send($subject, $message, $tos, $from = null)
  13. {
  14. $mailServerConfigured = Application_Model_Preference::GetMailServerConfigured() == true ? true : false;
  15. $mailServerRequiresAuth = Application_Model_Preference::GetMailServerRequiresAuth() == true ? true : false;
  16. $success = true;
  17. if ($mailServerConfigured) {
  18. $mailServer = Application_Model_Preference::GetMailServer();
  19. $mailServerPort = Application_Model_Preference::GetMailServerPort();
  20. if (!empty($mailServerPort)) {
  21. $port = $mailServerPort;
  22. }
  23. if ($mailServerRequiresAuth) {
  24. $username = Application_Model_Preference::GetMailServerEmailAddress();
  25. $password = Application_Model_Preference::GetMailServerPassword();
  26. $config = array(
  27. 'auth' => 'login',
  28. 'ssl' => 'ssl',
  29. 'username' => $username,
  30. 'password' => $password
  31. );
  32. } else {
  33. $config = array(
  34. 'ssl' => 'tls'
  35. );
  36. }
  37. if (isset($port)) {
  38. $config['port'] = $port;
  39. }
  40. $transport = new Zend_Mail_Transport_Smtp($mailServer, $config);
  41. }
  42. $mail = new Zend_Mail('utf-8');
  43. $mail->setSubject($subject);
  44. $mail->setBodyText($message);
  45. foreach ((array) $tos as $to) {
  46. $mail->addTo($to);
  47. }
  48. if ($mailServerConfigured) {
  49. $mail->setFrom(isset($from) ? $from : Application_Model_Preference::GetMailServerEmailAddress());
  50. try {
  51. $mail->send($transport);
  52. } catch (Exception $e) {
  53. $success = false;
  54. }
  55. } else {
  56. $mail->setFrom(isset($from) ? $from : Application_Model_Preference::GetSystemEmail());
  57. try {
  58. $mail->send();
  59. } catch (Exception $e) {
  60. $success = false;
  61. }
  62. }
  63. return $success;
  64. }
  65. }