PHP邮件格式赢得';t将所有表单字段发送到电子邮件
PHP邮件格式赢得';t将所有表单字段发送到电子邮件,php,email,Php,Email,s_name和s_email字段不会显示在发送的电子邮件中,只显示附件和邮件。请协助更改代码,以便在电子邮件中显示所有表单字段 我看了很多帖子,但似乎找不到问题的答案,我将不胜感激 <?php if($_POST && isset($_FILES['file'])) { $recepient_email = "recepient@yourmail.com"; //recepient $from_email = "info@your_do
s_name
和s_email
字段不会显示在发送的电子邮件中,只显示附件和邮件。请协助更改代码,以便在电子邮件中显示所有表单字段
我看了很多帖子,但似乎找不到问题的答案,我将不胜感激
<?php
if($_POST && isset($_FILES['file'])) {
$recepient_email = "recepient@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("sanwebe.com");
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($recepient_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.');
}
}
?>
你有额外的双引号吗?应该是这样的:
$headers .= "Reply-To:" .$sender_email. "\r\n";
只有在检查长度是否小于4时,电子邮件中才使用您的$sender\u名称。您能看到所有表单字段都在这样做吗
<?php print_r($_POST); ?>
仔细检查所有表单字段是否都有
名称
attr,以及它们是否与上面打印的字段匹配。尝试隔离或指出需要帮助的代码部分。
<?php print_r($_POST); ?>