【发布时间】:2021-04-16 00:55:39
【问题描述】:
我需要在联系表单中添加一个部分,供人们上传他们的信息(PDF 或图像 (JPG/PNG/ETC))。我得到了上传文件的部分,但它没有通过电子邮件发送 PDF(电子邮件工作正常,但它在没有上传文件的情况下到达,我不知道为什么)。我会把我的 HTML/PHP 代码放在上面,所以如果你能帮助我知道问题出在哪里或者我如何实现上传文件,那就太好了!
HTML
<form class="mb-2" action="freetest.php" onsubmit="return validateForm();" method="post">
<div class="form-group">
<div>
<label class="mb-0">Name</label>
</div>
<div class="form-group">
<input class="form-control-input" type="text" id="firstName" name="firstName" placeholder="Your Name" required>
</div>
</div>
<div class="form-group">
<div>
<label class="mb-0">Last Name</label>
</div>
<div class="form-group">
<input class="form-control-input" type="text" id="lastName" name="lastName" placeholder="Amelie" required>
</div>
</div>
<div class="form-group">
<div>
<label class="mb-0">Mail</label>
</div>
<div>
<input class="form-control-input" type="text" name="email" id="yourEmail" placeholder="Example@gmail.com" required>
</div>
</div>
<div class=form-group>
<label class="mb-0">¿Any Question?</label>
<textarea class="form-control-textarea" rows="8" name="message" required></textarea>
</div>
<div class="form-group">
<div>
<label for="file-upload" class="custom-file-upload mt-2"><i class="fas fa-cloud-upload-alt"></i> Upload File</label>
<input id="file-upload" type="file" name="file" multiple="multiple" required>
</div>
</div>
<button type="submit" class="btn-solid-lg">Send</button>
</form>
PHP
<?php
$ToEmail = 'mymail@gmail.com';
$EmailSubject = 'Free test';
$mailheader = "From: ".$_POST["lastName"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "First Name: ".$_POST["firstName"]."<br/>";
$MESSAGE_BODY .= "Last Name: ".$_POST["lastName"]."<br/>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br/>";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["message"])."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
【问题讨论】:
-
如果您想使用
mail()功能,请查看回复 -
我建议使用 PHP Mailer
-
我在任何地方都看不到
<input type='file' />。无论如何,您可能都希望动态创建该元素,然后单击它而不看到它。一旦你有一个,你将需要FormDateInstance.append(yourFile)。伪代码:const fd = new FormData, upload = document.querySelector('input[type=file]'); upload.onchange = function(){ if(this.files){ const file = this.files[0], name = file.name, dot = name.lastIndexOf('.'); if(dot !== -1 && name.slice(dot).toLowerCase() === '.pdf'){ fd.append('fileGlobal', this.files[0], 'uploadName.pdf'); } } }。以$_FILES['fileGlobal']访问。 -
关于我的最后一条评论,顺便说一下,您通过
XMLHttpRequest发送FormData。
标签: javascript java php html email