Jump to content

Recommended Posts

Hi, I am very new to php, and cannot get this code to work.

    I do not know what I have done wrong and as I said I am not very good at php yet, the problem what I want the code to do is send an email along with attachment to direct to my email address! obviously it does not work can someone please help me. yours in desperation Moe.

 

 

 

 

 

<html>

<head>

<title> attachment sender </title>

</head>

<body>

<?php

 

 

 

$to_email = 'michael<moescreations2009@googlemail.com>';

 

$name      = $_POST['name'];

$telephone = $_POST['telephone'];

$message = $_POST['message'];

 

 

$fileatt      = $_FILES['fileatt']['tmp_name'];

$fileatt_type = $_FILES['fileatt']['type'];

$fileatt_name = $_FILES['fileatt']['name'];

 

 

 

if (is_uploaded_file($fileatt)) {

 

  $file = fopen($fileatt,'rb');

  $data = fread($file,filesize($fileatt));

  fclose($file);

 

 

  $semi_rand = md5(time());

  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

 

 

  $headers .= "\nMIME-Version: 1.0\n" .

              "Content-Type: multipart/mixed;\n" .

              " boundary=\"{$mime_boundary}\"";

 

 

  $message = "This is a multi-part message in MIME format.\n\n" .

            "--{$mime_boundary}\n" .

            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .

            "Content-Transfer-Encoding: 7bit\n\n" .

            $message . "\n\n";

 

    $data = chunk_split(base64_encode($data));

 

 

  $message .= "--{$mime_boundary}\n" .

              "Content-Type: {$fileatt_type};\n" .

              " name=\"{$fileatt_name}\"\n" .                     

              "Content-Transfer-Encoding: base64\n\n" .

              $data . "\n\n" .

              "--{$mime_boundary}--\n";

}

 

 

$ok = @mail($to,$name,$address,$message, $headers);

if ($ok) {

  echo "<p>Your details have been sent.</p>";

} else {

  echo "<p>Mail could not be sent. Sorry!</p>";

}

?>

</body>

</html>

 

Ok, I took the @ out, and ran it again and this is the resulting messages.

 

Notice: Undefined variable: headers in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 38

 

Notice: Undefined variable: to in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 62

 

Notice: Undefined variable: address in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 62

 

Warning: mail() [function.mail]: "sendmail_from" not set in php.ini or custom "From:" header missing in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 62

 

Mail could not be sent. Sorry!

 

 

could you let me know how to correct it so I can use it and then someone else may also use it and not have the same problems as me.

 

 

 

It says what the problem is right there:

 

Undefined variable: headers

 

You haven't defined the variable $headers. But you are passing it to the mail function. The way your script is set up, $headers is only defined when there is an upload. So you need to define an empty variable outside the 'if' statement like this:

 

$headers = '';

<?php

$to = "michaelpowens@hotmail.co.uk";

$name    = $_POST['name'];

$subject = $_POST['subject'];

$message = $_POST['message'];

$fileatt      = $_FILES['fileatt']['tmp_name'];

$fileatt_type = $_FILES['fileatt']['type'];

