大家早上好
我正在为我不久前建立的一个登陆页面添加一个简单的简历文件上传表单功能。我希望当表单提交后,表单的内容,包括上传的文件(任何格式)会以电子邮件的形式发送到一个选定的电子邮件地址。目前,我已经下载了composer,并使用它来安装PHPMailer。我在项目的根目录下有composer.lock、composer.json(其中有phpmailer的代码块)和composer.phar文件。
到目前为止,当我提交上传表格时,我得到的是一个空白页。我甚至没有看到回音 "错误",我要求它显示我是否有问题发送邮件。应该发生的是填写表格,上传文件并提交。然后应该出现一个感谢页面,感谢用户上传他们的简历,当然邮件会出现在收件箱中,里面有所有的信息和附件。
以下是上传表格的代码--------------------------------------。
<section id="upload">
<div>
<h2>Upload your CV:</h2>
</div>
<div id="uploadText">
<form action="toemail.php" method="POST" id="uploadform" enctype="multipart/form-data">
<label for="name" id="labelName">Name:</label> <br>
<input type="text" name="fullName" id="name" required>
<select id="typeofjob" name="jobpreference" required>
<option value="choose" selected disabled>--Choose Area of Expertise--</option>
<option value="Front of house">Front of House</option>
<option value="Back of house">Back of House</option>
</select>
<input type="file" id="filetoupload" name="file" required>
<button type="submit" name="upload_button" id="uploadbtn">Upload CV</button>
</form>
</div>
</section>
这里是我的php代码. 这是我第一次使用phpmailer,所以可能是我忽略了什么。先谢谢你的帮助。
<?php
// using Composer to dounwload PHP Mailer and PHP Mailer to attach the uploaded file to a sent email
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(isset($_POST['upload_button'])){
require 'phpmailer/phpmailerAutoload.php';
require 'vendor/autoload.php';
$mail = new PHPMailer();
$name = $_POST['fullName'];
$preference = $_POST['jobpreference'];
$filename = $_FILES['file']['name'];
$file = $_FILES['file']['tmp_name'];
// $destination = 'uploads/'. $filename;
// $extension = pathinfo($filename,PATHINFO_EXTENSION);
// $size = $_FILES['file']['size'];
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Your Domain Name
$mail->SMTPAuth = true;
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->Username = "[email protected]"; // Your Email ID
$mail->Password = "___"; // Password of your email id
$mail->setFrom = "[email protected]";
$mail->FromName = "___";
$mail->AddAddress ("___"); // On which email id you want to get the message
$mail->addAttachment($file,$filename); //This line Use to Keep User Txt,Doc,pdf file ,attachment
$mail->IsHTML(true);
$mail->Subject = "Uploaded CV by $name"; // This is your subject
// HTML Message Starts here
$mail->Body = "
<html>
<body>
<table style='width:600px;'>
<tbody>
<tr>
<td style='width:150px'><strong>Name: </strong></td>
<td style='width:400px'>$name</td>
</tr>
<tr>
<td style='width:150px'><strong>Email ID: </strong></td>
<td style='width:400px'>$preference</td>
</tr>
</tbody>
</table>
</body>
</html>
";
// HTML Message Ends here
if(!$mail->send())
{
echo "Error";
}
else
{
header('location:thanksforcv.html');
}
}
?>
感谢任何帮助。
空白页面通常意味着一个致命的PHP错误,所以你应该检查你的Web服务器的错误日志以了解更多细节。很可能是这一行有问题。
require 'phpmailer/phpmailerAutoload.php';
这个文件在过去的3年里没有出现在PHPMailer中,所以这将导致一个致命的错误;这可能是复制&粘贴Stack Overflow上的旧代码的结果。如果你对代码是否正确有任何疑问。始终 直接从 本源.