dongzhan1878 2012-05-19 18:19
浏览 42
已采纳

PHP - 尝试从带有附件的脚本发送电子邮件,但它不附加Hi

I have a file that I am creating on the fly like this:

// Create and save the string on the file system
$str = "Business Plan: 
Some more text";
$fp = fopen("alex.txt", 'w+');
fwrite($fp, $str);

// email with the attachment
$to = 'alex.genadinik@gmail.com'; 
$subject = 'Your business plan attached';

//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 
 
$headers = "From: BusinessPlanApp@example.com"; 
//add boundary string and mime type specification 
$headers .= "
Content-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('alex.txt')));

$contents = "some contents of the email";                   

mail($to, $subject, $contents, $headers);

And the file is saving to the file system AND an email is getting sent to me with the right body and subject.

The only thing going wrong is the attachment is unnamed file of zero bytes. Any idea why that might happen? Is it a permissions issue? Or something in my email?

Thanks!

  • 写回答

2条回答 默认 最新

  • douao1926 2012-05-19 18:49
    关注

    You email code seems to be missing a few things. I began fixing it but it might be better to start from scratch. I'd recommend a library, but if not, the following code should achieve what you want:

    // Create and save the string on the file system
    $str = "Business Plan: 
    Some more text";
    $fp = fopen("alex.txt", 'w+');
    fwrite($fp, $str);
    fclose($fp);
    
    // email fields: to, from, subject, and so on
    $from = "BusinessPlanApp@example.com";
    $to = 'alex.genadinik@gmail.com';
    $subject = 'Your business plan attached';
    $headers = "From: $from";
    
    // boundary
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
    // headers for attachment
    $headers .= "
    MIME-Version: 1.0
    " . "Content-Type: multipart/mixed;
    " . " boundary=\"{$mime_boundary}\"";
    
    // message text
    $contents = "This is the email content";
    
    // multipart boundary
    $message = "--{$mime_boundary}
    " . "Content-Type: text/plain; charset=\"iso-8859-1\"
    " .
        "Content-Transfer-Encoding: 7bit
    
    " . $contents. "
    
    ";
    
    // preparing attachments
    $message .= "--{$mime_boundary}
    ";
    $fp =    fopen('alex.txt',"rb");
    $data =    fread($fp,filesize('alex.txt'));
    fclose($fp);
    $data = chunk_split(base64_encode($data));
    $message .= "Content-Type: application/octet-stream; name=\"".basename('alex.txt')."\"
    " .
        "Content-Description: ".basename('alex.txt')."
    " .
        "Content-Disposition: attachment;
    " . " filename=\"".basename('alex.txt')."\"; size=".filesize('alex.txt').";
    " .
        "Content-Transfer-Encoding: base64
    
    " . $data . "
    
    ";
    $message .= "--{$mime_boundary}--";
    mail($to, $subject, $message, $headers);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办