PHP电子邮件附件不会存储在已发送的文件夹中

时间:2019-07-01 22:48:17

标签: php email email-attachments

我正在使用PHP将电子邮件存储在已发送的文件夹中。我在将附件存储在已发送文件夹中时遇到问题,因为附件将不会存储在已发送文件夹中,而只会存储在已发送文件夹中的电子邮件。

$attachment的输出:

iVBORw0KGgoAAAANSUhEUgAABQAAAAOyCAIAAACjVO/kAAAAAXNSR0IArs4c6QAAAARnQU1BAACx jwv8YQUAAAA
JcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhe7P0FeBxJguYPz80y3e7et3uw97/bmbuZm94d3J7pmQY3T3P3NMM0
u9vuNkObmW2ZmdluM5PclplkSbYkSxZYzMxosb83K6KisqJAVVKV8P097yNHRkZERgZkxevMyvrefdKdaWpquldXX11b
W1ldU1FVTVEURVEURVFU1xFW6VirY8WOdbtcZNOhQa4u1Lf0EjTS1EURVEURVHdRVi9Yw0vV/Okk6AB7n40NDZW+db6Vlb
pMRRFURRFURTVa+XP5TFW8ljPy5U96XBogLsZmC3aFGqvKh1iKIqiKIqiKKqXy8+L5MZGPhHdOdAAdycwT7SZQ1EURVEURVFUd
1SPuQ+cHB0RfPrYvhULVo0bAiGATUTK3V0MGuBuQ3NzszZnKIqiKIqiKIrqvsIKX671uycF 2Rk75k9fPOxLp8IuJJBJuww0wP5l
w+ZtGzdvlxvto6qGr7yiKIqiKIqiqK6rvIJCSIt0o6qaWrnW bytZWVm7d+9ebWHr1q13796VO7wn4naUDHlG8OljmuN1KiSTGboG
7TLANTU1U6bPhuS2H7gRGnYj JKyhoUFu+wjU/Mixk6j5Z18OgBDYtWc/IsVeuFYRaCcoR5Tffg9c3+Drr/5SFEVRFEVRFOVTnTl3EdIi3
att74VuaWnB3+PHjy9atOjcuXMXLlwICgq6ePHi8uXL9+zZI9J4i1cGODjwqLK4i4Z+ocJO Y8IvBslsXYC2G2DhfuHuJk+bJaP8AAxw0L
kLvvXAV64FDxwyUlhTsxCJXcK1yqTtQJSDxoEQaKcHrqzWp4or5RUUbt2x65sxEyEEtL0URVEURVEURflJI8dMwCJci3Svymp5E64NBAcHh
4aGbt68eefO nbt27Vq7du3Vq1cjIiLa9pvDnhvgguwMs7817O7wfkuG9xN/jYCDB/b

这是完整的代码:

<?php

$dmy=date("d-M-Y H:i:s"); 
$dmy.= " +0200"; // Had to do this bit manually as server and me are in different timezones 

$username = 'myusername';
$password = 'mypassword';

$mailserver = '{imap.example.com:993/imap/ssl/novalidate-cert}INBOX.Sent';
$mailbox = imap_open($mailserver, $username, $password);
$messageID = sprintf("<%s.%s@%s>",
    base_convert(microtime(), 10, 36),
    base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
    'example.com');

$boundary = "------=".md5(uniqid(rand())); 
$header = "MIME-Version: 1.0 "; 
$header .= "Content-Type: multipart/mixed; boundary=".$boundary; 
$header .= " "; 
$file = "uploads/add-attachment.png"; 
$filename = "add-attachment.png"; 
$ouv = fopen ("$file", "rb");
$lir = fread ($ouv, filesize ("$file"));
fclose($ouv); 

$attachment = chunk_split(base64_encode($lir)); 

$msg2 .= "--$boundary "; 
$msg2 .= "Content-Transfer-Encoding: base64 "; 
$msg2 .= "Content-Disposition: attachment; filename=\"$filename\"\r\n" ;
$msg2 .=  "\r\n" . $attachment . "\r\n"; 
$msg2 .= "\r\n\r\n\r\n"; 
$msg2 .= "--$boundary--\r\n\r\n"; 
$msg2 .= " "; 
$msg3 .= "--$boundary-- "; 
imap_append($mailbox, $mailserver,

    "From: rob@example.com r\n".
    "To: ddsgds@gmail.com \r\n".
    "Subject: This is the subject \r\n".
    "Date: ".date("r", strtotime("now"))."\r\n".
    "Message-ID: ".$messageID."\r\n".
    "MIME-Version: 1.0 \r\n".
    "Content-Type: multipart/mixed; boundary=".$boundary.
    "Content-Transfer-Encoding: 7bit \r\n".
    "\r\n\r\n".
    $msg2.
    $msg3,
    "\\Seen"
    );
imap_close ($mailbox);

echo "message have been stored in the sent folder, now let check";
echo "<br>";
?>

我无法找出为什么电子邮件附件未存储在已发送的文件夹中。我已经检查了文件位置,并输入了正确的文件位置。我还检查了文件名,变量$attachment和我输入的所有内容是否正确,因此与它无关。我可能会错过一些东西,但是我不确定。

能否请您举一个例子,说明如何将电子邮件附件存储在已发送的文件夹中?

谢谢。

1 个答案:

答案 0 :(得分:-2)

您没有将附件存储在任何地方,因为您只能阅读现有文件并关闭阅读。如果要在此处写$attachment变量,则需要使用如下代码:

$write_file = fopen('new_file.txt', 'w');
fwrite($write_file, $attachment);