-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmail.php
More file actions
70 lines (50 loc) · 1.85 KB
/
mail.php
File metadata and controls
70 lines (50 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/*
* This software is the property of its authors.
* See the copyright.txt file for more details.
*
*/
function send_mail ($addresses, $subject, $message, $headers) {
foreach ((array) $addresses as $address) {
assertTrue(strpos($address, '@') !== false);
$domain = substr($address, strpos($address, '@') + 1);
assertTrue(getmxrr($domain, $mx) && count($mx) > 0);
$stream = fsockopen($mx[0], 25, $errno, $errstr, 3);
assertTrue($stream);
assertTrue($errno == 0);
$lcheaders = array();
foreach ($headers as $k => $v)
$lcheaders[strtolower($k)] = $v;
// TODO: autocreate if not exists
assertTrue(isset($lcheaders['from']));
fwrite($stream, "HELO " . htmlspecialchars($domain) . "\r\n");
send_mail_process_response($stream, "250");
fwrite($stream, "MAIL FROM: <" . $lcheaders['from'] . ">\r\n");
send_mail_process_response($stream, "250");
fwrite($stream, "RCPT TO: <" . $address . ">\r\n");
send_mail_process_response($stream, "250");
fwrite($stream, "DATA\r\n");
send_mail_process_response($stream, "354");
foreach ($headers as $k => $v)
fwrite($stream, "$k: $v\r\n");
fwrite($stream, "Date: " . date('r') . "\r\n");
fwrite($stream, "Subject: " . preg_replace('/\r?\n/', ' ', $subject) . "\r\n");
fwrite($stream, "To: <" . $address . ">\r\n");
fwrite($stream, "\r\n" . trim($message) . "\r\n");
fwrite($stream, ".\r\n");
send_mail_process_response($stream, "250");
fwrite($stream, "QUIT\r\n");
send_mail_process_response($stream, "221");
}
}
function send_mail_process_response ($stream, $exceptedCode) {
while (true) {
$line = trim(fread($stream, 1024));
$code = substr($line, 0, 3);
if ($code == $exceptedCode)
return;
if ($code >= 200 && $code <= 299)
continue;
assertTrue(false);
}
}