php发送的电子邮件附件不正确

php发送的电子邮件附件不正确,php,forms,email,email-attachments,Php,Forms,Email,Email Attachments,我在php页面上使用以下代码发送带有附件的电子邮件: //define the receiver of the email $to = 'myemail@domain.com'; //define the subject of the email $subject = 'Test email with attachment'; //create a boundary string. It must be unique //so we use the MD5 algorithm

我在php页面上使用以下代码发送带有附件的电子邮件:

    //define the receiver of the email 
$to = 'myemail@domain.com'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['curriculum_vitae']['tmp_name'])));
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="attachment.zip"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
//定义电子邮件的收件人
$to$myemail@domain.com'; 
//定义电子邮件的主题
$subject='带附件的测试电子邮件';
//创建一个边界字符串。它必须是独一无二的
//所以我们使用MD5算法来生成一个随机散列
$random_hash=md5(日期('r',时间());
//定义我们想要传递的标题。请注意,它们用分隔符分隔\r\n
$headers=“From:webmaster@example.com\r\n适用于:webmaster@example.com"; 
//添加边界字符串和mime类型规范
$headers.=“\r\n内容类型:多部分/混合;边界=\”PHP混合-“$random\u散列。”\”;
//将atachment文件内容读入字符串,
//用MIME base64对其进行编码,
//然后把它分成小块
$attachment=chunk\u split(base64\u编码(文件获取内容($\u文件['Certural\u vitae']['tmp\u name']));
//定义消息的主体。
ob_start()//打开输出缓冲
?> 
--PHP混合-
内容类型:多部分/备选;boundary=“PHP alt-”
--PHP alt-
内容类型:文本/纯文本;charset=“iso-8859-1”
内容传输编码:7bit
你好,世界!!!
这是一封简单的短信。
--PHP alt-
内容类型:text/html;charset=“iso-8859-1”
内容传输编码:7bit
你好,世界!
这是HTML格式的东西。

--PHP alt-- --PHP混合- 内容类型:application/zip;name=“attachment.zip” 内容传输编码:base64 内容配置:附件 --PHP混合--
由于脚本工作正常,您需要以您上传的
简历文件的名称更改
附件.zip
,否则无论您上传什么文件,您都只能获得
附件.zip

因此,改变:

Content-Type: application/zip; name="attachment.zip"

内容类型:应用程序/zip;name=“”
如果附件未发布,请使用此选项发送附件

$to = 'myemail@domain.com'; 
$subject = 'Test email with attachment'; 
$random_hash = md5(date('r', time())); 
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com"; 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
if(isset($_FILES['curriculum_vitae']['name']) && $_FILES['curriculum_vitae']['name'] != ''){ $attachment = chunk_split(base64_encode(file_get_contents($_FILES['curriculum_vitae']['tmp_name']))); }
ob_start();
?> 
--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?>  
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

<?php if(isset($_FILES['curriculum_vitae']['name']) && $_FILES['curriculum_vitae']['name'] != ''){ ?>--PHP-mixed-<?php echo $random_hash; ?>  
Content-Type: application/zip; name="<?php echo $_FILES['curriculum_vitae']['name']; ?>"  
Content-Transfer-Encoding: base64  
Content-Disposition: attachment  

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>--
<?php } ?>
<?php 
$message = ob_get_clean(); 
$mail_sent = @mail( $to, $subject, $message, $headers ); 
echo $mail_sent ? "Mail sent" : "Mail failed"; 
$to='1!'myemail@domain.com'; 
$subject='带附件的测试电子邮件';
$random_hash=md5(日期('r',时间());
$headers=“From:webmaster@example.com\r\n适用于:webmaster@example.com"; 
$headers.=“\r\n内容类型:多部分/混合;边界=\”PHP混合-“$random\u散列。”\”;
如果(isset($_文件['Calumeru vitae']['name'])和($_文件['Calumeru vitae']['name'])!=''{$attachment=chunk_split(base64_编码(文件获取内容($_文件['Calumeru vitae']['tmp_name']));)
ob_start();
?> 
--PHP混合-
内容类型:多部分/备选;boundary=“PHP alt-”
--PHP alt-
内容类型:文本/纯文本;charset=“iso-8859-1”
内容传输编码:7bit
你好,世界!!!
这是一封简单的短信。
--PHP alt-
内容类型:text/html;charset=“iso-8859-1”
内容传输编码:7bit
你好,世界!
这是HTML格式的东西。

--PHP alt-- --PHP混合- 内容类型:application/zip;name=“” 内容传输编码:base64 内容配置:附件 --PHP混合---
这个简单的PHP脚本或电子邮件函数能够发送包含单个附加文件的纯文本邮件消息。必须先上载该文件,否则web服务器上应该有一个现有文件。你可以用这个

功能:

<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}
?>
<?php 
$my_file = "somefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>

代码:

<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
        echo "mail send ... OK"; // or use booleans here
    } else {
        echo "mail send ... ERROR!";
    }
}
?>
<?php 
$my_file = "somefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>


适合我。可能您的
mail()
功能有问题,您的主机不允许使用
mail()
您可以在下载电子邮件后在电脑上正确打开附件吗?上传文件的扩展名可能有问题吗?谢谢Mihai,工作很好。最后,我想问的第一个问题是索尔文有什么问题吗?我的意思是,将附件输入留空,不会出现错误?我遇到了一些问题。如果你有'booking.php',我应该有这个文件的内容($_文件['calumory\u vitae']['tmp\u name']);正确的?我仍然收到警告信息:/对不起,那是我的一个文件,做了一些测试,没有文件:)完美的Mihai。谢谢你所做的一切!Mihai很抱歉再次打扰你。我注意到,当我阅读电子邮件时,带有类似“a”或“ç”字符的电子邮件会出现问题。它们看起来是这样的:“çÃo”。我的问题是,这是我正在使用的网络邮件程序的问题,还是我在发送电子邮件的代码中使用的编码有问题?