Script to send email not working

Joined
Apr 10, 2023
Messages
1
Reaction score
0
<?php
echo "The PHP script is executing.";
// ...rest of the code here
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if a file was uploaded
if (!empty($_FILES["emailList"]["tmp_name"])) {
// Use email addresses from file
$to = file_get_contents($_FILES["emailList"]["tmp_name"]);
} else {
// Use email address from input field
$to = $_POST["to"];
}

$subject = $_POST["subject"];
$body = $_POST["body"];

$headers = "From: " . $_POST["username"] . "\r\n";
$headers .= "Reply-To: " . $_POST["username"] . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8\r\n";

if (mail($to, $subject, $body, $headers)) {
echo "Email was sent successfully.";
} else {
echo "Email failed to send.";
}
var_dump(mail($to, $subject, $body, $headers));
}


?>
<!DOCTYPE html>
<html>
<head>
<title>Stats</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="username">Email:</label>
</td>
<td>
<input type="email" id="username" name="username" required />
</td>
</tr>
<tr>
<td>
<label for="password">Password:</label>
</td>
<td>
<input type="password" id="password" name="password" required />
</td>
</tr>
<tr>
<td>
<label for="smtpHost">SMTP host:</label>
</td>
<td>
<input type="text" id="smtpHost" name="smtpHost" required />
</td>
</tr>
<tr>
<td>
<label for="smtpPort">SMTP port:</label>
</td>
<td>
<input type="text" id="smtpPort" name="smtpPort" required />
</td>
</tr>
<tr>
<td>
<label for="to">Recipient:</label>
</td>
<td>
<textarea id="to" name="to" required></textarea>
</td>
</tr>
<tr>
<td>
<label for="emailList">Upload email addresses from file:</label>
</td>
<td>
<input type="file" id="emailList" name="emailList" accept=".txt" />
</td>
</tr>
<tr>
<td>
<label for="subject">Subject:</label>
</td>
<td>
<input type="text" id="subject" name="subject" required />
</td>
</tr>
<tr>
<td>
<label for="body">Message body:</label>
</td>
<td>
<textarea id="body" name="body" required></textarea>
</td>
</tr>
</table>
<button type="submit">Send</button>
</form>
</body>

<script>
// Move the email list input field to the right of the email address input field
const emailInput = document.getElementById("to");
const emailListInput = document.getElementById("emailList");
emailInput.parentNode.insertBefore(emailListInput, emailInput.nextSibling);

// Set the value of the email address input field to the contents of the selected file
emailListInput.addEventListener("change", function() {
const file = emailListInput.files[0];
const reader = new FileReader();
reader.addEventListener("load", function() {
// Split the file contents by newline characters to get an array of email addresses
const emailAddresses = reader.result.split("\n");
// Remove any empty email addresses from the array
const nonEmptyEmailAddresses = emailAddresses.filter((emailAddress) => emailAddress.trim() !== "");
// Join the non-empty email addresses with comma separators
const emailList = nonEmptyEmailAddresses.join(",");
emailInput.value = emailList;
});
reader.readAsText(file)
});
</script>
 
Joined
Mar 31, 2023
Messages
95
Reaction score
8
Here is the fix code
PHP:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (!empty($_FILES["emailList"]["tmp_name"])) {
        $to = file_get_contents($_FILES["emailList"]["tmp_name"]);
    } else {
        $to = $_POST["to"];
    }

    $subject = $_POST["subject"];
    $body = $_POST["body"];

    $headers = "From: " . $_POST["username"] . "\r\n";
    $headers .= "Reply-To: " . $_POST["username"] . "\r\n";
    $headers .= "Content-type: text/plain; charset=UTF-8\r\n";

    if (mail($to, $subject, $body, $headers)) {
        echo "Email was sent successfully.";
    } else {
        echo "Email failed to send.";
    }
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Stats</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
<table>
<tr>
<td>
<label for="username">Email:</label>
</td>
<td>
<input type="email" id="username" name="username" required />
</td>
</tr>
<tr>
<td>
<label for="password">Password:</label>
</td>
<td>
<input type="password" id="password" name="password" required />
</td>
</tr>
<tr>
<td>
<label for="smtpHost">SMTP host:</label>
</td>
<td>
<input type="text" id="smtpHost" name="smtpHost" required />
</td>
</tr>
<tr>
<td>
<label for="smtpPort">SMTP port:</label>
</td>
<td>
<input type="text" id="smtpPort" name="smtpPort" required />
</td>
</tr>
<tr>
<td>
<label for="to">Recipient:</label>
</td>
<td>
<textarea id="to" name="to" required></textarea>
</td>
</tr>
<tr>
<td>
<label for="emailList">Upload email addresses from file:</label>
</td>
<td>
<input type="file" id="emailList" name="emailList" accept=".txt" />
</td>
</tr>
<tr>
<td>
<label for="subject">Subject:</label>
</td>
<td>
<input type="text" id="subject" name="subject" required />
</td>
</tr>
<tr>
<td>
<label for="body">Message body:</label>
</td>
<td>
<textarea id="body" name="body" required></textarea>
</td>
</tr>
</table>
<button type="submit">Send</button>
</form>
</body>

<script>
const emailInput = document.getElementById("to");
const emailListInput = document.getElementById("emailList");
emailInput.parentNode.insertBefore(emailListInput, emailInput.nextSibling);

emailListInput.addEventListener("change", function() {
    const file = emailListInput.files[0];
    const reader = new FileReader();
    reader.addEventListener("load", function() {
        const emailAddresses = reader.result.split("\n");
        const nonEmptyEmailAddresses = emailAddresses.filter((emailAddress) => emailAddress.trim() !== "");
        const emailList = nonEmptyEmailAddresses.join(",");
        emailInput.value = emailList;
    });
    reader.readAsText(file)
});
</script>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top