0

**Update: Updated the code and added the email contents as seen in Gmail. **

I have a problem which I've been trying to solve a couple of days. But since I have been unable to make any progress, I'd like the help of someone else. I'm trying to send a zip file from my server to my email. However, the email is being sent empty. This is the code:

`$headers = "From: $from";
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\"";   
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"

Attached, please find your backup file. 

--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment

--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";`

Thanks to all reading and wanting to help.

The resulting email as seen in Gmail is

Attached, please find your backup file.

--PHP-mixed-e44b531b0e0538185289abc521eda78d
Content-Type: application/zip; name=backup-29-11-2014-1417275285-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment

UEsDBBQAAAAIAFZ8fUUs...sUEsFBgAAAAACAAIAeAAAAD4B

AAAAAA==

--PHP-mixed-e44b531b0e0538185289abc521eda78d--
user2604185
  • 63
  • 10
  • I can't spot an error, have you checked if all the variables are getting filled properly? i.e. echo $headers, echo $random, echo $message ... – chrki Nov 29 '14 at 14:22
  • $headers is `From : Content-Type: multipart/mixed; boundary:"PHP-mixed-ed0c7bd6c7024332067e17602743495e"`. The rest shows up fine too. – user2604185 Nov 29 '14 at 14:27
  • Ok I looked through various email source codes and some tutorials, I noticed this in your headers: ```boundary: PHP-mixed-$random``` - try ```=``` instead of ```:``` like this: ```boundary = PHP-mixed-$random``` – chrki Nov 29 '14 at 15:08
  • @chrki I did that but now all the email is treated as text. For example, the attachment is part of the body (as text). – user2604185 Nov 29 '14 at 15:16

1 Answers1

2

I got it working by doing the following changes:

  • Removed space character in From: headers, it's an invalid header otherwise
  • Changed boundary: to boundary=
  • Removed intendation in $message because that seems to break the email. The boundary string --PHP-mixed-random separates the different parts of the email, like plain text, html text, attachments from each other. When there are spaces in front of the boundary separator, it is not interpreted as the separator anymore, but as normal text.

Here's the code:

<?php
$from = "xxx@myserver";
$email = "xxx@gmail.com";
$subject = "backup file";
$date = "29-11-2014";
$time = "1417275285"; // test file "backup-29-11-2014-1417275285-9.zip" in the same folder

$headers = "From: $from"; // remove space
/* Generate boundary string */
$random = md5(time());
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random."\""; // : to =
/* Read the file */
$attachment = chunk_split(base64_encode(file_get_contents("backup-$date-$time-9.zip")));
/* Define body */
$message = "--PHP-mixed-$random
Content-Type: text/plain; charset=\"iso-8859-1\"

Attached, please find your backup file. 

--PHP-mixed-$random
Content-Type: application/zip; name=backup-$date-$time-9.zip
Content-Transfer-Encoding: base64
Content-Disposition: attachment

$attachment

--PHP-mixed-$random--"; // no intendation
$mail = mail($email, $subject, $message, $headers);
echo $mail ? "Email sent<br>" : "Email sending failed<br>";
chrki
  • 6,143
  • 6
  • 35
  • 55
  • Sorry but this does not work as it should. The contents of the email are as I have put in the edited question. – user2604185 Nov 30 '14 at 11:17
  • Well I'm using this exact same script (I added the missing variables in an edit) and the email shows up perfectly fine in Gmail as well as Mozilla Thunderbird – chrki Nov 30 '14 at 12:09
  • Including linebreaks and spaces? – user2604185 Nov 30 '14 at 12:11
  • Yes, I literally just copy and pasted my code, email looks good: Screenshot http://i.imgur.com/KSUZn8a.png – chrki Nov 30 '14 at 12:28
  • Does your $message end with AAAAAA==? Could that be the problem? I don't know from where it's coming. – user2604185 Nov 30 '14 at 12:31
  • Mine ends in AAAA= but that shouldn't be the problem I think, here's the email source code: http://pastebin.com/NLTneFCA – chrki Nov 30 '14 at 12:48
  • I've played around the whole thing and identified the problem to be when I put the attachment part (starting with PHP mixed after the text) in line with the other code (4 spaces indented). If I leave the attachment part not indented it works. I know you've already written that but I think I took it in a wrong way. What could be causing the problem and how can I make this part in line with the rest of the code, please? – user2604185 Nov 30 '14 at 13:23
  • I've manged to fix this. Could you please add a note in the answer that the whole text (or the attachment part) should be right next to the margin? Then, I'll be able to set this as answer. – user2604185 Nov 30 '14 at 13:40
  • 1
    I added 2 sentences explaining what is going on. Here's a thread on SO I found about putting long strings in PHP code, it shows several possbilites: http://stackoverflow.com/questions/1848945/best-practices-working-with-long-multiline-strings-in-php – chrki Nov 30 '14 at 13:45