Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Email issue

Status
Not open for further replies.

pundabuyer

Programmer
Jun 9, 2011
2
GB
Hi Guys,

Im having an issue! I have an html form in a php document that auto submits to itself.

The web form allows a file upload which does upload fine. i use the following code to try and send the form via email to the recipient:

<?
if (isset($_POST['control1'])){
// Load Variables from the form
$'control1'= $_POST['control1'];
// load the other variables here

//Define the email attributes
$filename = $_FILES['file']['tmp_name'];
$fileFinal = $_FILES['file']['name'];
$to = "example@me.co.uk";
$subject = "A new email has arrived";

$random_hash = sha1(date('r', time()));

// Read in our file attachment
$attachment = file_get_contents($filename);
$encoded = base64_encode($attachment);
$attachment = chunk_split($encoded);

$headers = "From: example@me.co.uk\r\nReply-To: example@me.co.uk";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";


$message = "Hello World!!!";

if ($filename<>null){// ie - a file was uploaded
$message .= "
--PHP-mixed-$random_hash
Content-Type: application/zip; name=$fileFinal
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash";}

//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
}
?>



The problem i am having is that this code is very unfamiliar to me. Forget about the actual variables and form data for now, all i need to do is get the Hello world message to send.

if an attachment is selected it does come through but with no hello world message. (it does however come through with an additional ATTxxxx.txt file attached.

If i dont select a file to upload, the code send the email with no file attachment as expected but it does put the hello world message in the email body.

Any help would be MASSIVELY helpful, an explaination of the email code would be beneficial too i am familiar with all of the other PHP code.
 
Try changing

if ($filename<>null){ $message .= "

to

if ($filename<>null){$message = $message. "

 
i have always found it easiest to use phpmailer for all multipart messages etc.

but some thoughts

usually there are two crlfs between headers and body. so in these blocks
Code:
--PHP-mixed-$random_hash
Content-Type: application/zip; name=$fileFinal
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 
$attachment
and an extra crlf before $attachment
also enquote the file name \"$fileFinal\"

you also have not provided the headers for the message body.

Code:
$message = <<<MESSAGE
--PHP-mixed-{$random_hash}  
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit

Hello World

MESSAGE;

and lastly (and I'm not sure about this) I think it is normal to finish with a boundary as well

Code:
$message .= <<<BOUNDARY
--PHP-mixed-{$random_hash}  

BOUNDARY;

but the best advice I can give is to use phpmailer.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top