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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
| <?php
class email {
#Init------------------------------------------------------------------------------------------
function __construct() {
#import settings
include_once("settings.php");
}
#Send email ------------------------------------------------------------------------------------
function send_email($from, $to, $subject, $text, $html=false ,$attachment_content=false, $attachment_mimetype=false, $attachment_filename=false ) {
#init
$mime_boundary=md5(time());
$message = "";
#clean up both email addresses
#extract from email address
if ( !ereg("<", $from) ) { $from_email = $from; }
else { $from_email = str_replace(">", "",substr($from,strpos($from, "<")+1)); }
#clean up email addresses
if ( ereg("<", $from) ) { $from = str_replace($from_mail, str_replace(" ", "", $from_mail), $from); }
else { $from = str_replace(" ", "", $from); }
$to = str_replace(" ", "", $to);
#check if both email addresses are valid
if ( !$this->validate_email($from_email) || !$this->validate_email($to) ) { return false; }
#check if server is running on windows, macintosh or linux and define end of lines
$eol = $this->get_eol();
#compile headers
$headers = "From: " . $from . $eol;
$headers .= "Reply-To: " . $from . $eol;
$headers .= "Return-Path: " . $from . $eol;
$headers .= "Message-ID: <" . time() . " server@" . $_SERVER['SERVER_NAME'] . ">" . $eol;
$headers .= "X-Mailer: PHP v" . phpversion() . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
if ( $attachment_content ) { $headers .= "Content-Type: multipart/related; boundary=\"" . $mime_boundary . "\"" . $eol; }
elseif ( !$html ) {
$headers .= "Content-Type: text/plain; charset=utf-8" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
}
else {
$headers .= "Content-Type: text/html; charset=utf-8" . $eol;
$headers .= "Content-Transfer-Encoding: 8bit" . $eol;
}
#compile attachment
if ( $attachment_content ) {
$message .= "--" . $mime_boundary . $eol;
$message .= "Content-Type: ".$attachment_mimetype . "; name=\"" . $attachment_filename . "\"" . $eol;
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"" . $attachment_filename . "\"" . $eol.$eol;
$message .= $attachment_content . $eol . $eol;
$message .= "--".$mime_boundary.$eol;
if ( !$html ) {
$message .= "Content-Type: text/plain; charset=iso-8859-1" . $eol;
$message .= "Content-Transfer-Encoding: 8bit" . $eol;
}
else {
$message .= "Content-Type: text/html; charset=iso-8859-1" . $eol;
$message .= "Content-Transfer-Encoding: 8bit" . $eol;
}
}
#add content
$message .= $text . $eol;
#close multipart if attachment
if ( $attachment_content ) { $message .= "--" . $mime_boundary . "--" . $eol . $eol; }
#check if sendmail can be used
if ( $this->settings['email_use_sendmail'] == "1" ) {
ini_set(sendmail_from,$from);
mail($to, $subject, $message, $headers);
ini_restore(sendmail_from);
}
#else use SMTP
elseif ( $this->settings['email_use_smtp'] == "1" ) {
#connect
$handle = $this->smtp_connect($this->settings['mail_server'], $this->settings['mail_port'], 30, 1, 1, 1);
#setup mail
$this->smtp_command($handle, "HELO web" . $eol, 1, 1);
$this->smtp_command($handle, "MAIL FROM: ". $from . $eol, 1, 1);
$this->smtp_command($handle, "RCPT TO: ". $to . $eol, 1, 1);
$this->smtp_command($handle, "DATA". $eol, 1, 1);
$this->smtp_command($handle, $message . $eol . "." . $eol, 1, 1);
$this->smtp_command($handle, "QUIT". $eol, 1, 1);
#close
$this->smtp_close($handle);
#return
return true;
}
#return false
else { return false; }
}
#Connect to SMTP-Server--------------------------------------------------------------------------
function smtp_connect($host, $port, $timeout=30) {
#connect
$handle = fsockopen($host, $port, $errno, $errstr, $timeout);
#wait for response from server
if(!$handle) { return false; }
$response = fgets($handle,1);
$bytes_left = socket_get_status($handle);
if ($bytes_left > 0) { $response .= fread($handle, "10000"); }
#return
return $handle;
}
#Send command to SMTP-Server using SOCKS-------------------------------------------------------
function smtp_command($handle, $command) {
#execute command
fputs($handle, $command);
#wait for response
$response = fgets($handle,1);
$bytes_left = socket_get_status($handle);
if ($bytes_left > 0) { $response .= fread($handle, "10000"); }
echo $command . " : " . $response;
}
#Close connection with SMTP-Server-------------------------------------------------------------
function smtp_close($handle) { fclose($handle); }
#Check if email is valid-----------------------------------------------------------------------
function validate_email($email) {
if ( eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) ) { return true; }
else { return false; }
}
#set end-of-line depending on server OS--------------------------------------------------------
function get_eol() {
#windows
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) { return "\r\n"; }
#mac
elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) { return "\r";}
#linux
else { return "\n";}
}
} |