PHP 发送文件的邮件脚本不工作
PHP mail script with send file don't work
为什么我的脚本不起作用?
我尝试使用 xampp 和在线主机。
我试过 $headers = "From: $from \r\n";
我收到了 "Sent successfully!" 但我没有收到任何邮件。
$to = "********";
$from = $_POST['email'];
$name = $_POST['nume'];
$名字 = $_POST['prenume'];
$phone = $_POST['telefon'];
$city = $_POST['oras'];
$地址 = $_POST['adresa'];
$faculty = $_POST['facultate'];
$title1 = $_POST['titlu1'];
$desc1 = $_POST['desc1'];
$title2 = $_POST['titlu2'];
$desc2 = $_POST['desc2'];
$title3 = $_POST['titlu3'];
$desc3 = $_POST['desc3'];
$subject = "Luminile Iernii - Inscriere: $nume $prenume";
$message = " 姓氏和姓氏:$surname $surname\n 电子邮件:$from\n 电话号码:$phone\n 城市:$city\n 地址:$address\n 教育机构:$faculty\n照片 1 的标题:$title1 \n 照片 1 的描述:$desc1 \n 照片 2 的标题:$title2 \n 照片 2 的描述:$desc2 \n 照片 3 的标题:$title3 \n 照片 3 的描述: $desc3\n";</p>
<pre><code> // Temporary paths of selected files
$file1 = $_FILES['file1']['tmp_name'];
$file2 = $_FILES['file2']['tmp_name'];
$file3 = $_FILES['file3']['tmp_name'];
// File names of selected files
$filename1 = "Fotografie 1";
$filename2 = "Fotografie 2";
$filename3 = "Fotografie 3";
// array of filenames to be as attachments
$files = array($file1, $file2, $file3);
$filenames = array($filename1, $filename2, $filename3);
// include the from email in the headers
$headers = "From: $from";
// boundary
$time = md5(time());
$boundary = "==Multipart_Boundary_x{$time}x";
</code>
附件是否需要边界?
<code>
// headers used for send attachment with email
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\"";
// multipart boundary
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$boundary}\n";
// attach the attachments to the message
foreach( $files as $key => $value )
{
if( empty( $value ) )
{
unset( $files[$key] );
unset( $filenames[$key] );
}
}
for($x = 0; $x < count($files); $x++) {
$file = fopen($files[$x], "r");
$content = fread($file,filesize($files[$x]));
fclose($file);
$content = chunk_split(base64_encode($content));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n";
$message .= "--{$boundary}\n";
}
// sending mail
$sendmail = mail($to, $subject, $message, $headers);
// verify if mail is sent or not
if ($sendmail) {
echo "Sent successfully!";
} else {
echo "Error occurred. Try again!";
}
</code>
谁能给我解释一下?
我不明白...我做错了什么?
您的发件人是否设置了用于发送文件的 enctype?
<form action="upload.php" method="post" enctype="multipart/form-data">
尝试使用 http://github.com/PHPMailer/PHPMailer
这是一个 PHP 插件,无需编写所有代码即可处理发送邮件的所有压力任务。
从 http://github.com/PHPMailer/PHPMailer 下载 PHPMailer 脚本并将其上传到您的服务器。
像这样包含该文件:<?php require_once('/path/to/class.phpmailer.php'); ?>
您的最终结果将如下所示:
require_once('/path/to/class.phpmailer.php');
$email = new PHPMailer();
$email->From = 'you@example.com';
$email->FromName = 'Your Name';
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
$email->Send();
如果您尝试在没有此 class 文件的情况下发送附件,您将编写一堆不需要的代码。这个插件使发送电子邮件变得更加容易,最终发送带有附件的电子邮件也更加容易。
希望对您有所帮助。
为什么我的脚本不起作用?
我尝试使用 xampp 和在线主机。
我试过 $headers = "From: $from \r\n";
我收到了 "Sent successfully!" 但我没有收到任何邮件。
$to = "********";
$from = $_POST['email'];
$name = $_POST['nume'];
$名字 = $_POST['prenume'];
$phone = $_POST['telefon'];
$city = $_POST['oras'];
$地址 = $_POST['adresa'];
$faculty = $_POST['facultate'];
$title1 = $_POST['titlu1'];
$desc1 = $_POST['desc1'];
$title2 = $_POST['titlu2'];
$desc2 = $_POST['desc2'];
$title3 = $_POST['titlu3'];
$desc3 = $_POST['desc3'];
$subject = "Luminile Iernii - Inscriere: $nume $prenume";
$message = " 姓氏和姓氏:$surname $surname\n 电子邮件:$from\n 电话号码:$phone\n 城市:$city\n 地址:$address\n 教育机构:$faculty\n照片 1 的标题:$title1 \n 照片 1 的描述:$desc1 \n 照片 2 的标题:$title2 \n 照片 2 的描述:$desc2 \n 照片 3 的标题:$title3 \n 照片 3 的描述: $desc3\n";</p>
<pre><code> // Temporary paths of selected files
$file1 = $_FILES['file1']['tmp_name'];
$file2 = $_FILES['file2']['tmp_name'];
$file3 = $_FILES['file3']['tmp_name'];
// File names of selected files
$filename1 = "Fotografie 1";
$filename2 = "Fotografie 2";
$filename3 = "Fotografie 3";
// array of filenames to be as attachments
$files = array($file1, $file2, $file3);
$filenames = array($filename1, $filename2, $filename3);
// include the from email in the headers
$headers = "From: $from";
// boundary
$time = md5(time());
$boundary = "==Multipart_Boundary_x{$time}x";
</code>
附件是否需要边界?
<code> // headers used for send attachment with email $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$boundary}\""; // multipart boundary $message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $message .= "--{$boundary}\n"; // attach the attachments to the message foreach( $files as $key => $value ) { if( empty( $value ) ) { unset( $files[$key] ); unset( $filenames[$key] ); } } for($x = 0; $x < count($files); $x++) { $file = fopen($files[$x], "r"); $content = fread($file,filesize($files[$x])); fclose($file); $content = chunk_split(base64_encode($content)); $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$filenames[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $content . "\n\n"; $message .= "--{$boundary}\n"; } // sending mail $sendmail = mail($to, $subject, $message, $headers); // verify if mail is sent or not if ($sendmail) { echo "Sent successfully!"; } else { echo "Error occurred. Try again!"; } </code>
谁能给我解释一下?
我不明白...我做错了什么?
您的发件人是否设置了用于发送文件的 enctype?
<form action="upload.php" method="post" enctype="multipart/form-data">
尝试使用 http://github.com/PHPMailer/PHPMailer
这是一个 PHP 插件,无需编写所有代码即可处理发送邮件的所有压力任务。
从 http://github.com/PHPMailer/PHPMailer 下载 PHPMailer 脚本并将其上传到您的服务器。
像这样包含该文件:<?php require_once('/path/to/class.phpmailer.php'); ?>
您的最终结果将如下所示:
require_once('/path/to/class.phpmailer.php');
$email = new PHPMailer();
$email->From = 'you@example.com';
$email->FromName = 'Your Name';
$email->Subject = 'Message Subject';
$email->Body = $bodytext;
$email->AddAddress( 'destinationaddress@example.com' );
$file_to_attach = 'PATH_OF_YOUR_FILE_HERE';
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );
$email->Send();
如果您尝试在没有此 class 文件的情况下发送附件,您将编写一堆不需要的代码。这个插件使发送电子邮件变得更加容易,最终发送带有附件的电子邮件也更加容易。
希望对您有所帮助。