wp_mail attachment not working, when the form is filled with an attachment the email is sent but with no attachment received. in wp_mail documentation they said t should add something like that:
$attachments = array(
'/path/to/file1.jpg',
'/path/to/file2.png'
);
but there are random people who will attach random files with random name like gorilla05.png gamelf5.jpg. so the file name will change and i don't know which path to use in this case.
code.php in use:
<?php
if(isset($_POST['submit']) && strpos($_SERVER['HTTP_REFERER'], 'https://example.com/custom-file/') !== false) {
$to = '[email protected]'; // Replace with your own email address
$subject = $_POST['subject']; // Get the value of the subject field from the form
$message = "Name: " . $_POST['name'] . "<br><br>";
$message .= "Email: " . $_POST['email'] . "<br><br>";
$message .= "Message: " . $_POST['message'] . "<br><br>";
$headers = array('Content-Type: text/html; charset=UTF-8');
if (wp_mail($to, $subject, $message, $headers, $attachments)) {
wp_redirect('/thank-you-contact-form-sent'); // Replace with the URL of your thank-you page
exit();
} else {
wp_redirect('/sorry-contact-form-not-sent'); // Replace with the URL of your sorry page
exit();
}
}
?>
form.html:
<form action="code.php" id="code" enctype="multipart/form-data" method="post">
<div class="form-group"><label for="name">Name:</label>
<input id="name" class="form-control" name="name" type="text" /></div>
<div class="form-group"><label for="email">Email:</label>
<input id="email" class="form-control" name="email" type="email" /></div>
<div class="form-group"><label for="subject">Subject:</label>
<input id="subject" class="form-control" name="subject" type="text" /></div>
<div class="form-group"><label for="message">Message:</label>
<textarea id="message" class="form-control" name="message" rows="5"></textarea></div>
<div class="form-group"><label for="image">Image (PNG or JPG, max 2MB):</label>
<input id="image" class="form-control" accept=".png,.jpg" name="image" required="" type="file" /></div>
<div class="form-group"><button class="btn btn-primary" style="width: 100%;" name="submit" type="submit">Send Message</button></div>
</form>
EDIT
This is the code that i'm using now. There's a small problem with it, when i submit the form, it sends email to my admin email which is what i want but it also send an email to the email in the form. i want that is sent just to my admin email + it would be great if there are some securities that i can add to protect from spammers. thanks so much.
<?php
if(isset($_POST['submit']) && strpos($_SERVER['HTTP_REFERER'], 'https://example.com/custom/') !== false) {
$to = '[email protected]';
$subject = $_POST['subject'];
$message = "Name: " . $_POST['name'] . "<br><br>";
$message .= "Email: " . $_POST['email'] . "<br><br>";
$message .= "Message: " . $_POST['message'] . "<br><br>";
$headers = array('Content-Type: text/html; charset=UTF-8');
// Allow just png and jpg maximum file size 2mb
if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
$maxFileSize = 2000000;
$fileSize = $_FILES['image']['size'];
if ($fileSize > $maxFileSize) {
die('File too large! Maximum file size is 2mb.');
}
$allowedExtensions = array('png', 'jpg');
$fileExtension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
if (!in_array($fileExtension, $allowedExtensions)) {
die('Invalid file extension. Just png and jpg extensions are allowed! ');
}
}
$attachments = array();
// Handle the uploaded file
if ($_FILES['image']['error'] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES['image']['tmp_name'];
$filename = basename($_FILES['image']['name']);
$destination = sys_get_temp_dir() . '/' . $filename; // Use system temp directory
// Move the file to the temporary directory
if (move_uploaded_file($tmp_name, $destination)) {
$attachments = array($destination);
}
}
// Send email with attachments, and redirect to thank you page
if (wp_mail($to, $subject, $message, $headers, $attachments)) {
foreach($attachments as $file) {
unlink($file); // Remove file from temp directory after sending
}
// Redirect thank you page
wp_redirect('/thank-you-contact-form-sent'); // Replace with the URL of your thank-you page
exit();
} else {
foreach($attachments as $file) {
unlink($file); // Remove file from temp directoryI apologize, it seems that my previous response was cut off. Here's the rest of the modified code:
}
wp_redirect('/sorry-contact-form-not-sent'); // Replace with the URL of your sorry page
exit();
}
}
?>