【问题标题】:Attachment on email sent by php is incorrectphp发送的电子邮件附件不正确
【发布时间】:2012-08-30 09:33:43
【问题描述】:

我在我的 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"; 

但是,我遇到了一些问题和疑问,如果你们能帮助我,我将不胜感激。

首先,如果我将附件的输入留空,我会收到一个错误“警告:file_get_contents() [function.file-get-contents]: Filename cannot be empty in ...”

我希望附件是可选的,所以如果我留空我想忽略这个错误,这可能吗?

其次,当我发送带有附件的电子邮件并从电子邮件中下载时,我无法打开发送的文件。我得到一个 .zip 文件,尝试打开时收到以下消息:“存档格式未知或已损坏”。

你知道问题可能是什么吗?

谢谢!

【问题讨论】:

  • 为我工作。可能你的mail()函数有问题,你的主机不允许使用mail()
  • 邮件的附件下载后可以在电脑上正确打开吗?会不会是上传文件的扩展名有问题?

标签: php forms email email-attachments


【解决方案1】:

由于脚本完美运行,您需要将attachment.zip 更改为您上传的curriculum_vitae 文件的名称,否则无论您上传什么文件,您都只会得到attachment.zip

所以改变:

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

Content-Type: application/zip; name="<?php echo $_FILES['curriculum_vitae']['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"; 

【讨论】:

  • 感谢米海,完美运行。刚刚结束,我的第一个问题的问题有什么解决方法吗?我的意思是,将附件输入留空并且不会出错?
  • 我遇到了一些问题。在你有'booking.php'的地方我应该有这个 file_get_contents($_FILES['curriculum_vitae']['tmp_name'])));对?我仍然收到警告消息:/
  • 抱歉,这是我的一个文件,做了一些测试,没有一个文件:)
  • 完美的米海。非常感谢您所做的一切!
  • 米海很抱歉再次打扰您。我注意到当我阅读电子邮件时,带有“à”或“ç”这样的字符的电子邮件会出现问题。它们看起来像这样:“ção”。我的问题是,这是我正在使用的网络邮件程序的问题,或者它必须与我在用于发送电子邮件的代码中使用的编码有关?
【解决方案2】:

这个简单的 PHP 脚本或电子邮件函数能够发送包含单个附件的纯文本邮件消息。该文件必须先上传,否则您的网络服务器上应该有一个现有文件。你可以用这个

功能:

<?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);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 2011-09-16
    相关资源
    最近更新 更多