我有这个表单发送电子邮件,访问者可以发送附件和他的电子邮件,因为一切都很好发送电子邮件,但附件来到这样的电子邮件
如果他上传任何C.V,则C.V将附加到此电子邮件中 --_ 1_c4fe3315ccb7d6076c71d64ec5265ecc内容类型: 多部分/替代;边界=" _2_c4fe3315ccb7d6076c71d64ec5265ecc"
--_ 2_c4fe3315ccb7d6076c71d64ec5265ecc Content-Type:text / plain; 字符集=" UTF-8"内容传输编码:7位
--_ 2_c4fe3315ccb7d6076c71d64ec5265ecc-- --_ 1_c4fe3315ccb7d6076c71d64ec5265ecc内容类型: 应用/八位字节流;命名=" Yousef.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";
}
}
?>
我从一些在线教程复制了这段代码,但无法理解为什么它不符合附件
提前致谢
答案 0 :(得分:1)
问题:我猜您在content-type declaration
中为His mail:
代码添加了额外空间。看到我的代码之前不需要空格,如果你给空间它会产生错误。
现在,下面给出了格式正确的工作代码:
<?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";
}
}
?>
我希望这能解决你的问题。因为它适用于我的HTML代码。
快乐编码!!!
答案 1 :(得分:1)
Hay @Mikky你只需要调整代码试试这个
<?php
error_reporting(E_ALL | E_STRICT);
if (isset($_POST['submit'])) {
$position = $c;
$name = $_POST['cName'];
$telephone = $_POST['cTelephone'];
$cEmail = $_POST['cEmail'];
$myCv = $_FILES['upFile']['name'];
$strTo = 'info@test.com';
$strSubject = "Someone apply for career";
$strMessage = nl2br("This information is for someone who apply for your new career\r\n
Position Applied For:" . $position . ",\r\n
His Name Is: " . $name . ",\r\n
His Phone Number: " . $telephone . ",\r\n
His Email: " . $cEmail . " \r\n");
//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));
$strHeader = "";
$strHeader .= "From:info@test.com\r\nReply-To:info@test.com";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"" . $strSid . "\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--" . $strSid . "\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage . "\n\n";
//*** Attachment ***//
if ($_FILES['upFile']['name'] != "") {
$strFilesName = $_FILES["upFile"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["upFile"]["tmp_name"])));
$strHeader .= "--" . $strSid . "\n";
$strHeader .= "Content-Type: application/octet-stream; name=\"" . $strFilesName . "\"\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment; filename=\"" . $strFilesName . "\"\n\n";
$strHeader .= $strContent . "\n\n";
}
$flgSend = @mail($strTo, $strSubject, null, $strHeader); // @ = No Show Error //
if ($flgSend) {
header("Location:index.php?pid=17&msg=done");
} else {
echo "Cannot send mail.";
}
}
?>
让我知道它现在和你一起工作
答案 2 :(得分:0)
function mailWithAttachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message, $totalmailsent)
{
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\n";
$header .= "Reply-To: ".$replyto."\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
$header .= "This is a multi-part message in MIME format.\n";
$header .= "--".$uid."\n";
$header .= "Content-type:text/html; charset=iso-8859-1\n";
$header .= $message."\n\n";
$header .= "--".$uid."\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\n\n";
$header .= $content."\n\n";
$header .= "--".$uid."--";
if(mail($mailto, $subject, "", $header))
{
return 1;
}
else
{
return 0;
}
}
你试一试,如果修好了我就让我,因为我用本机的php邮件
答案 3 :(得分:0)
我使用以下代码和 swiftmailer 来完成这项工作。
function send_email_attachment($email, $name, $filename) {
//return $email; //exit;
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('username')
->setPassword('password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$the_message = "Hi {$name},<br/> Your booking is CONFIRMED and the invoice pertaining to the booking is attached with this mail. <br/> ";
// Create a message
$message = Swift_Message::newInstance('Subject')
->setFrom(array('email' => 'name'))
->setTo(array($email => $name))
->setBody($the_message, 'text/html')
->attach(Swift_Attachment::fromPath($filename))
;
// Send the message
$result = $mailer->send($message);
//if ($result){ return 'DONE';}else{ return 'SORRY';}
return;
}
?>
当我将它用于我帮助开发的其中一个网站时,这很有效,我希望它也适合你。
<磷>氮。 B。:您需要获得swiftmailer才能使代码正常工作。答案 4 :(得分:0)
也许考虑使用PHPMailer这样的简化库?它可以处理大部分需要您花费时间的部分,您不需要添加标题以及所有这些 - 它已为您完成。
示例:
$mail=new PHPMailer();
$mail->isSMTP();
//host through which the mail should be sent
$mail->Host="smtp.mydomain.com";
//address where the email is going to be sent from
$mail->From="me@mydomain.com";
//address where the email is going
$mail->AddAddress("person@otherdomain.com");
//subject for the email
$mail->Subject="Example Email";
//Your email will be displayed as HTML
$mail->IsHTML(true);
// you can use HTML here
$mail->Body="Hi, This <strong>is</strong> an email";
//base64 encoding is applied here by default
$mail->AddAttachment("path/to/file");
//send the email
if ($mail->Send())
{
echo "Mail Sent";
}
else
{
echo "Error sending mail: " . $mail->ErrorInfo;
}
我通常使用与此非常相似的代码而没有任何问题。我希望这会有所帮助,让你的生活更加简单。请记住在脚本中包含PHP邮件程序库,请参阅tutorial here了解相关信息。