我的PHP Mailer代码给出了HTTP 500错误

时间:2019-07-05 12:55:13

标签: php phpmailer contact-form

我为一个使用普通文本框,下拉选项框,日期日历,复选框和附件的网站编写了一份详细的申请表。我只使用普通的PHP就可以通过精美的电子邮件发送表格,但是在处理附件部分时却卡住了。然后,我学会了使用PHP Mailer的最佳方法,因此我为此完成了PHP,但是却收到了可怕的HTTP 500错误。请有人检查我的代码,看看我要去哪里错了。预先非常感谢。

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'href="/PHPMailer/src/Exception.php"';
require 'href="/PHPMailer/src/PHPMailer.php"';

$message = '';

function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}

if(isset($_POST["submit"]))
{
$path = 'href="/upload/"' . $_FILES["attachment"]["name"];
move_uploaded_file($_FILES["attachment"]["tmp_name"], $path);
$message = '
<h3 align="center">Application Details</h3>
<table border="1" width="100%" cellpadding="5" cellspacing="5">
<tr>
<td width="30%">First Name</td>
<td width="70%">'.$_POST["first_name"].'</td>
</tr>
<tr>
<td width="30%">Last Name</td>
<td width="70%">'.$_POST["last_name"].'</td>
</tr>
<tr>
<td width="30%">Email Address</td>
<td width="70%">'.$_POST["email"].'</td>
</tr>
<tr>
<td width="30%">Phone Number</td>
<td width="70%">'.$_POST["phone_number"].'</td>
</tr>
<tr>
<td width="30%">Position</td>
<td width="70%">'.$_POST["position"].'</td>
</tr>
<tr>
<td width="30%">WhatsApp Number</td>
<td width="70%">'.$_POST["whatsapp_no"].'</td>
</tr>
<tr>
<td width="30%">Yacht Name</td>
<td width="70%">'.$_POST["yacht_name"].'</td>
<tr>
<td width="30%">Yacht Type</td>
<td width="70%">'.$_POST["yacht_type"].'</td>
</tr>
<tr>
<td width="30%">Yacht Size</td>
<td width="70%">'.$_POST["yacht_size"].'</td>
</tr>
<tr>
<td width="30%">Cruising Type</td>
<td width="70%">'.$_POST["cruising_type"].'</td>
</tr>
<tr>
<td width="30%">Yacht Location</td>
<td width="70%">'.$_POST["yacht_location"].'</td>
</tr>
<tr>
<td width="30%">Yacht Itinenary</td>
<td width="70%">'.$_POST["yacht_itinenary"].'</td>
</tr>
<tr>
<td width="30%">Work Posistion</td>
<td width="70%">'.$_POST["work_position"].'</td>
</tr>
<tr>
<td width="30%">Work Type</td>
<td width="70%">'.$_POST["work_type"].'</td>
</tr>
<tr>
<td width="30%">Start Date</td>
<td width="70%">'.$_POST["start_date"].'</td>
</tr>
<tr>
<td width="30%">Finish Date</td>
<td width="70%">'.$_POST["finishing_date"].'</td>
<tr>
<td width="30%">Visa Required</td>
<td width="70%">'.$_POST["visa_req"].'</td>
</tr>
<tr>
<td width="30%">Qualifications Required</td>
<td width="70%">'.$_POST["qual_req"].'</td>
</tr>
<tr>
<td width="30%">Job Description</td>
<td width="70%">'.$_POST["job_desc"].'</td>
</tr>
<tr>
<td width="30%">Shared Cabin</td>
<td width="70%">'.$_POST["shared_cabin"].'</td>
</tr>
<tr>
<td width="30%">Understood Terms</td>
<td width="70%">'.$_POST["terms"].'</td>
</tr>
<tr>
<td width="30%">Confidental</td>
<td width="70%">'.$_POST["confidential"].'</td>
</tr>
</table>
';
if (isset($_POST['shared_cabin'])){
$_POST['shared_cabin']; // Displays value of checked checkbox.
} 
if (isset($_POST['terms'])){
$_POST['terms']; // Displays value of checked checkbox.
} 
if (isset($_POST['confidential'])){
$_POST['confidential']; // Displays value of checked checkbox.
}   

require 'class/class.phpmailer.php';
$mail = new PHPMailer(TRUE);
$mail->From = $_POST["email_from"];     //Sets the From email address for 
the message
$mail->FromName = $_POST["first_name"]["last_name"];    //Sets the From 
name of the message
$mail->AddAddress('timmy2872@gmail.com');  //Adds a "To" address
$mail->WordWrap = 50;       //Sets word wrapping on the body of the 
message to a given number of characters
$mail->IsHTML(true);       //Sets message type to HTML
$mail->AddAttachment($path);     //Adds an attachment from a path on the 
filesystem
$mail->Subject = 'Employer Application Form details';    //Sets the 
Subject of the message
$mail->Body = $message;       //An HTML or plain text message body
if($mail->Send())        //Send an Email. Return true on success or false 
on error
{
$message = '<div class="alert alert-success">Application Successfully 
Submitted</div>';
unlink($path);
}
else
{
$message = '<div class="alert alert-danger">There is an Error</div>';
}
}

?>

1 个答案:

答案 0 :(得分:0)

现在,所有问题都已修复并像梦一样工作。 问题是,我正在处理的站点位于“临时”文件夹中,以免破坏旧站点。我在子目录中而不是服务器的主目录中有PHPMailer和Upload文件夹,因此这是需要的方式-

keys(s)

也不需要此行-

require "/home/awolpm/PHPMailer/src/Exception.php";
require "/home/awolpm/PHPMailer/src/PHPMailer.php";

上传文件夹必须为-

 require 'class/class.phpmailer.php';

肯定让我挠了一下头!

要做的最后一件事-在成功发送后弹出一个模式框,并将确认电子邮件返回给发件人。