/webapp/_lib/model/class.Mailer.php

https://github.com/billfox3/ThinkUp · PHP · 88 lines · 38 code · 3 blank · 47 comment · 9 complexity · ada2a54eafbc0e44430078977d659e2e MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/_lib/model/class.Mailer.php
  5. *
  6. * Copyright (c) 2009-2011 Gina Trapani
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  25. * @license http://www.gnu.org/licenses/gpl.html
  26. * @copyright 2009-2011 Gina Trapani
  27. */
  28. class Mailer {
  29. /**
  30. * For testing purposes only; this is the name of the file the latest email gets written to.
  31. * @var str
  32. */
  33. const EMAIL = '/latest_email';
  34. /**
  35. * Send email from ThinkUp instalation. If you're running tests, just write the message headers and contents to
  36. * the file system in the compiled_view folder.
  37. * @param str $to A valid email address
  38. * @param str $subject
  39. * @param str $message
  40. */
  41. public static function mail($to, $subject, $message) {
  42. $config = Config::getInstance();
  43. $app_title = $config->getValue('app_title');
  44. $host = self::getHost();
  45. $mail_header = "From: \"{$app_title}\" <notifications@{$host}>\r\n";
  46. $mail_header .= "X-Mailer: PHP/".phpversion();
  47. //don't send email when running tests, just write it to the filesystem for assertions
  48. if ((isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS") || getenv("MODE")=="TESTS") {
  49. $test_email = THINKUP_WEBAPP_PATH . '_lib/view/compiled_view' . Mailer::EMAIL;
  50. $fp = fopen($test_email, 'w');
  51. fwrite($fp, $mail_header."\n");
  52. fwrite($fp, "to: $to\n");
  53. fwrite($fp, "subject: $subject\n");
  54. fwrite($fp, "message: $message");
  55. fclose($fp);
  56. return $message;
  57. } else {
  58. mail($to, $subject, $message, $mail_header);
  59. }
  60. }
  61. /**
  62. * Return the current host's name, ie, $_SERVER['HTTP_HOST'] if it is set.
  63. * @return str Host name
  64. */
  65. private static function getHost() {
  66. if (isset($_SERVER['HTTP_HOST'])) {
  67. return $_SERVER['HTTP_HOST'];
  68. } else {
  69. return "";
  70. }
  71. }
  72. /**
  73. * Return the contents of the last email Mailer "sent" out.
  74. * For testing purposes only; this will return nothing in production.
  75. * @return str The contents of the last email sent
  76. */
  77. public static function getLastMail() {
  78. $test_email_file = THINKUP_WEBAPP_PATH . '_lib/view/compiled_view' . Mailer::EMAIL;
  79. if (file_exists($test_email_file)) {
  80. return file_get_contents($test_email_file);
  81. } else {
  82. return '';
  83. }
  84. }
  85. }