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