Added "Themes" to personal preferences
[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 include("../functions/prefs.php");
22
23 $to = parseAddrs($t);
24 $cc = parseAddrs($c);
25 $bcc = parseAddrs($b);
26 $body = stripslashes($body);
27 $from_addr = "$username@$domain";
28 $reply_to = getPref($username, "reply_to");
29 $from = getPref($username, "full_name");
30 if ($from == "")
31 $from = "<$username@$domain>";
32 else
33 $from = $from . " <$username@$domain>";
34
35
36 $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString);
37 if (!$smtpConnection) {
38 echo "Error connecting to SMTP Server.<br>";
39 echo "$errorNumber : $errorString<br>";
40 exit;
41 }
42 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
43
44 $to_list = getLineOfAddrs($to);
45 $cc_list = getLineOfAddrs($cc);
46
47 /** Lets introduce ourselves */
48 fputs($smtpConnection, "HELO $domain\n");
49 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
50
51 /** Ok, who is sending the message? */
52 fputs($smtpConnection, "MAIL FROM:$from_addr\n");
53 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
54
55 /** send who the recipients are */
56 for ($i = 0; $i < count($to); $i++) {
57 fputs($smtpConnection, "RCPT TO:<$to[$i]>\n");
58 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
59 }
60 for ($i = 0; $i < count($cc); $i++) {
61 fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n");
62 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
63 }
64 for ($i = 0; $i < count($bcc); $i++) {
65 fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n");
66 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
67 }
68
69 /** Lets start sending the actual message */
70 fputs($smtpConnection, "DATA\n");
71 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
72
73 fputs($smtpConnection, "Subject: $subject\n"); // Subject
74 fputs($smtpConnection, "From: $from\n"); // Subject
75 fputs($smtpConnection, "To: <$to_list>\n"); // Who it's TO
76
77 if ($cc_list) {
78 fputs($smtpConnection, "Cc: <$cc_list>\n"); // Who the CCs are
79 }
80 fputs($smtpConnection, "X-Mailer: SquirrelMail (version $version)\n"); // Identify SquirrelMail
81 fputs($smtpConnection, "Reply-To: $reply_to\n");
82 fputs($smtpConnection, "MIME-Version: 1.0\n");
83 fputs($smtpConnection, "Content-Type: text/plain\n");
84
85 fputs($smtpConnection, "$body\n"); // send the body of the message
86
87 fputs($smtpConnection, ".\n"); // end the DATA part
88 $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024)));
89
90 fputs($smtpConnection, "QUIT\n"); // log off
91
92 fclose($smtpConnection);
93 }
94 ?>