我需要随邮件发送一份pdf,可以吗?
$to = "xxx";
$subject = "Subject" ;
$message = 'Example message with <b>html</b>';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: xxx <xxx>' . "\r\n";
mail($to,$subject,$message,$headers);
我错过了什么?
HTML代码:
<form enctype="multipart/form-data" method="POST" action="">
<label>Your Name <input type="text" name="sender_name" /> </label>
<label>Your Email <input type="email" name="sender_email" /> </label>
<label>Your Contact Number <input type="tel" name="contactnumber" /> </label>
<label>Subject <input type="text" name="subject" /> </label>
<label>Message <textarea name="description"></textarea> </label>
<label>Attachment <input type="file" name="attachment" /></label>
<label><input type="submit" name="button" value="Submit" /></label>
</form>
PHP代码:
<?php
if($_POST['button']){
{
//Server Variables
$server_name = "Your Name";
$server_mail = "your_mail@domain.com";
//Name Attributes of HTML FORM
$sender_email = "sender_email";
$sender_name = "sender_name";
$contact = "contactnumber";
$mail_subject = "subject";
$input_file = "attachment";
$message = "description";
//Fetching HTML Values
$sender_name = $_POST[$sender_name];
$sender_mail = $_POST[$sender_email];
$message = $_POST[$message];
$contact= $_POST[$contact];
$mail_subject = $_POST[$mail_subject];
//Checking if File is uploaded
if(isset($_FILES[$input_file]))
{
//Main Content
$main_subject = "Subject seen on server's mail";
$main_body = "Hello $server_name,<br><br>
$sender_name ,contacted you through your website and the details are as below: <br><br>
Name : $sender_name <br>
Contact Number : $contact <br>
Email : $sender_mail <br>
Subject : $mail_subject <br>
Message : $message.";
//Reply Content
$reply_subject = "Subject seen on sender's mail";
$reply_body = "Hello $sender_name,<br>
\t Thank you for filling the contact form. We will revert back to you shortly.<br><br>
This is an auto generated mail sent from our Mail Server.<br>
Please do not reply to this mail.<br>
Regards<br>
$server_name";
//#############################DO NOT CHANGE ANYTHING BELOW THIS LINE#############################
$filename= $_FILES[$input_file]['name'];
$file = chunk_split(base64_encode(file_get_contents($_FILES[$input_file]['tmp_name'])));
$uid = md5(uniqid(time()));
//Sending mail to Server
$retval = mail($server_mail, $main_subject, "--$uid\r\nContent-type:text/html; charset=iso-8859-1\r\nContent-Transfer-Encoding: 7bit\r\n\r\n $main_body \r\n\r\n--$uid\r\nContent-Type: application/octet-stream; name=\"$filename\"\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"$filename\"\r\n\r\n$file\r\n\r\n--$uid--", "From: $sender_name <$sender_mail>\r\nReply-To: $sender_mail\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"$uid\"\r\n\r\n");
//Sending mail to Sender
$retval = mail($sender_mail, $reply_subject, $reply_body , "From: $server_name<$server_mail>\r\nMIME-Version: 1.0\r\nContent-type: text/html\r\n");
//#############################DO NOT CHANGE ANYTHING ABOVE THIS LINE#############################
//Output
if ($retval == true) {
echo "Message sent successfully...";
echo "<script>window.location.replace('index.html');</script>";
} else {
echo "Error<br>";
echo "Message could not be sent...Try again later";
echo "<script>window.location.replace('index.html');</script>";
}
}else{
echo "Error<br>";
echo "File Not Found";
}
}else{
echo "Error<br>";
echo "Unauthorised Access";
}
您可以尝试使用以下代码:
$filename = 'myfile';
$path = 'your path goes here';
$file = $path . "/" . $filename;
$mailto = 'mail@mail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($file);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: name <test@test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
编辑14 - 6 - 2018
为更多的可读性在一些电子邮件提供商
使用
$body .= $eol。美元的消息。eol美元。eol美元;而且
$body .= $eol。美元的内容。eol美元。eol美元;
这对我很有用。它还附加了多个附件。很容易
<?php
if ($_POST && isset($_FILES['file'])) {
$recipient_email = "recipient@yourmail.com"; //recepient
$from_email = "info@your_domain.com"; //from email using site domain.
$subject = "Attachment email from your website!"; //email subject line
$sender_name = filter_var($_POST["s_name"], FILTER_SANITIZE_STRING); //capture sender name
$sender_email = filter_var($_POST["s_email"], FILTER_SANITIZE_STRING); //capture sender email
$sender_message = filter_var($_POST["s_message"], FILTER_SANITIZE_STRING); //capture message
$attachments = $_FILES['file'];
//php validation
if (strlen($sender_name) < 4) {
die('Name is too short or empty');
}
if (!filter_var($sender_email, FILTER_VALIDATE_EMAIL)) {
die('Invalid email');
}
if (strlen($sender_message) < 4) {
die('Too short message! Please enter something');
}
$file_count = count($attachments['name']); //count total files attached
$boundary = md5("specialToken$4332"); // boundary token to be used
if ($file_count > 0) { //if attachment exists
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:" . $from_email . "\r\n";
$headers .= "Reply-To: " . $sender_email . "" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//message text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($sender_message));
//attachments
for ($x = 0; $x < $file_count; $x++) {
if (!empty($attachments['name'][$x])) {
if ($attachments['error'][$x] > 0) { //exit script and output error if we encounter any
$mymsg = array(
1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
2 => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3 => "The uploaded file was only partially uploaded",
4 => "No file was uploaded",
6 => "Missing a temporary folder");
die($mymsg[$attachments['error'][$x]]);
}
//get file info
$file_name = $attachments['name'][$x];
$file_size = $attachments['size'][$x];
$file_type = $attachments['type'][$x];
//read file
$handle = fopen($attachments['tmp_name'][$x], "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
$body .= "--$boundary\r\n";
$body .= "Content-Type: $file_type; name=" . $file_name . "\r\n";
$body .= "Content-Disposition: attachment; filename=" . $file_name . "\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n";
$body .= "X-Attachment-Id: " . rand(1000, 99999) . "\r\n\r\n";
$body .= $encoded_content;
}
}
} else { //send plain email otherwise
$headers = "From:" . $from_email . "\r\n" .
"Reply-To: " . $sender_email . "\n" .
"X-Mailer: PHP/" . phpversion();
$body = $sender_message;
}
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if ($sentMail) { //output success or failure messages
die('Thank you for your email');
} else {
die('Could not send mail! Please check your PHP mail configuration.');
}
}
?>
$to = "to@gmail.com";
$subject = "Subject Of The Mail";
$message = "Hi there,<br/><br/>This is my message.<br><br>";
$headers = "From: From-Name<from@gmail.com>";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=ISO-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
$filepath = 'uploads/'.$_FILES['image']['name'];
move_uploaded_file($_FILES['image']['tmp_name'], $filepath); //upload the file
$filename = $_FILES['image']['name'];
$file = fopen($filepath, "rb");
$data = fread($file, filesize($filepath));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$filename\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$filename\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
mail($to, $subject, $message, $headers);