【发布时间】:2014-06-25 16:16:47
【问题描述】:
我有这个表格来发送电子邮件,访问者可以用他的电子邮件发送附件,一切都会顺利发送的电子邮件,但附件会像这样发送到电子邮件中
如果他上传了任何 C.V,C.V 将附在此电子邮件中 --_1_c4fe3315ccb7d6076c71d64ec5265ecc 内容类型: 多部分/替代;边界="_2_c4fe3315ccb7d6076c71d64ec5265ecc"
--_2_c4fe3315ccb7d6076c71d64ec5265ecc 内容类型:文本/纯文本; charset="UTF-8" 内容传输编码:7bit
--_2_c4fe3315ccb7d6076c71d64ec5265ecc-- --_1_c4fe3315ccb7d6076c71d64ec5265ecc 内容类型: 应用程序/八位字节流;名称="优素福.txt" 内容传输编码:base64 内容处置:附件
--_1_c4fe3315ccb7d6076c71d64ec5265ecc--
我可以看到我附加的文件名,但电子邮件没有附件
HTML 表单
<form id="sendYourCV" method="post" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data"
class="padding_16px">
<div class="padding_16 clear_both">
<p class="f_right complaints_form"><label for="cSector">القطاع :</label></p>
<select name="cSector" id="cSector" class="styled-select f_right">
<?php
$getSections = $db->prepare("SELECT * FROM career_selection_sec_arabic");
$getSections->execute();
$res = $getSections->get_result();
while ($s = $res->fetch_object()) {
echo "<option value='$s->sector'>$s->sector</option>";
}
?>
</select>
</div>
<div class="padding_16 clear_both">
<p class="f_right complaints_form"><label for="cAdministration">الادارة :</label></p>
<select name="cAdministration" id="cAdministration" class="styled-select f_right">
<?php
$getSections = $db->prepare("SELECT * FROM career_selection_admin_arabic");
$getSections->execute();
$res = $getSections->get_result();
while ($a = $res->fetch_object()) {
echo "<option value='$a->admin'>$a->admin</option>";
}
?>
</select>
</div>
<div class="padding_16 clear_both">
<p class="f_right complaints_form"><label for="cBranch">الفرع :</label></p>
<select name="cBranch" id="cBranch" class="styled-select f_right">
<?php
$getSections = $db->prepare("SELECT * FROM career_selection_bran_arabic");
$getSections->execute();
$res = $getSections->get_result();
while ($s = $res->fetch_object()) {
echo "<option value='$s->branch'>$s->branch</option>";
}
?>
</select>
</div>
<div class="padding_16 clear_both">
<p class="f_right complaints_form"><label for="cCareer">الوظيفة :</label></p>
<select name="cCareer" id="cCareer" class="styled-select f_right">
<?php
$getSections = $db->prepare("SELECT * FROM career_selection_car_arabic");
$getSections->execute();
$res = $getSections->get_result();
while ($s = $res->fetch_object()) {
echo "<option value='$s->career'>$s->career</option>";
}
?>
</select>
</div>
<div class="padding_16 clear_both m_top_10px">
<p class="f_right complaints_form">الاسم :</p>
<input name="cName" type="text" class="form_textarea_rec f_right" placeholder="اسمك"/>
</div>
<div class="padding_16 clear_both m_top_10px">
<p class="f_right complaints_form">التليفون :</p>
<input name="cTelephone" type="text" class="form_textarea_rec f_right" placeholder="تليفونك"/>
</div>
<div class="padding_16 clear_both m_top_10px">
<p class="f_right complaints_form">الايميل :</p>
<input name="cEmail" type="text" class="form_textarea_rec f_right" placeholder="بريدك الالكتروني"/>
</div>
<div class="padding_16 clear_both m_top_10px">
<p class="f_right complaints_form">الرسالة :</p>
<span class="margin_49px">
<textarea name="cMessage" class="form_textarea_vertical_2 f_right m_left_6px" placeholder="رسالتك"
type="text"></textarea>
</span>
</div>
<div class="padding_16 clear_both m_top_10px">
<p class="f_right complaints_form"></p>
</div>
<div class="padding_16 clear_both m_top_10px">
<p class="f_right complaints_form">سيرتك الذاتية :</p>
<input type="file" name="upFile" id="upFile" class="f_right" style="direction:rtl;"/>
</div>
<input type="submit" name="submit" id="submit" class="btn_main m_bottom_18px m_left_286px"
value="ارسل"/>
</form>
这是我使用的表单代码
<?php
error_reporting(E_ALL | E_STRICT);
if (isset($_POST['submit'])) {
$sector = $_POST['cSector'];
$cAdministration = $_POST['cAdministration'];
$cBranch = $_POST['cBranch'];
$cCareer = $_POST['cCareer'];
$name = $_POST['cName'];
$telephone = $_POST['cTelephone'];
$cEmail = $_POST['cEmail'];
$cMessage = $_POST['cMessage'];
$recipient = 'info@test.com';
$subject = "Someone apply for career";
$myCv = $_FILES['upFile']['name'];
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['upFile']['tmp_name'])));
$boundary = md5(date('r', time()));
$content = "This information is for someone who apply for your new career\n
Sector Applied For:" . $sector . ",\n
Administration Applied For:" . $cAdministration . ",\n
Branch Applied For:" . $cBranch . ",\n
Career Applied For:" . $cCareer . ",\n
His Name Is: " . $name . ",\n
His Phone Number: " . $telephone . ",\n
His Message: " . $cMessage . ",\n
His Email: " . $cEmail . ",\n if he uploaded any C.V the C.V will be attached to this Email
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"UTF-8\"
Content-Transfer-Encoding: 7bit
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$myCv\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
$headers = "From:info@test.com\r\nReply-To:info@test.com";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$sent = mail($recipient, $subject, $content, $headers);
if ($sent) {
header("Location:index.php?pid=17&carr=your message has been sent, we will contact you soon.");
} else {
echo "Error";
}
}
?>
我从一些在线教程中复制了这段代码,但不明白为什么它不能与附件一起使用
提前致谢
【问题讨论】:
-
省去你的痛苦,找一个为你做 MIME 的库。
-
不仅是mime,还有安全性(标头注入等)。了解幕后发生的事情确实很好,但您不需要重新发明已经非常好的现有轮子。
-
@Quentin 你能举个例子,我在哪里可以找到这个库或任何可以完成这项工作的应用程序谢谢
-
@Mkky 谷歌。
php mail library -
@Mikky 看我的回答here