smtp.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. $to = parseAddrs($t);
  19. $cc = parseAddrs($c);
  20. $bcc = parseAddrs($b);
  21. $body = stripslashes($body);
  22. $from = "$username@$domain";
  23. echo "<FONT FACE=\"Arial,Helvetica\">";
  24. $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
  25. if (!$smtpConnection) {
  26. echo "Error connecting to SMTP Server.<br>";
  27. echo "$errorNumber : $errorString<br>";
  28. exit;
  29. }
  30. $to_list = getLineOfAddrs($to);
  31. $cc_list = getLineOfAddrs($cc);
  32. /** Lets introduce ourselves */
  33. fputs($smtpConnection, "HELO $domain\n");
  34. /** Ok, who is sending the message? */
  35. fputs($smtpConnection, "MAIL FROM:<$from>\n");
  36. /** send who the recipients are */
  37. for ($i = 0; $i < count($to); $i++) {
  38. fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
  39. }
  40. for ($i = 0; $i < count($cc); $i++) {
  41. fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
  42. }
  43. for ($i = 0; $i < count($bcc); $i++) {
  44. fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
  45. }
  46. /** Lets start sending the actual message */
  47. fputs($smtpConnection, "DATA\n");
  48. fputs($smtpConnection, "Subject: $subject\n"); // Subject
  49. fputs($smtpConnection, "To: <$to_list>\n"); // Who it's TO
  50. if ($cc_list) {
  51. fputs($smtpConnection, "Cc: <$cc_list>\n"); // Who the CCs are
  52. }
  53. fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
  54. fputs($smtpConnection, "$body\n"); // send the body of the message
  55. fputs($smtpConnection, ".\n"); // end the DATA part
  56. fputs($smtpConnection, "QUIT\n"); // log off
  57. echo "</FONT>";
  58. }
  59. ?>