简体   繁体   English

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

[英]PHP Email attachment will not store in the sent folder

I am working on PHP to store the emails in the sent folder. 我正在使用PHP将电子邮件存储在已发送的文件夹中。 I have got a problem with store the attachments in the sent folder because the attachment will not store in the sent folder as only the email message that will store in the sent folder. 我在将附件存储在已发送文件夹中时遇到问题,因为附件将不会存储在已发送文件夹中,而只会存储在已发送文件夹中的电子邮件。

Output for $attachment : $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

Here is the full code: 这是完整的代码:

<?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>";
?>

I am unable to find out why the email attachments did not store in the sent folder. 我无法找到为什么电子邮件附件未存储在已发送的文件夹中。 I have checked the file location and I have put the correct file location. 我已经检查了文件位置,并输入了正确的文件位置。 I have also checked the file name, the variable $attachment and everything I have put are correct so nothing have to do with it. 我还检查了文件名,变量$attachment和我输入的所有内容是否正确,因此与它无关。 I might have miss something but I am not sure what. 我可能会错过一些东西,但是我不确定。

Can you please show me an example how I can be able to store the email attachments in the sent folder? 您能给我一个例子,说明如何将电子邮件附件存储在已发送的文件夹中吗?

Thank you. 谢谢。

You aren't storing the attachment anywhere cause you only read the existing file and closing reading. 您不会将附件存储在任何地方,因为您只会读取现有文件并关闭阅读。 If you want to write somehere the $attachment variable you need to use something like this: 如果要在此处写入$attachment变量,则需要使用如下代码:

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

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

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