ここで私の試みは私が間違っていることを教えてくれますか?文字列からCSV添付ファイルを使用して電子メールを送信する方法
allputcsvは、配列をCSV形式の文字列に変換します。これはbase64でエンコードされ、チャンク分割されていますが、これは通常のファイルではうまくいくようですが、文字列を使用しているので何か別のものがありますか?
<?php
function sputcsv($row, $delimiter = ',', $enclosure = '"', $eol = "\n")
{
static $fp = false;
if ($fp === false)
{
$fp = fopen('php://temp', 'r+');
}
else
{
rewind($fp);
}
if (fputcsv($fp, $row, $delimiter, $enclosure) === false)
{
return false;
}
rewind($fp);
$csv = fgets($fp);
if ($eol != PHP_EOL)
{
$csv = substr($csv, 0, (0 - strlen(PHP_EOL))) . $eol;
}
return $csv;
}
function allputcsv($arr) {
$str = "";
foreach($arr as $val) {
$str .= sputcsv($val);
}
return $str;
}
function send_mail($arr) {
$to = '[email protected]';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$headers = "From: [email protected]\r\nReply-To: [email protected]";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"--".$random_hash."\"";
$attachment = chunk_split(base64_encode(allputcsv($arr)));
ob_start();
?>
--<?php echo $random_hash; ?>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--<?php echo $random_hash; ?>
Content-Type: application/vnd.ms-excel;
name="test.csv"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="test.csv"
<?php echo $attachment; ?>
--<?php echo $random_hash; ?>--
<?php
$message = ob_get_clean();
$mail_sent = @mail($to, $subject, $message, $headers);
}
$array = array(array(1,2,3,4,5,6,7),array(1,2,3,4,5,6,7),array(1,2,3,4,5,6,7));
send_mail($array);
?>
注:私は選択肢を持っていない(私はラッパーを使用することができますが、私はないことを好むだろう)と私は、サーバー側でファイルを保存カントメール機能を使用する必要があります。
に電子メールを送信する
と関連phpmailerのライン、私はそれがすべて今完璧に働いてしまったありがとうございました。 (最後の行でsend_mailはsend_csv_mailでなければなりません) –
ありがとうございます!私はこの問題を完全に間違った方法で行っていましたが、これですべてが解決されました! –