I am trying to create a program to send emails with PHPMailer and I am having a problem.
I am implementing a simple CRUD with HTML that sends it's content via POST Method to the PHP file that execute the PHPMailer. But the problem is that I want to send a file (for example a Microsoft Word file) via the CRUD and it's not working.
Here is my code...
THE HTML CRUD 👇
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Send email</title>
</head>
<body>
<div>
<h1>SEND EMAIL</h1>
<br><br>
<form action="email.php" method="POST" enctype="multipart/form-data">
<div>
<label>Subject</label>
<input type="text" name="subject">
</div>
<div>
<label>Message</label>
<input type="text" name="message">
</div>
<div>
<label>Attach file</label>
<input type="file" name="file">
</div>
<div>
<input type="submit" value="Send">
</div>
</form>
</div>
</body>
</html>
THE PHPMailer implementation 👇
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/Exception.php';
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = 0;
$mail->CharSet = 'UTF-8';
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myemail@gmail.com';
$mail->Password = 'mypassword';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('myemail@gmail.com');
$mail->addAddress('mailreceiver@gmail.com');
$mail->isHTML(true);
$mail->Subject = $_POST['subject'];
$mail->Body = $_POST['message'];
$mail->addAttachment($_FILES['file']); 👈 THIS IS WHAT DONT WORK 🔴
$mail->send();
echo("Email has been send");
} catch (Exception $e) {
echo("Error couldn't send the mail");
}
?>
Everything works perfectly without the implementation of the $mail->addAttachment($_FILES['file']);
But when I try to implement the part to attach a file it seems to be inaccessible for the PHPMailer
So, ¿How can I fix this? I want to attach a file to the PHPMailer with the HTML CRUD
source https://stackoverflow.com/questions/72673578/phpmailer-attachments-issue
No comments:
Post a Comment