【发布时间】:2015-03-22 03:37:41
【问题描述】:
此代码似乎可以正常工作,但是当我打开邮件上的附件时,它无法打开或出错。一切都很好,除了附件。有人可以帮忙吗。谢谢。
<?php
if (isset($_POST["send"])) {
$subject = "Applicant";
$name = $_POST['contact_name'];
$position = $_POST['position'];
$tmpName = $_FILES['attachment']['tmp_name'];
$fileType = $_FILES['attachment']['type'];
$fileName = $_FILES['attachment']['name'];
# Open a file
$file = fopen($tmpName, "r" );
if( $file == false )
{
echo "Error in opening file";
exit();
}
# Read the file into a variable
$size = $_FILES['attachment']['size'];
$content = fread( $file, $size);
# encode the data for safe transit
# and insert \r\n after every 76 chars.
$encoded_content = chunk_split(base64_encode($content));
# Get a random 32 bit number using time() as seed.
$num = md5( time() );
# Define the main headers.
$header = "From: Applicant\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; ";
$header .= "boundary=$num\r\n";
$header .= "--$num\r\n";
# Define the position section
$header .= "Content-Type: text/plain\r\n";
$header .= "Content-Transfer-Encoding:8bit\r\n\n";
$header .= "$name\r\n";
$header .= "$position\r\n";
$header .= "--$num\r\n";
# Define the attachment section
$header .= "Content-Type: multipart/mixed; ";
$header .= "name=\"$fileName\"\r\n";
$header .= "Content-Transfer-Encoding:base64\r\n";
$header .= "Content-Disposition:attachment; ";
$header .= "filename=\"$fileName\"\r\n\n";
$header .= "$encoded_content\r\n";
$header .= "--$num--";
$message = "Name: ".$name."\r\n"."Position: ".$position;
# Send email now
$retval = mail ('sample@gmail.com', $subject, $message, $header);
if( $retval == true ){
echo "<div style='border-style: solid; border: thick double #00386c; margin-bottom: 10px;'><h1 style='text-align: center; color: #00386c; padding: 10px;'>Your resume was sent! Thank you.</h1></div>";
}else{
echo "error";exit;
}
}
?>
【问题讨论】:
-
你在用phpmailer发邮件吗???
-
我不确定它是否是一个phpmailer,但代码是在服务器上上传的。 @拉尔
标签: php html email sendmail contact-form