smtp.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?
  2. /** smtp.php
  3. **
  4. ** This contains all the functions needed to send messages through
  5. ** an smtp server.
  6. **/
  7. function smtpReadData($smtpConnection) {
  8. $read = fgets($smtpConnection, 1024);
  9. $counter = 0;
  10. while ($read) {
  11. echo $read . "<BR>";
  12. $data[$counter] = $read;
  13. $read = fgets($smtpConnection, 1024);
  14. $counter++;
  15. }
  16. }
  17. function sendMessage($smtpServerAddress, $smtpPort, $username, $domain, $t, $c, $b, $subject, $body, $version) {
  18. include("../config/config.php");
  19. $to = parseAddrs($t);
  20. $cc = parseAddrs($c);
  21. $bcc = parseAddrs($b);
  22. $body = stripslashes($body);
  23. $from = "$username@$domain";
  24. echo "<FONT FACE=\"Arial,Helvetica\">";
  25. $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
  26. if (!$smtpConnection) {
  27. echo "Error connecting to SMTP Server.<br>";
  28. echo "$errorNumber : $errorString<br>";
  29. exit;
  30. } else {
  31. $tmp = fgets($smtpConnection, 1024);
  32. }
  33. $to_list = getLineOfAddrs($to);
  34. $cc_list = getLineOfAddrs($cc);
  35. /** Lets introduce ourselves */
  36. fputs($smtpConnection, "HELO $domain\n");
  37. /** Ok, who is sending the message? */
  38. fputs($smtpConnection, "MAIL FROM:<$from>\n");
  39. /** send who the recipients are */
  40. for ($i = 0; $i < count($to); $i++) {
  41. fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
  42. }
  43. for ($i = 0; $i < count($cc); $i++) {
  44. fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
  45. }
  46. for ($i = 0; $i < count($bcc); $i++) {
  47. fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
  48. }
  49. /** Lets start sending the actual message */
  50. fputs($smtpConnection, "DATA\n");
  51. fputs($smtpConnection, "Subject: $subject\n"); // Subject
  52. fputs($smtpConnection, "From: <$from>\n"); // Subject
  53. fputs($smtpConnection, "To: <$to_list>\n"); // Who it's TO
  54. if ($cc_list) {
  55. fputs($smtpConnection, "Cc: <$cc_list>\n"); // Who the CCs are
  56. }
  57. fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
  58. fputs($smtpConnection, "Reply-To: $from\n");
  59. fputs($smtpConnection, "MIME-Version: 1.0\n");
  60. fputs($smtpConnection, "Content-Type: text/plain\n");
  61. fputs($smtpConnection, "$body\n"); // send the body of the message
  62. fputs($smtpConnection, ".\n"); // end the DATA part
  63. fputs($smtpConnection, "QUIT\n"); // log off
  64. echo "</FONT>";
  65. fclose($smtpConnection);
  66. }
  67. ?>