PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/MODULES/Mail/Mail.php

https://bitbucket.org/smr/omc
PHP | 140 lines | 99 code | 27 blank | 14 comment | 10 complexity | 0d63d0ee4b44f86db9c80be15345501e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * Class that deals mainly with email sending, aiming to provide a small API to PEAR's SASL/SMTP monster classes
  4. * @author Victor Nitu
  5. * @desc Class that deals mainly with email sending, aiming to provide a small API to PEAR's SASL/SMTP monster classes
  6. *
  7. */
  8. class Mail
  9. {
  10. const CRLF = "\r\n";
  11. private
  12. $Server, $Port, $Localhost,
  13. $skt;
  14. public
  15. $Username, $Password, $ConnectTimeout, $ResponseTimeout,
  16. $Headers, $ContentType, $From, $To, $Cc, $Subject, $Message,
  17. $Log;
  18. function __construct($server = "127.0.0.1", $port = 25)
  19. {
  20. $this->Server = $server;
  21. $this->Port = $port;
  22. $this->Localhost = "localhost";
  23. $this->ConnectTimeout = 30;
  24. $this->ResponseTimeout = 8;
  25. $this->From = array();
  26. $this->To = array();
  27. $this->Cc = array();
  28. $this->Log = array();
  29. $this->Headers['MIME-Version'] = "1.0";
  30. $this->Headers['Content-type'] = "text/plain; charset=utf-8";
  31. }
  32. private function GetResponse()
  33. {
  34. stream_set_timeout($this->skt, $this->ResponseTimeout);
  35. $response = '';
  36. while (($line = fgets($this->skt, 515)) != false)
  37. {
  38. $response .= trim($line) . "\n";
  39. if (substr($line,3,1)==' ') break;
  40. }
  41. return trim($response);
  42. }
  43. private function SendCMD($CMD)
  44. {
  45. fputs($this->skt, $CMD . self::CRLF);
  46. return $this->GetResponse();
  47. }
  48. private function FmtAddr(&$addr)
  49. {
  50. if ($addr[1] == "") return $addr[0]; else return "\"{$addr[1]}\" <{$addr[0]}>";
  51. }
  52. private function FmtAddrList(&$addrs)
  53. {
  54. $list = "";
  55. foreach ($addrs as $addr)
  56. {
  57. if ($list) $list .= ", ".self::CRLF."\t";
  58. $list .= $this->FmtAddr($addr);
  59. }
  60. return $list;
  61. }
  62. function AddTo($addr,$name = "")
  63. {
  64. $this->To[] = array($addr,$name);
  65. }
  66. function AddCc($addr,$name = "")
  67. {
  68. $this->Cc[] = array($addr,$name);
  69. }
  70. function SetFrom($addr,$name = "")
  71. {
  72. $this->From = array($addr,$name);
  73. }
  74. function Send()
  75. {
  76. $newLine = self::CRLF;
  77. //Connect to the host on the specified port
  78. $this->skt = fsockopen($this->Server, $this->Port, $errno, $errstr, $this->ConnectTimeout);
  79. if (empty($this->skt))
  80. return false;
  81. $this->Log['connection'] = $this->GetResponse();
  82. //Say Hello to SMTP
  83. $this->Log['helo'] = $this->SendCMD("EHLO {$this->Server}");
  84. //Request Auth Login
  85. $this->Log['auth'] = $this->SendCMD("AUTH LOGIN");
  86. $this->Log['username'] = $this->SendCMD(base64_encode($this->Username));
  87. $this->Log['password'] = $this->SendCMD(base64_encode($this->Password));
  88. //Email From
  89. $this->Log['mailfrom'] = $this->SendCMD("MAIL FROM:<{$this->From[0]}>");
  90. //Email To
  91. $i = 1;
  92. foreach (array_merge($this->To,$this->Cc) as $addr)
  93. $this->Log['rcptto'.$i++] = $this->SendCMD("RCPT TO:<{$addr[0]}>");
  94. //The Email
  95. $this->Log['data1'] = $this->SendCMD("DATA");
  96. //Construct Headers
  97. if (!empty($this->ContentType))
  98. $this->Headers['Content-type'] = $this->ContentType;
  99. $this->Headers['From'] = $this->FmtAddr($this->From);
  100. $this->Headers['To'] = $this->FmtAddrList($this->To);
  101. if (!empty($this->Cc))
  102. $this->Headers['Cc'] = $this->FmtAddrList($this->Cc);
  103. $this->Headers['Subject'] = $this->Subject;
  104. $this->Headers['Date'] = date('r');
  105. $headers = '';
  106. foreach ($this->Headers as $key => $val)
  107. $headers .= $key . ': ' . $val . self::CRLF;
  108. $this->Log['data2'] = $this->SendCMD("{$headers}{$newLine}{$this->Message}{$newLine}.");
  109. // Say Bye to SMTP
  110. $this->Log['quit'] = $this->SendCMD("QUIT");
  111. fclose($this->skt);
  112. }
  113. }