-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail-function.php
More file actions
140 lines (120 loc) · 4.83 KB
/
email-function.php
File metadata and controls
140 lines (120 loc) · 4.83 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
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
<?php
include('config.php');
include('manualdbconfig.php');
function authgMail($from, $namefrom, $to, $nameto, $subject, $message) {
$sqlEmail="
Select
a.name,
a.value,
a.id
From
mdl_cifaconfig a
Where
(a.name = 'smtphosts'Or
a.name = 'smtpuser'Or
a.name = 'smtppass')";
$queryEmail=mysql_query($sqlEmail);
$rowEmail=mysql_fetch_array($queryEmail);
if($rowEmail['name']=='smtphosts'){ $smtpEmail=$rowEmail['value']; }
if($rowEmail['name']=='smtpuser'){ $smtpUser=$rowEmail['value']; }
if($rowEmail['name']=='smtppass'){ $smtppass=$rowEmail['value']; }
$smtpServer = $smtpEmail; //"pop.mmsc.com.my"; //ip address of the mail server. This can also be the local domain name
$port = "25"; // should be 25 by default, but needs to be whichever port the mail server will be using for smtp
$timeout = "45"; // typical timeout. try 45 for slow servers
$username = $smtpUser;//"mohd.arizan/mmsc.com.my"; // the login for your smtp
$password = $smtppass;//"mohdarizan123"; // the password for your smtp
$localhost = "127.0.0.1"; // Defined for the web server. Since this is where we are gathering the details for the email
$newLine = "\r\n"; // aka, carrage return line feed. var just for newlines in MS
$secure = 0; // change to 1 if your server is running under SSL
//connect to the host and port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 4096);
if(empty($smtpConnect)) {
$output = "Failed to connect: $smtpResponse";
//echo $output;
echo "Not send";
return $output;
}
else {
$logArray['connection'] = "<p>Connected to: $smtpResponse";
//echo "<p />connection accepted<br>".$smtpResponse."<p />Continuing<p />";
//echo "<br/><p>Thank your for subscribed our module</p>";
}
//you have to say HELO again after TLS is started
fputs($smtpConnect, "HELO $localhost". $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['heloresponse2'] = "$smtpResponse";
//request for auth login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authrequest'] = "$smtpResponse";
//send the username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authusername'] = "$smtpResponse";
//send the password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['authpassword'] = "$smtpResponse";
//email from
fputs($smtpConnect, "MAIL FROM: <$from>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailfromresponse'] = "$smtpResponse";
//email to
fputs($smtpConnect, "RCPT TO: <$to>" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['mailtoresponse'] = "$smtpResponse";
//the email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data1response'] = "$smtpResponse";
//construct headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto <$to>" . $newLine;
$headers .= "From: $namefrom <$from>" . $newLine;
//observe the . after the newline, it signals the end of message
fputs($smtpConnect, "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n");
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['data2response'] = "$smtpResponse";
// say goodbye
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 4096);
$logArray['quitresponse'] = "$smtpResponse";
$logArray['quitcode'] = substr($smtpResponse,0,3);
fclose($smtpConnect);
//a return value of 221 in $retVal["quitcode"] is a success
return($logArray);
}
//details about email content
//***************************///
//***************************///
$queryrole = $DB->get_records('role_assignments',array('contextid'=>'1', 'roleid'=>'10'));
foreach($queryrole as $qrole){
$sqlusers=mysql_query("SELECT * FROM mdl_cifauser WHERE id='".$qrole->userid."'");
$susers=mysql_fetch_array($sqlusers);
$fullname=$susers['firstname'].''.$susers['lastname'];
$email=$susers['email'];
$sqlAdmin="
Select
a.lastname,
a.id,
a.username,
a.email,
a.firstname
From
mdl_cifauser a
Where
a.id = '2'";
$queryAdmin=mysql_query($sqlAdmin);
$rowAdmin=mysql_fetch_array($queryAdmin);
//include('email_notification.php');
$from=$rowAdmin['email'];//"mohd.arizan@mmsc.com.my"; //administrator mail
$namefrom="CIFA Administrator";
$to = $email;
$nameto = $fullname;
$subject = "[download exam software]";
$message = "Hi <b>$fullname</b>, you need to download exam software. Please login CIFA ONLINE.";
// this is it, lets send that email!
authgMail($from, $namefrom, $to, $nameto, $subject, $message);
?>