简体   繁体   English

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

[英]PHP - trying to send email from script with attachment, but its not attachingHi

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: \nSome 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 \r\n 
$headers = "From: BusinessPlanApp@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('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! 谢谢!

You're putting your mime data into $attachment , but then don't use that variable anywhere, so you're not actually attaching anything. 您要将mime数据放入$attachment ,但不要在任何地方使用该变量,因此实际上并没有附加任何内容。

You'd be better off using a library, such as PHPMailer or Swiftmailer , to do this for you. 您最好使用PHPMailerSwiftmailer之类的库来为您完成此操作。 It's far less hassle and they provide FAR better diagnostics in case of trouble. 这样麻烦就少了很多,它们可以在发生故障时为FAR提供更好的诊断。

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: \nSome 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 .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

// message text
$contents = "This is the email content";

// multipart boundary
$message = "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" . $contents. "\n\n";

// preparing attachments
$message .= "--{$mime_boundary}\n";
$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')."\"\n" .
    "Content-Description: ".basename('alex.txt')."\n" .
    "Content-Disposition: attachment;\n" . " filename=\"".basename('alex.txt')."\"; size=".filesize('alex.txt').";\n" .
    "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}--";
mail($to, $subject, $message, $headers);

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

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