Jump to content

mail () function will not send attachment


phpnewbieca

Recommended Posts

 

 

The function (Sendmail_2 () - below) should send an email with an attachment:

The variables $Email $Email_2 $Subject $Attachedfile is the information from the form.

 

No Error Message. No Attachment

 

Please tell me what I am doing wrong that prevents the script from attaching the file to the email.

 

<?php
// processess Sendmailform.html
error_reporting(E_ALL);
// Define variables  
$Date = date("F d, Y");
// Get Data From Form
// validate expected data exists
if(isset($_POST['Email'])){
  $Email = $_POST['Email'];
}
if(isset($_POST['Email_2'])){
  $Email_2 = $_POST['Email_2'];
}
if(isset($_POST['Subject'])){
  $Subject = $_POST['Subject'];
}
if(isset($_POST['Text'])){
  $Text = $_POST['Text'];
}
if(isset($_POST['Attachedfile'])){
  $Attachedfile = $_POST['Attachedfile'];
} 
// Call Function
if(!isset($_POST['Attachedfile'])) {
  // Call Function
  Sendmail_2();
  Sendmail_1();
}
// FUNCTION
function Sendmail_1() { 
  global $Date, $Email, $Email_2, $Subject, $Text, $Attachedfile; 
  // Print to Browser
  echo "<html>\n";
  echo "<head>\n";
  echo "<TITLE>Send Mail</title>\n";
  echo "</head>\n";
  echo "<BODY BGCOLOR='blue' TEXT='white'>\n"; 
  echo "<table align='center'>\n";
  echo "<tr>\n";
  echo "<td>\n";
  echo "<br /><br /><br /><br /><br /><br /><br /><br />\n";
  echo "</td>\n";
  echo "</tr>\n";
  echo "</table>\n";
  echo "<br />\n";
  echo "<table width='600' align='center' border='15'>\n";  
  echo "<tr>\n";
  echo "<th align='center' valign='center' colspan='1' width='600'>\n";
  echo "<font size='+2'>\n";
  echo "~ ~ ~ This is Email you sent ~ ~ ~\n";
  echo "</font>\n";
  echo "</th>\n";
  echo "</tr>\n";
  echo "</table>\n"; 
  echo "<table width='600' align='center' border='15'>\n";  
  echo "<tr>\n";
  echo "<th align='right' valign='center' colspan='1' width='250'>\n";
  echo "\t To: $Email\n";
  echo "<br />\n";
  echo "\t (Recipient's Email Address)\n";
  echo "</th>\n";
  echo "</tr>\n"; 
  echo "<tr>\n";
  echo "<th align='right' valign='center' colspan='1' width='250'>\n";
  echo "\t From: $Email_2\n";
  echo "<br />\n";
  echo "\t Sender's Email Address)\n";
  echo "</th>\n";
  echo "</tr>\n"; 
  echo "<tr>\n";
  echo "<th align='right' valign='center' colspan='1'  width='250'>\n";
  echo "\t Subject: $Subject\n";
  echo "</th>\n";
  echo "</tr>\n";   
  echo "<tr>\n";
  echo "<th align='right' valign='center' colspan='1'  width='250'>\n";
  echo "\t Message: $Text\n";
  echo "</th>\n";
  echo "</tr>\n"; 
  if(isset($_POST['Attachedfile'])) {
   echo "<tr>\n";
   echo "<th align='right' valign='center' colspan='1'  width='250'>\n";
   echo "\t Attached File: " .basename( $_FILES['Attachedfile']['name']). "\n";
   echo "<br />\n";
   echo "</th>\n";
   echo "</tr>\n";
  }
  echo "</table>\n";
  echo "<table width='600' Align='center'>\n";
  echo " <tr>\n";
  echo "<td width='600' Align='left'>\n";
  echo "<br /><br />\n";
  echo "Please visit us again <a href=\"http://www.horace-franklin-jr.com\">www.horace-franklin-jr.com
<http://www.horace-franklin-jr.com></a>\n";
  echo "</td>\n";
  echo "</tr>\n";
  echo "</table>\n";
  echo "</body>\n";
  echo "</html>";
}
//////////////////////////// 
// FUNCTION               //
// Email with Attachment  //
////////////////////////////
function Sendmail_2(){
  global $Email, $Email_2, $Subject, $Text, $Attachedfile;
  $Attachedfile =  basename( $_FILES['Uploadedfile']['name']);
  if($Attachedfile == basename( $_FILES['Attachedfile']['name'])){
   // FUNCTION
   function died(){
    global $Email, $Email_2, $Subject, $Text, $Attachedfile;
    // your error code can go here
    echo "These errors appear below.\n";
    echo "<br /><br />\n";
    echo "$error_message\n";
    echo "<br /><br />\n";
    echo "Please go back and fix these errors.\n";
    echo "<br /><br />\n";
    die();
   }
   $error_message = "";
   $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
<mailto:+@[A-Z0-9.-]+\.[A-Z]{2,4}$>";
   if(!eregi($email_exp,$Email)){
    $error_message.= 'The Email Address you entered does not appear to be valid.<br />';
   }
   if(!eregi($email_exp,$Email_2)){
    $error_message.= 'The Email Address you entered does not appear to be valid.<br />';
   }
   if(strlen($Text) < 2){
    $error_message.= 'The Comments you entered do not appear to be valid.<br />';
   }
   if(strlen($error_message) > 0){
    die($error_message);
   }
   //FUNCTION
   function clean_string($string){
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
   }
   $SendTo = "$Email";
   $From = "$Email_2";
   $Subject = "$Subject";
   //supply the text and html versions of your email message 
   $Text = "$Text\n";
   $html = "$Text\n";
   //provide path to the file to be attached 
   $file = "/xxxx/xxxxxxxx/public_html/upload/$Attachedfile";
   //create a boundary string. It must be unique 
   //so we use the MD5 algorithm to generate a random hash 
   $random_hash = md5(date('r',time())); 
   //define the headers we want passed. Note that they are separated with \r\n 
   $headers = "From: "."<"."$From".">"."\n";
   $headers.= "Reply-To: "."<"."$From".">"; 
   $headers.= "CC: "."<"."horace@horace-franklin-jr.com
<mailto:horace@horace-franklin-jr.com>".">"; 
   //add boundary string and mime type specification
   $headers.= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-{$random_hash}  \""; 
   //read the atachment file contents into a string, 
   //encode it with MIME base64, 
   //and split it into smaller chunks 
   $attachment = chunk_split(base64_encode(file_get_contents($file))); 
   //define the body of the message. 
   $body = "--PHP-mixed-{$random_hash}\r\n".
   "Content-Type: multipart/alternative;
   boundary=\"PHP-alt-{$random_hash}\"\r\n".
   "--PHP-alt-{$random_hash}\r\n"."Content-Type: text/plain;
   charset=\"iso-8859-1\"\r\n"."Content-Transfer-Encoding: 7bit\r\n\r\n".
   "{$message}\r\n\r\n"."--PHP-alt-{$random_hash}\r\n\r\n".
   "Content-Type: text/html; charset=\"iso-8859-1\"\r\n".
   "Content-Transfer-Encoding: 7bit\r\n\r\n". "{$html}\r\n\r\n".
   "--PHP-alt-{$random_hash}--\r\n\r\n"."--PHP-mixed-{$random_hash}\r\n".
   "Content-Type: ".mime_content_type($file)."; name=\"".
   basename($file)."\"\r\n"."Content-Transfer-Encoding: base64\r\n".
   "Content-Disposition: attachment\r\n\r\n"."{$attachment}\r\n".
   "--PHP-mixed-{$random_hash}--\r\n\r\n"; 
   //send the email 
   $mail_sent = @mail($SendTo, $Subject, $body, $headers); 
   if($mail_sent){
    echo $mail_sent;
   }
   else{
    echo "Email was not sent\n";
   }
  }
  else {
   echo "Attachement not sent\n" 
  }
}
// FUNCTION
// Email with Out Attachment
function Sendmail_3(){
  global $Email, $Email_2, $Subject, $Text;
  // FUNCTION
  function died(){
   global $Email, $Email_2, $Subject, $Text;
   // your error code can go here
   echo "These errors appear below.\n";
   echo "<br /><br />\n";
   echo "$error_message\n";
   echo "<br /><br />\n";
   echo "Please go back and fix these errors.\n";
   echo "<br /><br />\n";
   die();
  }
  $error_message = "";
  $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$
<mailto:+@[A-Z0-9.-]+\.[A-Z]{2,4}$>";
  if(!eregi($email_exp,$Email)){
   $error_message.= 'The Email Address you entered does not appear to be valid.<br />';
  }
  if(!eregi($email_exp,$Email_2)){
   $error_message.= 'The Email Address you entered does not appear to be valid.<br />';
  }
  if(strlen($Text) < 2){
   $error_message.= 'The Comments you entered do not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0){
   die($error_message);
  }
  //FUNCTION
  function clean_string($string){
   $bad = array("content-type","bcc:","to:","cc:","href");
   return str_replace($bad,"",$string);
  }
  $SendTo = "$Email\n";
  $From = "$Email_2\n";
  $Subject = "$Subject\n";
  $email_message = "$Text\n";
  // create email headers
  $headers = 'From: '.$Email_2."\r\n".
  'Reply-To: '.$Email_2."\r\n" .
  'X-Mailer: PHP/' . phpversion();
  //send the email 
  $mail_sent = @mail($SendTo, $Subject, $email_message, $headers); 
  if($mail_sent){
   echo $mail_sent;
  }
  else{
   echo "Email was not sent\n";
  }
}
?>

