我对PHP发送邮件有疑问。我正在尝试通过在线表格在PHP的帮助下发送附件。我收到的电子邮件中的数据非常好,但附件有时如下所示:
2 application octet-stream; name=""
(这是纯文本格式,带有此名称的附件)。我在做什么错了?
<?php
// if button is pressed - ACTION
if (isset($_POST['reg_application'])) {
// get fields into variables
$firstname = strip_tags($_POST['firstname']);;
$lastname = strip_tags($_POST['lastname']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
// get other form fields into variables
$country = strip_tags($_POST['country']);
$experience = strip_tags($_POST['experience']);
$englishlevel = strip_tags($_POST['englishlevel']);
$availability = strip_tags($_POST['availability']);
$licence = strip_tags($_POST['drivingLicence']);
$nursing = strip_tags($_POST['nursing']);
$signed = "Signed";
//Deal with the email
$to = 'example@example.com';
$subject = "+1 $country - $firstname $lastname";
//Deal with sending
//$message = strip_tags($_POST['message']);
$message = "ALL DETAILS\n\nFirst Name: $firstname\nLast Name: $lastname\nEmail: $email\nPhone: $phone\nCountry: $country\nExperience: $experience\nEnglish: $englishlevel\nAvailability: $availability\nDriving Licence: $licence\nNursing Experience: $nursing\nAccepted T&C: $signed";
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = "From: example@example.com\r\nReply-To: daniel@hidd.co.uk";
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\"";
$message="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/plain; charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit
$message
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $message, $headers);
header("Location: success.php?application-sent");
}
?>
答案 0 :(得分:0)
尝试以下代码:
$name = "Name";
$email = "Email Address";
$to = "$name <$email>";
$from = "XYZ";
$subject = "TEST SUBJECT";
$mainMessage = "Hi, here's the file.";
$fileatt = "./test.pdf";
$fileatttype = "application/pdf";
$fileattname = "newname.pdf";
$headers = "From: $from";
// File
$file = fopen ( $fileatt, 'rb' );
$data = fread ( $file, filesize ( $fileatt ) );
fclose ( $file );
// Attach the file
$semi_rand = md5 ( time () );
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" . "-{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\n" . "Content-Transfer-Encoding: 7bit\n\n" . $mainMessage . "\n\n";
$data = chunk_split ( base64_encode ( $data ) );
$message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatttype};\n" . " name=\"{$fileattname}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileattname}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "-{$mime_boundary}-\n";
// Send Email
if (mail ( $to, $subject, $message, $headers )) {
echo "The email was sent.";
} else {
echo "There was an error sending the mail.";
}