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

PHP-尝试从带有附件的脚本发送电子邮件,但不是attachingHi,php,email,Php,Email,我有一个正在动态创建的文件,如下所示: // 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 busines

我有一个正在动态创建的文件,如下所示:

// 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);
文件正在保存到文件系统中,一封带有正确正文和主题的电子邮件正在发送给我

唯一出错的是附件是零字节的未命名文件。知道为什么会这样吗?这是权限问题吗?还是我邮件里的什么


谢谢

您正在将mime数据放入
$attachment
中,但不要在任何地方使用该变量,因此实际上没有附加任何内容


您最好使用诸如或之类的库来为您执行此操作。这大大减少了麻烦,而且在出现问题时,它们提供了更好的诊断。

您将mime数据放入
$attachment
中,但是不要在任何地方使用该变量,因此您实际上没有附加任何内容


您最好使用诸如或之类的库来为您执行此操作。这大大减少了麻烦,而且在出现故障时,它们提供了更好的诊断。

您的电子邮件代码似乎缺少一些东西。我开始修复它,但最好从头开始。我推荐一个库,但如果没有,下面的代码应该可以满足您的需要:

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

您的电子邮件代码似乎缺少一些内容。我开始修复它,但最好从头开始。我推荐一个库,但如果没有,下面的代码应该可以满足您的需要:

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

有趣……我会研究一下,但在这种情况下,我应该添加mime内容作为额外参数吗?不。mime数据将成为消息体的一部分。但就像我说的,不要这样做。手工制作一封mime电子邮件很痛苦,而且很容易出错。你的整个附件过程可以用它们简化为一行代码。你放在标题中的基本上只是一些脚手架。你仍然没有将实际文件附加到任何地方。是的,这就是我想知道的…我实际上如何附加它?:)有趣……我会研究一下,但在这种情况下,我应该添加mime内容作为额外参数吗?不。mime数据将成为消息体的一部分。但就像我说的,不要这样做。手工制作一封mime电子邮件很痛苦,而且很容易出错。你的整个附件过程可以用它们简化为一行代码。你放在标题中的基本上只是一些脚手架。你仍然没有将实际文件附加到任何地方。是的,这就是我想知道的…我实际上如何附加它?:)如图所示,构建自己的mime附件系统非常痛苦,而且非常冗长。使用phpmailer或swiftmailer可以将整个代码块减少到大约4行。这很有效,所以我接受了它。我还不想在这上面花太多时间,所以这真的很有帮助。非常感谢!马克,我完全明白你的意思。Dully指出:)如图所示,构建自己的mime附件系统是痛苦的,而且非常冗长。使用phpmailer或swiftmailer可以将整个代码块减少到大约4行。这很有效,所以我接受了它。我还不想在这上面花太多时间,所以这真的很有帮助。非常感谢!马克,我完全明白你的意思。沉闷地说:)