I have an html mail form on my site, which should allow users to attach an image file to their form submissions.
For a few years, the form had been working like a charm.
*Note: I have been directing the form data to a gmail account.
Recently, the form submissions began to be severely delayed in arriving, or never showed up at all.
I do not recall changing anything in the code that could have possibly caused this sudden change.
I have tested the issue by redirecting the form data to other email platforms, with 'partial' success.
More specifically... meaning that the form submissions do seem to arrive at the newly directed destination platforms consistently when tested.
However... the attachments (images) do not successfully arrive. The attachments arrive broken (0 bytes) and cannot be downloaded or viewed on any of the other platforms.
This leads me to believe that the culprit may be somewhere in the file attachment part of the php code itself. This theory may explain why gmail rejects the submissions altogether, and why the other platform deliver the submissions, but with broken attachments.
However, as I mentioned, I have not altered the code in any way, and I have examined it several times, but I cannot spot any bugs or issues.
Any help in solving this mystery would be appreciated.
I have included my php-handler code below...
PHP HANDLER:
<?php
$fileName = $_FILES['userImages']['name'];
$tmpName = $_FILES['userImages']['tmp_name'];
$name = $_POST['userName'];
$email = $_POST['userEmail'];
$composition =
"\r\n\n\nName: " . $name .
"\r\nEmail Address: " . $email;
$fromname ="$name";
$fromemail = "$email";
$mailto = 'receiving@email.com';
$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.php');
?>
You are missing a number of $eol
in your email content (especially in the attachment part)
You have not specified the $subject (e.g. $subject="Test email PHP";)
So please amend to :
<?php
$fileName = $_FILES['userImages']['name'];
$tmpName = $_FILES['userImages']['tmp_name'];
$name = $_POST['userName'];
$email = $_POST['userEmail'];
$subject="Test email PHP";
$composition ="\r\n\n\nName: " . $name . "\r\nEmail Address: ". $email;
$fromname =$name;
$fromemail = $email;
$mailto = 'receiving@email.com';
$content = file_get_contents($tmpName);
$content = chunk_split(base64_encode($content));
$separator = md5(uniqid(time()));
$eol = "\r\n";
$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed;". $eol. " boundary=\"" . $separator . "\"" . $eol;
// $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$body .= $composition . $eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream;". $eol. " name=\"" . $fileName . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment;". $eol . " filename=\"" . $fileName."\"" . $eol . $eol ;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
mail($mailto, $subject, $body, $headers);
//header('location: /sent_confirmation.php');
echo "Done";
?>
By the way, please note that for xxx@gmail.com recipients, the gmail server may filter out your email because of various reasons (no PTR record / SPF / DMARC / DKIM, etc.) which you may have to fix separately. So please try the above for non-gmail account again first.