-1

I am having a problem where the PHP script sends the collected data fields as an attachment (array.htm) and the main message is blank. So no attachment and a blank message. Can you assist me?

<?php
// Email recipient & email subject
$to = "webmaster@webmaster.com";
$subject = 'This is a test';

// Collected parameters
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST ['telephone'];
$address = $_POST ['address'];
$postcode = $_POST ['postcode'];
$type = $_POST ['type'];
$description = $_POST['description'];
$attachments = $_FILES ['file'];
$path = $_SERVER['DOCUMENT_ROOT'];
if (!empty($_FILES['attachment']['tmp_name'])) {
    $path = $_FILES['attachment']['name'];
if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}

//PHP variables
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $attachments . "\"\n\n";

//Printed parameters
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Business Name:</b></td><td>' . $name . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Telephone:</b></td><td>' . $telephone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Address:</b></td><td>' . $address . '</td></tr>';
$message .= '<tr><td width="150px"><b>Postcode:</b></td><td>' . $postcode . '</td></tr>';
$message .= '<tr><td width="150px"><b>Business Type:</b></td><td>' . $type . '</td></tr>';
$message .= '<tr><td width="150px"><b>Description:</b></td><td>' . $description . '</td></tr>';
$message .= '</table>';

// Send mail
mail($to, $subject, $message, $headers);

// Redirect success
$theResults = header("Location: \success.html");
exit;
?>

UPDATE I changed the parameter 'attachment' to 'file' and now I get a correctly labelled attachment as per the file included in the form but the attachment still contains the form information text so it is not a true jpg as was attached originally and of course the message is still blank. BUT now I get the proper file attachment uploaded to the area of the server where the script runs. Does this help?

<?php
// Email recipient & email subject
$to = "webmaster@webmaster.com";
$subject = 'This is a test';

// Collected parameters
$name = $_POST['name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$type = $_POST['type'];
$description = $_POST['description'];
$file = $_FILES['file'];
$path = $_SERVER['DOCUMENT_ROOT'];
if (!empty($_FILES['file']['tmp_name'])) {
$path = $_FILES['file']['name'];
if (copy($_FILES['file']['tmp_name'], $path)) $file = $path;
}

//PHP variables
$headers = 'From: ' . $email . "\r\n" . 'Reply-To: ' . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Disposition: attachment; filename = \"" . $file . "\"\n\n";

//Printed parameters
$message = '<table width="100%" cellspacing="0" cellpadding="5" border="0">';
$message .= '<tr><td width="150px"><b>Business Name:</b></td><td>' . $name . '</td></tr>';
$message .= '<tr><td width="150px"><b>Email:</b></td><td>' . $email . '</td></tr>';
$message .= '<tr><td width="150px"><b>Telephone:</b></td><td>' . $telephone . '</td></tr>';
$message .= '<tr><td width="150px"><b>Address:</b></td><td>' . $address . '</td></tr>';
$message .= '<tr><td width="150px"><b>Postcode:</b></td><td>' . $postcode . '</td></tr>';
$message .= '<tr><td width="150px"><b>Business Type:</b></td><td>' . $type . '</td></tr>';
$message .= '<tr><td width="150px"><b>Description:</b></td><td>' . $description . '</td></tr>';
$message .= '</table>';

// Send mail
mail($to, $subject, $message, $headers);

// Redirect success
$theResults = header("Location: \success.html");
exit;
?>
gcmp
  • 1
  • 1
  • 3
    If *you* have a problem with the cumbersome manual mail/mime attachment construction, then that's *your* problem. Everyone else is using PHPMailer/SwitftMailer which simplify such things. And enabling `error_reporting` would clear up some of the path misapplication. Consider not overwriting variables. – mario Dec 14 '13 at 21:32
  • It's not relevant per se but add `html` and `body` tags to see if at least the message loads first – Onimusha Dec 14 '13 at 21:38
  • This is just a simple email form with a single attachment and I'm not aiming for installing frameworks. I like to stay away from that kind of thing and stick to the original construction. – gcmp Dec 14 '13 at 21:39
  • try checking out this question might help you out http://stackoverflow.com/questions/11004377/php-sending-emails-with-file-attachments-email-not-sending-at-all – Eli Y Dec 14 '13 at 21:40
  • what's your HTML form? Can you paste that too? – Onimusha Dec 14 '13 at 21:56

2 Answers2

4

As mario mentionned, I would definitively start using something like PHPMailer.

https://github.com/PHPMailer/PHPMailer

$mail->addAttachment('/var/tmp/file.tar.gz');

It will prove to ease up your email handling with PHP in the long run.

  • I know what you are saying as it may be an easy fix with a framework but I'm trying to learn more PHP as I usually learn what I need to know then experiment in new areas till I get it right. To be honest I am not a fan of Jquery in javascript and I do not know how well PHP libraries like PHP mailer are coded, either way it just seems overkill for what little I need. No offense intended, I just love learning :) – gcmp Dec 14 '13 at 21:54
  • 1
    I get you point. You can see how the library is implemented as it is completely open source. Though, if you really need to send email as part of a larger project, and need to use a remote SMTP, PHPMailer will help you doing it, where PHP's mail() will just let you down. This is just one of the things a well known library that as been developed over 10 years and used by probably tens of thousands of users will bring you. But won't discourage you to learn. – Jean-François Rioux Dec 14 '13 at 21:55
  • 1
    But reinventing the wheel may hurt you in the long run, and doesn't bring any real value to the table.... Learning from others, from their success and failures is as good if not way better than solving already solved problems with sub par solutions. No offence intended, as I once was in your shoes ;) – Jean-François Rioux Dec 14 '13 at 22:02
0

Space between $_POST ['telephone']; You need remove that spaces, like

$_POST['telephone'];
  ....
$_FILES['file'];
Krish R
  • 22,583
  • 7
  • 50
  • 59