$fileatt_name = $_FILES['fileatt']['name'];

  if (is_uploaded_file($fileatt,'rb')

  $file = fopen($fileatt,'rb');

  $data = fread($file,filesize($fileatt));

  fclose($file);

$headers = ' ';

  $semi_rand = md5(time();

  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .

              "Content-Type: multipart/mixed;\n" .

              " boundary=\"{$mime_boundary}\"";

  $message = "This is a multi-part message in MIME format.\n\n" .

            "--{$mime_boundary}\n" .

            "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .

            "Content-Transfer-Encoding: 7bit\n\n" .

            $message . "\n\n";

  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message

  $message .= "--{$mime_boundary}\n" .

              "Content-Type: {$fileatt_type};\n" .

              " name=\"{$fileatt_name}\"\n" .

              //"Content-Disposition: attachment;\n" .

              //" filename=\"{$fileatt_name}\"\n" .

              "Content-Transfer-Encoding: base64\n\n" .

              $data . "\n\n" .

              "--{$mime_boundary}--\n";

 

{

 

$ok = mail($to, $name, $subject, $message );

if ($ok) }

  echo "<p>Thankyou your mail has been sent</p>";

else {

  echo "<p>Mail could not be sent. Sorry!</p>";

}

?>

 

 

ok so after alot of fiddling around I am now down to this and now I keep getting this error message_

 

Parse error: syntax error, unexpected T_VARIABLE in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 12

 

So I looked up t variables and although it says line 12 it does not mean it will be,(I know you guys know this but I would like you to know I am actually trying to fix this myself and not just hoping you will do it for me) So I have been through and tried to match everything up and make sure its right, but when I change one it moves on to another and so on and so forth and I am now gettin confused, obviously I am going to keep trying to sort it myself and I also removed the $header and $address and $phone after finding out I can actually only have five when I was trying to use 6 and this sorted half my problem. thankyou for all your help it is much appreciated. if you can keep helping me I would be very greatful.

thankyou, moe

Hi, sorry I did not realise I had made it difficult to read, hope this is better.

 

 

<?php
// Read POST request params into global vars
$to   ="michaelpowens@hotmail.co.uk";  
$name    = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = " ";
// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];


if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);



  // Generate a boundary string
  $semi_rand = md5(time();
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  // Add a multipart boundary above the plain message
  $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

// Send the message
$ok = mail($to, $name, $subject, $message, $headers);
if ($ok) {
  echo "<p>Thankyou your mail has been sent</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>

Hi, i was really excited this afternoon as i got it working and managed to send myself 5 email then this happened and now will no longer send!!!

 

Warning: mail() [function.mail]: SMTP server response: 501 Bad address syntax in E:\domains\m\moescreations.co.uk\user\htdocs\mail.php on line 61

 

If it is bad syntax why would it send a few times then stop, I have tried googling it and the general sound seems that some mime do not accept full length email addresses but the ways they mention of curing the problem I do not understand. ( and agin why work for a few goes then stop?)can you please explain what I need to do and how I go about curing the problem or help if possible.

 

 

 

<?php

error_reporting(E_ALL); 
ini_set('display_errors', true); 

// Read POST request params into global vars
$to_email = 'michael<moescreations2009@googlemail.com>';
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$to ='michael<moescreations2009@googlemail.com>'; 
$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  // Add a multipart boundary above the plain message
  $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

// Send the message
$ok = mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Your details have been sent, thankyou.</p>";
} else {
  echo "<p> sorry you details could not be sent, please retry, Thankyou. </p>";
}
?>

 

I think I have put tags in if not please do not edit for me, can you just explain exactly how to add the tags, thanks I need to learn if I am goiong to use this forum properly.

 

thanks moe.

Hi guys thanks for all your help it seems to be working again??

  I assume I must have sent too many emails in quick succession.

 

 

The code is there so if anyone wishes to use it please feel free.

    And for all who have helped thankyou very much before I found this site I was on the verge of giving up, thankyou very much guys.

 

 

 

 

 

 

 

No the code above is the basic working version, I need to try and make it as secure as I can to stop spammers (well make it difficult for them anyway). The code above does now work once I have done a version with security which I shall be doing now I will post that up as well, I hope people find a use for it.

Haku, ken, daniel, thankyou all very much for your help and patience.

 

Moe.

 

 

<?php
// Read POST request params into global vars
$to   ="michaelpowens@hotmail.co.uk";  
$name    = $_POST['name'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = " ";

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
if (is_uploaded_file($fileatt)); {

// Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);


  // Generate a boundary string
  $semi_rand = md5(time();
  $mime_boundary = "=Multipart_Boundary_x{$semi_rand}x";
  
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary=\"{$mime_boundary}\"";

  // Add a multipart boundary above the plain message
  $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

// Send the message
$ok = mail($to, $name, $subject, $message, $headers);
if ($ok) {
  echo "<p>Thankyou your mail has been sent</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>

   

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.