Link to comment
Share on other sites

It would really help if you could give us the smallest piece of code that illustrated the problem. What you appear to be doing is saying "here is my program, please debug it for me." However, if you extracted just the part that was going wrong, you would firstly probably find out what was wrong, and secondly, make it quicker for us to help you.

 

There are many examples of mail() on the internet that illustrate how attachments work. I think I saw one today in the comments under the mail() entry in the php functions manual  online.

 

php manual mail() function

 

Link to comment
Share on other sites

try this

to sent mail with attachment.....

<?php
#Send mail attaching a file along with it...
$fileatt = "address.pdf"; // Path to the file
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "mypdffile.pdf"; // Filename that will be used for the file as the attachment

$email_from = "sales@mysite.com"; // Who the email is from
$email_subject = "Your attached file"; // The Subject of the email
$email_message = "Thanks for visiting mysite.com! Here is your free file.<br>";
$email_message .= "Thanks for visiting.<br>"; // Message that the email has in it
$email_to = "dr.virus.india@gmail.com"; 
//$email_to = $_POST['email']; // Who the email is to

$headers = "From: ".$email_from;

$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}\"";

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

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

$email_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($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo "<font face=verdana size=2><center>You file has been sent<br> to the email address you specified.<br>
Make sure to check your junk mail!<br>
Click <a href=\"#\" onclick=\"history.back();\">here</a> to return to mysite.com.</center>";

} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
?> 

Link to comment
Share on other sites

I think the problem is with this line:

 

if(isset($_POST['Attachedfile'])){

  $Attachedfile = $_POST['Attachedfile'];

}

 

I think if I change it to:

if(isset($_FILES['Attachedfile']['name'])) {

$Attachedfile = $_FILES['Attachedfile'];

}

 

It will allow the name of the file to be stored in the variable $Attachedfile.  Then I can use that variable elsewhere in the script.

 

I will try that and if it does not work I will give your script a try.

 

Thanks for your sugesstion!

- name contains the original path of the user uploaded file.

Link to comment
Share on other sites

I think the problem is with this line:

 

if(isset($_POST['Attachedfile'])){

  $Attachedfile = $_POST['Attachedfile'];

}

 

I think if I change it to:

if(isset($_FILES['Attachedfile']['name'])) {

$Attachedfile = $_FILES['Attachedfile']['name'];

}

 

It will allow the name of the file to be stored in the variable $Attachedfile.  Then I can use that variable elsewhere in the script.

 

I will try that and if it does not work I will give your script a try.

 

Thanks for your sugesstion!

- name contains the original path of the user uploaded file.

Link to comment
Share on other sites

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.