Nam Tab
Nam Tab

Reputation: 25

Recieving broken attachments from webform to cpanel webmail

I have a simple web form on my site, which requests a few fields of data from the user;

Name, Email and Message, as well as an image file.

For a few years, everything seemed to work smoothly with a gmail account set as the receiving email address.

However... recently, with no changes made to the code at all, the emails started coming in very slowly, and sometimes not at all.

Out of frustration, I created a webmail account with Roundcube through my cPanel, to receive the form submissions.

After testing, the submissions are showing up consistently now and very fast. I receive all of the texts portions of the data, every time.

That's the good news.

That bad news is that attachments (images) are broken. I receive a displayed outline of the image which shows the correct image name and file extension, but the file size shows '0' and the actual image is not displayed.

I can even 'download' the broken image, but even then, the file size is still '0' and still cannot be displayed.

Again... I haven't changed anything in the code except the recipient email address, from a gmail account to the webmail account.

When I used the very same code with gmail, the images always came in just fine.

I was on the phone with my hosting tech support last night for about 2 hours and we checked many things on the server side, including some Roundcube settings, such as; 'Always display images in message' etc...

We had no luck solving the mystery.

Can anyone recognize anything in this code which may cause the images to come in broken specifically in webmail?

Or if the code is in order, perhaps some other suggestions as to why this is happening?

Any help is appreciated... thank you.

<?php

$fileName =  $_FILES['userImages']['name'];
$tmpName = $_FILES['userImages']['tmp_name'];
$name = $_POST['userName'];
$email = $_POST['userEmail'];
$message = $_POST['userMessage'];

$composition =

"\r\n\n\nName: "                        . $name .
"\r\nEmail Address: "                   . $email .
"\r\n\n\nMessage: "                     . $message;

$subject ="User Message";

$fromname ="$name";
$fromemail = "$email";

$mailto = '[email protected]';

$content = file_get_contents($tmpName);
$content = chunk_split(base64_encode($content));

$separator = md5(time());

$eol = "\r\n";

$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;

$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $composition . $eol;

$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $fileName . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";

@mail($mailto, $subject, $body, $headers);

header('location: /sent_confirmation_page.html');

?>

Upvotes: 0

Views: 100

Answers (0)

Related Questions