LandoR LandoR - 7 months ago 18
PHP Question

loosing part of image during mail delivery

I've got a problem sending email with php. The first line of my base64 encoded image disappears during mail delivery. What am I doing wrong here?

This is part of the printed message before sending:

...

--67e5a910fa8cffc2b52b2aec743f9332
Content-Disposition: attachment
Content-Type: image/svg; name="Nyos_XS_roland_21-04-2016_06:54:33.svg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Nyos_XS_roland_21-04-2016_06:54:33.svg"
PHN2ZyB2aWV3Qm94PSIwIDAgMjk2NjkgMjA5OTAiIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcv
ZGMvZWxlbWVudHMvMS4xLyIgeG1sbnM6Y2M9Imh0dHA6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL25z
...


This is the message i receive:

...

--67e5a910fa8cffc2b52b2aec743f9332
Content-Disposition: attachment
Content-Type: image/svg; name="Nyos_XS_roland_21-04-2016_06:54:33.svg"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="Nyos_XS_roland_21-04-2016_06:54:33.svg"

ZGMvZWxlbWVudHMvMS4xLyIgeG1sbnM6Y2M9Imh0dHA6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL25z
...


my code:

<?php

if($_POST) {
date_default_timezone_set('Europe/Berlin');
$uid = md5(uniqid(time()));

$eol = PHP_EOL;

$name = $_POST['name'];
$gliderName = $_POST['glider'];
$gliderSize = $_POST['size'];

$subject = $gliderName . "_" . $gliderSize . "_" . $name . "_" . date('d-m-Y_h:i:s', time());
$subject = str_replace(' ', '_', $subject);

$comment = $_POST['comment'];
$attachment = $_POST['image'];
$sendToSwing = $_POST['sendToSwing'];

$mail = $_POST['email'];
$mail_to = $mail;

if ($sendToSwing) {
$mail_to .= "," . "ZIELADRESSE@SWING.DE";
}

$mail_from = "ABSENDERADRESSE@SWING.DE";
$from_name = "ABSENDERNAME";

$header = "From: " . $from_name . " <" . $mail_from . ">" . $eol;
$header .= "Reply-To: " . $mail_from.$eol;
$header .= "MIME-Version: 1.0" . $eol;
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"" . $eol . $eol;

$msg = '<html><head><title>' . $subject . '</title></head><body>' . $eol;
$msg .= '<b>glider:</b> ' . $gliderName . $eol;
$msg .= '<b>size:</b> ' . $gliderSize . $eol;
$msg .= '<b>customer name:</b> ' . $name . $eol;
$msg .= '<b>customer email:</b> ' . $mail . $eol;
$msg .= '<b>message from customer:</b> ' . $comment . $eol;
$msg .= '</body></html>' . $eol;

$message = "--" . $uid . $eol;
$message .= "Content-type:text/html; charset=utf-8" . $eol;
$message .= "Content-Transfer-Encoding: utf-8" . $eol . $eol;
$message .= $msg . $eol;

// attachment
//echo $eol.$attachment.$eol;
$attachment = chunk_split(base64_encode($attachment));
//echo $eol.$attachment.$eol;

$message .= "--" . $uid . $eol;
$message .= "Content-Type: image/svg; name=\"" . $subject . ".svg\"" . $eol;
$message .= "Content-Transfer-Encoding: base64" .$eol;
$message .= "Content-Disposition: attachment; filename=\"" . $subject . ".svg\"" . $eol;
$message .= $attachment . $eol;
$message .= "--".$uid."--";

echo $eol.$message.$eol;

if (mail($mail_to, $subject, $message, $header)) {
echo "success ";
} else {
echo "error sending email";
}

}

?>

Answer

Like this it should work (two new line before the payload):

$message .= "Content-Disposition: attachment; filename=\"" . $subject . ".svg\"" . $eol.$eol;
Comments