简体   繁体   English

PHP 将 HTML 格式的文件发送到邮件

[英]PHP Send file in HTML form to mail

i want the send 2 files attached to an email by using an html form.我希望使用 html 表单将 2 个文件附加到电子邮件中。 The mail is send with the 2 files but I can't see the files when i download (size: 0 ko).邮件与 2 个文件一起发送,但我下载时看不到这些文件(大小:0 ko)。 Cant you help me please?你能帮我吗? Here is the code of my php file:这是我的php文件的代码:

$boundary = "-----=".md5(uniqid(rand()));

$header  = "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\r\n";
$header .= "\r\n";

$msg = "Je vous informe que ceci est un message au format MIME 1.0 multipart/mixed.\r\n";

$msg .= "--$boundary\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding:8bit\r\n";
$msg .= "\r\n";
$msg .= "Ceci est un mail avec 2 fichiers joints\r\n";
$msg .= "\r\n";

$file = $_FILES['icone']['name'];
$fp   = fopen($file, "rb");   // le b c'est pour les windowsiens
$attachment = fread($fp, filesize($file));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));

$msg .= "--$boundary\r\n";
$msg .= "Content-Type: multipart/mixed; name=\"$file\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment; filename=\"$file\"\r\n";
$msg .= "\r\n";
$msg .= $attachment . "\r\n";
$msg .= "\r\n\r\n";

$file = $_FILES['nom_fichier']['name'];
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);
$attachment = chunk_split(base64_encode($attachment));

$msg .= "--$boundary\r\n";
$msg .= "Content-Type: multipart/mixed; name=\"$file\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "Content-Disposition: attachment; filename=\"$file\"\r\n";
$msg .= "\r\n";
$msg .= $attachment . "\r\n";
$msg .= "\r\n\r\n";

$msg .= "--$boundary--\r\n";

$destinataire = "jeanb@hotmail.com";
$expediteur   = $_POST['email'];
$reponse      = $expediteur;
echo "Ce script envoie un mail avec 2 fichiers joints à $destinataire";
mail($destinataire,
     "Email avec 2 fichiers joints (dont 1 inline)",
     $msg,
     "Reply-to: $reponse\r\nFrom: $destinataire\r\n".$header);

The warning message is:警告信息是:

Warning: fopen(date horloge.txt) [function.fopen]: failed to open stream: No such file or directory in /home/public_html/mail_candidat.php on line 34警告:fopen(date horloge.txt) [function.fopen]:无法打开流:第 34 行的 /home/public_html/mail_candidat.php 中没有这样的文件或目录

Warning: filesize() [function.filesize]: stat failed for date horloge.txt in /home/public_html/mail_candidat.php on line 35警告:filesize() [function.filesize]:第 35 行 /home/public_html/mail_candidat.php 中的日期 horloge.txt 统计失败

Warning: fread(): supplied argument is not a valid stream resource in /home/public_html/mail_candidat.php on line 35警告:fread():在第 35 行的 /home/public_html/mail_candidat.php 中提供的参数不是有效的流资源

Warning: fclose(): supplied argument is not a valid stream resource in /home/public_html/mail_candidat.php on line 36警告:fclose():在第 36 行的 /home/public_html/mail_candidat.php 中提供的参数不是有效的流资源

Ce script envoie un mail avec 2 fichiers joints à jeanb@hotmail.com Ce 脚本 envoie un mail avec 2 fichiers Joints à jeanb@hotmail.com

The path to the file is stored in 'tmp_name' and not in name.文件的路径存储在“tmp_name”中而不是名称中。 Name how the file is called but not the path to the file.命名文件的调用方式,而不是文件的路径。

in your example that would be.在你的例子中就是这样。

Before

$file = $_FILES['nom_fichier']['name'];
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);

After

$file = $_FILES['nom_fichier']['tmp_name'];
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);

fopen cannot find the file ( No such file or directory ). fopen找不到文件(没有这样的文件或目录)。 All the other warnings are related to that.所有其他警告都与此相关。

Check the path returned and see if It returns a valid path.检查返回的路径并查看它是否返回有效路径。

You have a working directory (That is a directory where is your php file) in this case this is : /home/public_html/ but you have to know did you have a permisson to upload a file to this directory, or you have a tmp or any other writeable directory.你有一个工作目录(这是你的 php 文件所在的目录),在这种情况下是:/home/public_html/ 但你必须知道你是否有将文件上传到这个目录的权限,或者你有一个 tmp或任何其他可写目录。

Extend your code like this:像这样扩展你的代码:

$file =  basename($_FILES['icone']['name']);
if (!move_uploaded_file($_FILES['icone']['tmp_name'], $file)) {    echo "Error";} 
echo "file: ".$file; //For debug only
$fp = fopen($file, "rb");
$attachment = fread($fp, filesize($file));
fclose($fp);
echo $attachment; //For debug only

If it is not work, please comment the warning or error message.如果它不起作用,请评论警告或错误消息。

As I see you didnt uploaded a File.我看到你没有上传文件。

Your error is said same "ailed to open stream: No such file or directory in /home/public_html/mail_candidat.php"您的错误是相同的“无法打开流:/home/public_html/mail_candidat.php 中没有这样的文件或目录”

Try to upload file before sending it:在发送之前尝试上传文件:

http://www.php.net/manual/en/features.file-upload.post-method.php http://www.php.net/manual/en/features.file-upload.post-method.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM