我试图在我的PHPmailer中添加PDF附件,但它不会发送附件。
控制器:
$recipient = Request::input('emailaddress');
$recipientName = Request::input('name');
$file = Request::input('file');
$mail = new \PHPMailer(true);
try {
$mail->isSMTP();
$mail->CharSet = "utf-8";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.sendgrid.net";
$mail->Port = 587;
$mail->Username = "username";
$mail->Password = "password";
$mail->setFrom("info@email.nl", "Business");
$mail->Subject = "Offerte";
$mail->MsgHTML("Test.");
$mail->addAddress($recipient, $recipientName);
if (isset($_FILES[$file]) &&
$_FILES[$file]['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES[$file]['tmp_name'],
$_FILES[$file]['name']);
}
$mail->send();
} catch (phpmailerException $e) {
dd($e);
} catch (Exception $e) {
dd($e);
}
return back()->with('send', 'send');
}
表格:
<form method="POST" action="/offer/sendmail">
<div class="form-group">
<label for="name">Aan:</label>
<input type="text" class="form-control" id="name" name="name" value="{{$offers->name}}">
</div>
<div class="form-group">
<label for="emailaddress">Email-adres:</label>
<input type="text" class="form-control" id="emailaddress" name="emailaddress" value="{{$offers->email}}">
</div>
<div class="form-group">
<label for="subject">Onderwerp:</label>
<input type="text" class="form-control" id="subject">
</div>
<div class="form-group">
<label for="subject">Bericht:</label>
<textarea class="form-control" rows="5" id="comment"></textarea>
</div>
<div class="form-group">
<label for="subject">Voeg een bestand toe:</label>
<input type ="file" name='file' id='uploaded_file'>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">Verzenden</button>
</form>
它确实发送了电子邮件,但其中没有附件。你知道为什么吗?我还更改了$ mail-&gt;用户名和密码,这样我就可以将我的凭据保密。
答案 0 :(得分:0)
此代码不安全;你信任来自$_FILES
的价值观。您需要致电is_uploaded_file
或move_uploaded_file
。 Read the PHP docs on handling uploads
在更基础的级别上,您尚未在表单上设置enctype
属性,因此文件元素无效 - 您需要添加enctype="multipart/form-data"
,因此您的form
标记将会成为
<form method="POST" action="/offer/sendmail" enctype="multipart/form-data">
设置MAX_FILE_SIZE
元素也是一个好主意。
将您的代码基于PHPMailer提供的send_file_upload示例,该示例正确且安全地处理上传。