PHP函数成功,但未收到邮件

  • 本文关键字:函数 成功 PHP php email
  • 更新时间 :
  • 英文 :


我用PHP制作了一个表单来发送邮件,当我在本地测试它时,它运行良好,我收到一封包含所有信息的电子邮件,但是当我把它放在主机上时,它说成功,但我从未收到邮件。也许是代码有问题,因为我有一个没有ATTACH文件的其他形式,它运行完美

$filenameee =  $_FILES['file']['name'];
$fileName = $_FILES['file']['tmp_name']; 
$name = $_POST['name'];
$email = $_POST['email'];
$title=$_POST['title'];
$prototip=$_POST['prototip'];
$description = $_POST['descripton'];
$message ="Name = ". $name . "rn  Email = " . $email . "rn rn Naslov = ".$title . "rn Opis = ".$description; 
$subject ="My email subject";
$mailto = 'levchegochev@gmail.com';  //the email which u want to recv this email


$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "rn";
// main header (multipart mandatory)
$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="" . $filenameee . """ . $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 "<script>

swal({
title: 'Ви благодариме за вашата апликација!',
text: 'Kе ве контактираме во рок од 24 часа',
icon: 'success',
button: 'Супер!',
});
</script>";



} else {
echo "<script>alert('Mail was not sent. Please try again later');</script>";
}

更新答案

在html表单中设置以下参数:

<form action="your_target_php_file.php" method="post" enctype="multipart/form-data">
PHP文件:

if ($_SERVER['REQUEST_METHOD'] == "POST") {
// file data from html element input[type='file']
$file = (object) [
'tmp_name' => $_FILES['file']['tmp_name'],
'name' => $_FILES['file']['name'],
'size' => $_FILES['file']['size'],
'type' => $_FILES['file']['type'],
'error' => $_FILES['file']['error'],
];
// other POST data
$post = (object) [
'name' => $_POST['name'],
'email' => $_POST['email'],
'title' => $_POST['title'],
'prototip' => $_POST['prototip'],
'description' =>  $_POST['descripton']
];
// email message
$message = "Name = ". $post->name . "rn  Email = " . $post->email . "rnrn Naslov = ";
$message .= $post->title . "rn Opis = ".$post->description;
// email subject
$subject = "My email subject";
//the email which u want to recv this email
$mailto = 'levchegochev@gmail.com';
//read from the uploaded file
$handle = fopen($file->tmp_name, "r");
$content = fread($handle, $file->size);
fclose($handle);
$encoded = chunk_split(base64_encode($content));
// boundary
$boundary = md5("random");
// email headers
$headers[] = "MIME-Version: 1.0";
$headers[] = "From: " .explode("@", $post->email)[0]. " <{$post->email}>";
$headers[] = "Reply-To: {$mailto}";
$headers[] = "Content-Type: multipart/mixed;";
$headers[] = "boundary = {$boundary}";
//plain text
$body = "--{$boundary}rn";
$body .= "Content-Type: text/plain; charset=ISO-8859-1rn";
$body .= "Content-Transfer-Encoding: base64rnrn";
$body .= chunk_split(base64_encode($message));
//attachment
$body .= "--$boundaryrn";
$body .="Content-Type: {$file->type}; name={$file->name}rn";
$body .="Content-Disposition: attachment; filename={$file->name}rn";
$body .="Content-Transfer-Encoding: base64rn";
$body .="X-Attachment-Id: ".rand(1000, 99999)."rnrn";
// Attaching the encoded file with email
$body .= $encoded;

if (mail($post->email, $subject, $body, implode("rn", $headers)))
echo ("<script>
swal({
title: 'Ви благодариме за вашата апликација!',
text: 'Kе ве контактираме во рок од 24 часа',
icon: 'success',
button: 'Супер!',
});
</script>");
} else {
echo "<script>window.alert('Mail was not sent. Please try again later');</script>";
}
}

我稍微修改了一下你的代码,我相信它会适合你的。