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条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?