Repository navigation
Expand file tree
/
Copy pathsend_email.php
More file actions
56 lines (48 loc) · 1.75 KB
/
Copy pathsend_email.php
File metadata and controls
56 lines (48 loc) · 1.75 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
<?php
require 'vendor/autoload.php';
if ($_SERVER["REQUEST_METHOD"] === "POST") {
// Get form inputs
$smtpHost = $_POST["smtp_host"];
$smtpPort = $_POST["smtp_port"];
$smtpUsername = $_POST["smtp_username"];
$smtpPassword = $_POST["smtp_password"];
$emailTo = $_POST["email_to"];
$emailFrom = $_POST["email_from"];
$subject = "Test SMTP Connection";
$message = "This is a test email to check the SMTP connection.";
// Attempt to send the test email
if (sendTestEmail($smtpHost, $smtpPort, $smtpUsername, $smtpPassword, $emailTo, $emailFrom, $subject, $message)) {
$resultMessage = "Test email sent successfully!";
} else {
$resultMessage = "Failed to send the test email. Check your SMTP settings.";
}
}
function sendTestEmail($host, $port, $username, $password, $to, $from, $subject, $message)
{
// Set the SMTP configuration
$config = [
'host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password,
'encryption' => 'tls', // You can use 'ssl' if your server requires it
];
// Create the SMTP transport
$transport = new \Swift_SmtpTransport($config['host'], $config['port'], $config['encryption']);
$transport->setUsername($config['username']);
$transport->setPassword($config['password']);
// Create the Mailer using the created transport
$mailer = new \Swift_Mailer($transport);
// Create the message
$message = (new \Swift_Message($subject))
->setFrom([$from])
->setTo([$to])
->setBody($message);
// Attempt to send the message
try {
return $mailer->send($message);
} catch (\Swift_TransportException $e) {
return false;
}
}