【发布时间】:2019-07-05 12:55:13
【问题描述】:
我为一个使用普通文本框、下拉选项框、日期日历、复选框和附件的网站编制了一份详细的申请表。我只使用普通的 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>';
}
}
?>
【问题讨论】:
-
A 500 错误是一种通用错误消息,几乎涵盖了脚本可能出错的每一件事。检查您的服务器错误日志以找出确切的错误消息。
-
据我所知,您也必须配置 phpmailer,因此请设置主机、用户名、密码等。但我可能错了,只是在没有任何配置的情况下从未见过。
标签: php phpmailer contact-form