【发布时间】:2023-03-17 16:39:02
【问题描述】:
所以有点快速背景。我才刚刚开始接触 php,而且我已经研究了几天了。抱歉,如果我的代码很乱,但我已尝试尽可能多地标记。
我有一个表格,完成后应该发生 3 件事。
- 电子邮件发送给收件人,所有填写的详细信息都通过格式精美的 html 表格 + 图像附件发送。
- 向表单申请人发送纯文本电子邮件,并表示感谢。
- 此外,在表单提交成功后,表单申请人将 自动定向到“谢谢”页面。
• 如果填写了所有必填字段并且图像已上传/附加,则一切正常。整个过程运行无误。
• 如果图片未上传/附加,我会收到以下消息
警告:file():第 49 行 /home/website/public_html/repairs/contact.php 中的文件名不能为空
警告:无法修改标头信息 - 第 108 行 /home/website/public_html/repairs/contact.php 中的标头(输出开始于 /home/website/public_html/repairs/contact.php:49)已发送
随后收到一封包含所有 html 标签/代码/纯文本的电子邮件。
仍会收到发送给“表格申请人”的带有感谢的纯文本电子邮件。
在下面查看我的 php 代码
<?php
// multiple recipients
$to = $_REQUEST['sendto'] ;
$from = $_REQUEST['email'] ;
// subject
$subject = "Repairs Request Form";
$fname = $_REQUEST['fname'] ;
$message = '
<html>
<body>
<table rules="all" style="border-color: #666; border="1px" "cellpadding="10">
<tr><td style="background: #eee;" colspan="2"><strong>Repairs Request Enquiry</strong></td></tr>
<tr><td><strong>First Name:</strong> </td><td>' . strip_tags($_POST['fname']) . '</td></tr>
<tr><td><strong>Last Name:</strong> </td><td>' . strip_tags($_POST['lname']) . '</td></tr>
<tr><td><strong>Email:</strong> </td><td>' . strip_tags($_POST['email']) . '</td></tr>
<tr><td><strong>Contact Number:</strong> </td><td>' . strip_tags($_POST['phone']) . '</td></tr>
<tr><td><strong>Address:</strong> </td><td>' . strip_tags($_POST['address']) . '</td></tr>
<tr><td><strong>Suburb:</strong> </td><td>' . strip_tags($_POST['suburb']) . '</td></tr>
<tr><td><strong>Postcode:</strong> </td><td>' . strip_tags($_POST['postcode']) . '</td></tr>
<tr><td><strong>Type of repair:</strong> </td><td>' . strip_tags($_POST['repair']) . '</td></tr>
<tr><td><strong>Nature of the problem:</strong> </td><td>' . $_POST['problem'] . '</td></tr>
<tr><td><strong>Is this urgent:</strong> </td><td>' . $_POST['urgent'] . '</td></tr>
<tr><td><strong>Type of Urgency:</strong> </td><td>' . $_POST['urgent_type'] . '</td></tr>
<tr><td><strong>Master key access required:</strong> </td><td>' . $_POST['masteraccess'] . '</td></tr>
</table>
</body>
</html>'
;
//ATTACHMENT START
/* GET File Variables */
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
/* Start of headers */
$headers = "From: $from";
//**XXXXXXXXXXXXXXXX PROBLEM IS HERE XXXXXXXXXXXXXXXX**
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
//END OF ATTACHMENT
//Message/email to the customer.
$headers2 = "From: noreply@website.com.au";
$subject2 = "Thank you. Your e-mail has been logged.";
$autoreply = "Thank you. Your e-mail has been logged.
We will attend to your request and be in touch shortly.
Rest assured, we are doing all we can to speed up this process.
Regards,
Repairs Team";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($fname == '') {print "You have not entered your first name";}
else {
$send = mail($to, $subject, $message, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( "Location: http://www.website.com.au/thankyoupage/thankyou.html" );}
else
{print "We encountered an error sending your mail, please notify webmaster@website.com.au"; }
}
}
?>
我试图关注这篇文章,但我只是失去了它。 Php form upload and email
非常感谢任何帮助。
【问题讨论】:
-
任何人都可以回答.. 作为一种解决方法,因为将图像上传到表单时不会发生错误。如果我有一个“如果”没有上传图像,是否可以使用存储在服务器上的图像?我可以创建一个虚拟图像并每次都通过电子邮件发送。