php - File attachment from form with phpMailer
one text
My form
<form class="c-form" enctype="multipart/form-data" action="http://www.test15.xyz/mail.php" method="post">
<div class="form-cont">
....
<div class="call-field call-field_file">
<div class="file-holder"><span class="f-quant"></span></div>
<input type="file" name="file" class="call-input">
</div>
....
<div class="form-add">
<div class="alert-text ital red">Not all required fields are filled</div>
<button type="submit" class="button submit">Submit</button>
</div>
</div>
</form>
Form.php code
require 'lib/src/PHPMailer.php';
require 'lib/src/SMTP.php';
require 'lib/src/Exception.php';
if (!empty($_POST)){
$email = htmlspecialchars(trim($_POST['email']));
$name = htmlspecialchars(trim($_POST['name']));
isset($_POST['surname']) ? $surname = htmlspecialchars(trim($_POST['surname'])) : $surname = 'Surname';
isset($_POST['phone']) ? $phone = htmlspecialchars(trim($_POST['phone'])) : $phone = 'tel';
$sub = htmlspecialchars(trim($_POST["subject"]));
$message = htmlspecialchars(trim($_POST['message']));
}
if ( !empty( $_FILES['file']['tmp_name'] ) && $_FILES['file']['error'] == 0 ) {
$filepath = $_FILES['file']['tmp_name'];
$filename = $_FILES['file']['name'];
} else {
$filepath = '';
$filename = '';
}
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
$mail->setFrom($email, $name);
$mail->addAddress('condom@test15.xyz');
$mail->addAddress('blagorad@list.ru');
$mail->Subject = $sub;
$mail->msgHTML("<html><body>
<p><php> $message <?> </p>
</html></body>");
$mail->addAttachment($filepath, $filename);
$mail->send();
The email sends the body correctly, but without the Attachment. Php.ini file_uploads = On. CMS Wordpress. Are there any server settings I need? I don`t use SMTP, auth, passwords etc. I want just my attached file to be uploaded to mail. File without form like that $mail->addAttachment('readme.html', 'readme file'); sends good.
Source