b8ea4ed6 |
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 | |
40ee9452 |
19 | function sendMessage($smtpServerAddress, $smtpPort, $username, $domain, $t, $c, $b, $subject, $body, $version) { |
7831268e |
20 | include("../config/config.php"); |
d3cdb279 |
21 | include("../functions/prefs.php"); |
7831268e |
22 | |
40ee9452 |
23 | $to = parseAddrs($t); |
24 | $cc = parseAddrs($c); |
25 | $bcc = parseAddrs($b); |
4f96d941 |
26 | $body = stripslashes($body); |
d3cdb279 |
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 | |
c175e2e4 |
35 | |
c175e2e4 |
36 | $smtpConnection = fsockopen($smtpServerAddress, $smtpPort, $errorNumber, $errorString); |
b8ea4ed6 |
37 | if (!$smtpConnection) { |
38 | echo "Error connecting to SMTP Server.<br>"; |
39 | echo "$errorNumber : $errorString<br>"; |
40 | exit; |
41 | } |
d3cdb279 |
42 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
b8ea4ed6 |
43 | |
40ee9452 |
44 | $to_list = getLineOfAddrs($to); |
45 | $cc_list = getLineOfAddrs($cc); |
46 | |
47 | /** Lets introduce ourselves */ |
48 | fputs($smtpConnection, "HELO $domain\n"); |
d3cdb279 |
49 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
7831268e |
50 | |
40ee9452 |
51 | /** Ok, who is sending the message? */ |
d3cdb279 |
52 | fputs($smtpConnection, "MAIL FROM:$from_addr\n"); |
53 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
b8ea4ed6 |
54 | |
40ee9452 |
55 | /** send who the recipients are */ |
56 | for ($i = 0; $i < count($to); $i++) { |
57 | fputs($smtpConnection, "RCPT TO:<$to[$i]>\n"); |
d3cdb279 |
58 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
40ee9452 |
59 | } |
60 | for ($i = 0; $i < count($cc); $i++) { |
61 | fputs($smtpConnection, "RCPT TO:<$cc[$i]>\n"); |
d3cdb279 |
62 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
40ee9452 |
63 | } |
64 | for ($i = 0; $i < count($bcc); $i++) { |
65 | fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\n"); |
d3cdb279 |
66 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
40ee9452 |
67 | } |
b8ea4ed6 |
68 | |
40ee9452 |
69 | /** Lets start sending the actual message */ |
b8ea4ed6 |
70 | fputs($smtpConnection, "DATA\n"); |
d3cdb279 |
71 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
7831268e |
72 | |
40ee9452 |
73 | fputs($smtpConnection, "Subject: $subject\n"); // Subject |
d3cdb279 |
74 | fputs($smtpConnection, "From: $from\n"); // Subject |
40ee9452 |
75 | fputs($smtpConnection, "To: <$to_list>\n"); // Who it's TO |
7831268e |
76 | |
40ee9452 |
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 |
d3cdb279 |
81 | fputs($smtpConnection, "Reply-To: $reply_to\n"); |
7ce342dc |
82 | fputs($smtpConnection, "MIME-Version: 1.0\n"); |
4809f489 |
83 | fputs($smtpConnection, "Content-Type: text/plain\n"); |
40ee9452 |
84 | |
85 | fputs($smtpConnection, "$body\n"); // send the body of the message |
7831268e |
86 | |
40ee9452 |
87 | fputs($smtpConnection, ".\n"); // end the DATA part |
d3cdb279 |
88 | $tmp = nl2br(htmlspecialchars(fgets($smtpConnection, 1024))); |
89 | |
40ee9452 |
90 | fputs($smtpConnection, "QUIT\n"); // log off |
78509c54 |
91 | |
92 | fclose($smtpConnection); |
b8ea4ed6 |
93 | } |
94 | ?> |