0

I am working on PHP to store the emails in the sent folder. 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:

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. 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.

Robert Jones
  • 390
  • 3
  • 18
  • attachments don't exist on their own in any imap folders. imap folders (and any other email folders) are where messages get stored. You can then view (or download) the attachments when viewing the email in that folder. I think you are leaving out something in terms of what isn't working for you. – Anthony Jul 01 '19 at 23:21

1 Answers1

-2

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:

$write_file = fopen('new_file.txt', 'w');
fwrite($write_file, $attachment);
  • yes I have stored the files in the uploads folder so this is a very bad post – Robert Jones Jul 01 '19 at 23:16
  • 1
    imap_append() does the storage –  Jul 01 '19 at 23:16
  • @tim yes `imap_append` the storage but how do you store the attachments? – Robert Jones Jul 01 '19 at 23:16
  • 1
    @RobertJones wish I could help, I believe the theory is fine but would have to spend quite some time testing to find an answer for you. –  Jul 01 '19 at 23:20
  • @RobertJones Read the first line [https://www.php.net/manual/en/function.imap-append.php](https://stackoverflow.com/questions/2649579/downloading-attachments-to-directory-with-imap-in-php-randomly-works) and then use google [https://stackoverflow.com/questions/2649579/downloading-attachments-to-directory-with-imap-in-php-randomly-works](https://stackoverflow.com/questions/2649579/downloading-attachments-to-directory-with-imap-in-php-randomly-works) – Нурлан Мамедов Jul 01 '19 at 23:31