PageRenderTime 64ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/class.smtp.php

https://bitbucket.org/wooptoo/secure-smtp-client
PHP | 122 lines | 79 code | 9 blank | 34 comment | 6 complexity | 2ffdd75fc434c0909ce42277512074ff MD5 | raw file
  1. <?php
  2. /**
  3. * Send messages using a local or remote SMTP server.
  4. * It supports TLS and SSL crypto.
  5. * @class Smtp
  6. * @author Radu Potop <wooptoo@gmail.com>
  7. * @license BSD
  8. */
  9. class Smtp {
  10. public $server;
  11. public $port;
  12. public $crypto;
  13. public $user;
  14. public $pass;
  15. private $timeout = '45';
  16. private $localhost = 'localhost';
  17. private $nl = "\r\n";
  18. private $conn;
  19. /**
  20. * Connect and Auth to server.
  21. *
  22. * @param string $server - remote server address or 'localhost'
  23. * @param int $port
  24. * @param string $crypto - can be null, ssl, tls
  25. * @param string $user - optional for localhost server
  26. * @param string $pass - optional for localhost server
  27. */
  28. function __construct($server, $port, $crypto=null, $user=null, $pass=null) {
  29. $this->server = $server;
  30. $this->port = $port;
  31. $this->crypto = $crypto;
  32. $this->user = $user;
  33. $this->pass = $pass;
  34. $this->connect();
  35. $this->auth();
  36. }
  37. /**
  38. * Connect to server.
  39. */
  40. function connect() {
  41. $this->crypto = strtolower(trim($this->crypto));
  42. $this->server = strtolower(trim($this->server));
  43. if($this->crypto == 'ssl')
  44. $this->server = 'ssl://' . $this->server;
  45. $this->conn = fsockopen(
  46. $this->server, $this->port, $errno, $errstr, $this->timeout
  47. );
  48. fgets($this->conn);
  49. return;
  50. }
  51. /**
  52. * Auth.
  53. */
  54. function auth() {
  55. fputs($this->conn, 'HELO ' . $this->localhost . $this->nl);
  56. fgets($this->conn);
  57. if($this->crypto == 'tls') {
  58. fputs($this->conn, 'STARTTLS' . $this->nl);
  59. fgets($this->conn);
  60. stream_socket_enable_crypto(
  61. $this->conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT
  62. );
  63. fputs($this->conn, 'HELO ' . $this->localhost . $this->nl);
  64. fgets($this->conn);
  65. }
  66. if($this->server != 'localhost') {
  67. fputs($this->conn, 'AUTH LOGIN' . $this->nl);
  68. fgets($this->conn);
  69. fputs($this->conn, base64_encode($this->user) . $this->nl);
  70. fgets($this->conn);
  71. fputs($this->conn, base64_encode($this->pass) . $this->nl);
  72. fgets($this->conn);
  73. }
  74. return;
  75. }
  76. /**
  77. * Send an email.
  78. *
  79. * @param string $from
  80. * @param string $to
  81. * @param string $subject
  82. * @param string $message
  83. * @param string $headers - optional
  84. */
  85. function send($from, $to, $subject, $message, $headers=null) {
  86. fputs($this->conn, 'MAIL FROM: <'. $from .'>'. $this->nl);
  87. fgets($this->conn);
  88. fputs($this->conn, 'RCPT TO: <'. $to .'>'. $this->nl);
  89. fgets($this->conn);
  90. fputs($this->conn, 'DATA'. $this->nl);
  91. fgets($this->conn);
  92. fputs($this->conn,
  93. 'From: '. $from .$this->nl.
  94. 'To: '. $to .$this->nl.
  95. 'Subject: '. $subject .$this->nl.
  96. $headers .$this->nl.
  97. $this->nl.
  98. $message . $this->nl.
  99. '.' .$this->nl
  100. );
  101. fgets($this->conn);
  102. return;
  103. }
  104. /**
  105. * Quit and disconnect.
  106. */
  107. function __destruct() {
  108. fputs($this->conn, 'QUIT' . $this->nl);
  109. fgets($this->conn);
  110. fclose($this->conn);
  111. }
  112. }
  113. ?>