-
Notifications
You must be signed in to change notification settings - Fork 276
/
testmail.php
157 lines (143 loc) · 7.31 KB
/
testmail.php
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
<?php
/**
* This diagnostic page helps you to check email settings.
* You can use this script in order to try to send an email with a debug trace.
* @copyright Copyright (c) 2014-2023 Benjamin BALET
* @license http://opensource.org/licenses/AGPL-3.0 AGPL-3.0
* @link https://github.com/bbalet/jorani
* @since 0.4.0
*/
require_once 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
define('BASEPATH', '.'); //Make this script works with nginx
$env = is_null(getenv('CI_ENV'))?'':getenv('CI_ENV');
//Configuration values are taken from application/config/(env)/email.php
//-----------------------------------------------------------------
//Please enter a valid target email address. A test email will be sent here
define('EMAIL_ADDRESS', '');
//-----------------------------------------------------------------
?>
<html>
<head>
<title>Jorani Email Configuration</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="icon" type="image/x-icon" href="favicon.ico" sizes="32x32">
<link rel="stylesheet" href="assets/dist/requirements.css">
<script type="text/javascript" src="assets/dist/requirements.js"></script>
</head>
<body>
<div class="container">
<ul class="nav nav-pills">
<li class="nav-item"><a class="nav-link" href="home" title="login to Jorani"><i class="mdi mdi-home nolink"></i></a></li>
<li class="nav-item"><a class="nav-link" href="requirements.php">Requirements</a></li>
<li class="nav-item"><a class="nav-link active" href="#">Email</a></li>
<li class="nav-item"><a class="nav-link" href="testldap.php">LDAP</a></li>
<li class="nav-item"><a class="nav-link" href="testssl.php">SSL</a></li>
<li class="nav-item"><a class="nav-link" href="testoauth2.php">OAuth2</a></li>
<li class="nav-item"><a class="nav-link" href="testapi.php">API HTTP</a></li>
</ul>
<h1>Test of your e-mail configuration</h1>
<?php
//Check if we can access to the configuration file
$pathCIConfigFile = realpath(join(DIRECTORY_SEPARATOR, array('application', 'config', $env, 'config.php')));
$pathConfigFile = realpath(join(DIRECTORY_SEPARATOR, array('application', 'config', $env, 'email.php')));
$configCIFileExists = file_exists($pathCIConfigFile);
$configFileExists = file_exists($pathConfigFile);
if (EMAIL_ADDRESS == '') {
echo '<div class="alert alert-danger" role="alert"><b>ERROR:</b> Please provide a valid e-mail address in testmail.php.</div>' . PHP_EOL;
} else {
if ($configFileExists && $configCIFileExists) {
include $pathCIConfigFile;
include $pathConfigFile;
try {
//Include shipped PHPMailer library
$mail = new PHPMailer(true); //true => throw exceptions on error
$mail->SMTPDebug = 1; //Errors and messages
$mail->Debugoutput = function($str, $level) {
echo nl2br($str);
};
//Test if we use SMTP protocol
if (strcasecmp($config['protocol'], 'smtp') == 0) {
echo '<b>INFO:</b> Selecting SMTP Protocol.<br />' . PHP_EOL;
$mail->IsSMTP();
$mail->Host = $config['smtp_host'];
$mail->Port = $config['smtp_port'];
$mail->SMTPAutoTLS = $config['smtp_auto_tls'];
if (strpos($config['smtp_port'], 'gmail') !== false) {
echo '<b>INFO:</b> Using GMAIL.<br />' . PHP_EOL;
}
}
//Test if we use sendmail
if (strcasecmp($config['protocol'], 'sendmail') == 0) {
echo '<b>INFO:</b> Selecting sendmail.<br />' . PHP_EOL;
$mail->IsSendmail();
if (array_key_exists('mailpath', $config)) {
if ($config['mailpath'] != '') {
echo '<b>INFO:</b> Changing sendmail path.<br />' . PHP_EOL;
ini_set('sendmail_path', $config['mailpath']);
}
}
}
//GMAIL requires _smtp_auth set to TRUE
if ($config['smtp_auth'] === TRUE) {
echo '<b>INFO:</b> SMTP with authentication.<br />' . PHP_EOL;
$mail->SMTPAuth = true;
$mail->Username = $config['smtp_user'];
$mail->Password = $config['smtp_pass'];
}
//GMAIL requires smtp_crypto set to tls
if ($config['smtp_crypto'] != '') {
echo '<b>INFO:</b> SMTP with crypto.<br />' . PHP_EOL;
$mail->SMTPSecure = $config['smtp_crypto'];
}
$mail->CharSet = $config['charset'];
$mail->AddAddress(EMAIL_ADDRESS, "Test e-mail");
$mail->SetFrom($config['from_mail'], "Jorani application");
$mail->Subject = "Test Message";
$mail->Body = 'This is a test.';
$mail->Send();
echo '<b>INFO:</b> Message sent.<br />' . PHP_EOL;
} catch (phpmailerException $e) {
echo '<b>ERROR:</b> PHPMailer has encountered an error.<br />' . PHP_EOL;
$text = $e->errorMessage();
$text = iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);
echo $text . PHP_EOL;
} catch (Exception $e) {
echo '<b>ERROR:</b> Unexpected error.<br />' . PHP_EOL;
$text = $e->getMessage();
$text = iconv(mb_detect_encoding($text, mb_detect_order(), true), "UTF-8", $text);
echo $text . PHP_EOL;
}
} else {
echo '<div class="alert alert-danger" role="alert"><b>ERROR:</b> The configuration doesn\'t exist.</div>' . PHP_EOL;
}
}
?>
<h3>Troubleshooting</h2>
<p>In case of error, here are some additional steps:</p>
<ul>
<li>Check the configuration with your IT Admin team.</li>
<li>If you are using GMAIL, please read <a target="_blank" href="https://support.google.com/mail/answer/78775?hl=en">this article</a>.</li>
<li>The STMP port may be blocked by your organization/server's security policy (firewall, etc.).</li>
<li>When running SELinux, the webserver is blocked by default (it cannot send e-mails or open a network connection). Please consider unblocking it:
<p>
<pre>
$ setsebool -P httpd_can_sendmail 1
$ setsebool -P httpd_can_network_connect 1
</pre>
</p>
</li>
<li>Some e-mail servers (eg Office 360) require to set a valid sender e-mail. Update <tt>config/config.php</tt>
<p>
<code>$config['from_mail'] = '[email protected]';</code>
</p>
</li>
<li>Some antivirus block STMP port by default.</li>
<li>Some SMTP server require the application server sending emails (i.e. Jorani) to be whitelisted (on the SMTP server).</li>
<li>Your webhosting company may forbid email functions.</li>
<li>Maybe that the emails are sent but put into SPAM folder.</li>
</ul>
</div>
</body>
</html>