我正在尝试创建联系人表单,以便附加文件。我是PHP的新手。我试着在youtube上查找视频,但找不到任何关于它的完整信息。我希望你能帮助我学习PHP。
此外,如果有更好的替代方案或方法,请与我分享。
HTML
<form style="display:flex; flex-direction: column;" action="../mail/mail.php" method="post">
<input type="text" id="name" name="name" placeholder="Your full name.." required="required" />
<label for="email">Email</label>
<input type="email" id="email" name="email" placeholder="Your e-mail.." required="required" />
<label for="country">Country</label>
<select id="country" name="country" required="required">
<option value="Netherlands">Netherlands</option>
<option value="Saint Kitts and Nevis">Saint Kitts and Nevis</option <option value="Tajikistan">Tajikistan</option>
</select>
<label for="subject">Subject</label>
<select id="subject" name="subject" required="required">
<option value="Choose">Click here to select..</option>
<option value="Choose1">Click here to select1..</option>
<option value="Choose2">Click here to select.2.</option></select>
<div class="attachment-row">
<input id="attachment-file" type="file" class="input-field" name="attachment[]">
</div>
<label for="message">Message</label>
<textarea id="message" name="message" placeholder="Write your message here.."style="height:200px"></textarea>
<input type="submit" class="btn-send-message" name="submit" value="Send" /></form>
PHP
<?php
ob_start();
if(isset($_POST['submit'])){
$to = "info@example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$country = $_POST['country'];
$subject = $_POST['subject'];
$message = "From: ". $from . "nn" . "Subject: ". $subject . "nn" . "Country: ". $country ."nn"."Name: ". $name ."nn". "Wrote the following:" . "nn" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "nn" . $_POST['message'];
mail($to,$subject,$message);
mail($from,$subject2,$message2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
header("Location: ../thank-you-for-contacting.php");
}
ob_end_flush();
?>
首先,您根本不需要输出缓冲区。您在这里所做的只是将一条回波线加载到内存中,然后将其转储到最后。
请阅读:https://www.php.net/manual/en/function.ob-start.php
您当前的代码甚至不查找附件,您需要以下内容:
if (count($_POST['attachment'])) {...}
接下来你需要弄清楚的是电子邮件本身的结构。对于基础知识,请快速阅读:https://www.php.net/manual/en/function.mail
将头和消息本身分开总是一个好主意,创建只使用一次的变量是一个坏主意。所以你可以试试这样的东西:
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$headers[] = 'To: '.filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
}
// They're both really bad filters, for application security You should do your research and define your own. Character encoding is a big deal btw.
一旦你的电子邮件格式正确,你应该看看前面提到的附件。要了解这些信息是如何发送的,请阅读:https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
TLDR;//如果你想编码东西,没有什么是太长的。。。但这应该提供一些清晰的
header[] = "Content-Type: multipart/mixed; boundary=$UNIQUE_string";
$message = "--$UNIQUE_string".PHP_EOL;
$message .= "Content-type: text/plain;".PHP_EOL;
$message .= $content.PHP_EOL;
$message .= "--$UNIQUE_string".PHP_EOL;
$message .= 'Content-Disposition: form-data; name="myFile"; filename="foo.txt"'.PHP_EOL;
$message .= "Content-Type: text/plain".PHP_EOL;
$message .= file_get_contents('foo.txt').PHP_EOL;
$message .= "--$UNIQUE_string--".PHP_EOL;