Jump to content

Send Email from Server with Attachment - NO ATTACHMENT


phpnewbieca

Recommended Posts

The objective of the attached script should do 4 things:

1. Upload a file to the server [function FileUp ()].

2. Display users input to the browser [function Sendmail_1()].

3. Send an email with an attachment [function Sendmail_2()], if a file has been attached.

4. Send an email with out attachment [function Sendmail_3()] .

 

The script will UPLOAD the file [function FileUp()], because it echos 'Success!'.

It will not DISPLAY THE USERS INPUT TO THE BROWSER [function Sendmail_1.

It will not attach the uploaded file to the email [function Sendmail_2].

 

Please tell me what I am doing wrong that prevents the script from displaying the users input to the browser and why it will not attached the uploaded 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
  FileUp();
  Sendmail_2();
  Sendmail_1();
}
else {  
  Sendmail_3();
  Sendmail_1();
}
// FUNCTIONS 
// Upload File
function FileUp(){ 
  global $Date, $Email, $Email_2, $Subject, $Text, $Attachedfile; 
  $target_path = "/xxxx/xxxxxxxx/public_html/upload";
  $target_path = $target_path . basename($_FILES['Attachedfile']['name']) ;
  $ok=1;
  //This is our size condition
  if ($Attachedfile > 2097152){
   echo "Your file is too large.<br>";
   $ok=0;
  }
  //This is our limit file type condition
  if ($Attachedfile == "text/php"){
   echo "No PHP files<br>";
   $ok=0;
  }
  if ($Attachedfile == "application/octet-stream"){
   echo "No EXE files<br>";
   $ok=0;
  }
  //Here we check that $ok was not set to 0 by an error
  if ($ok==0){
   Echo "Sorry your file was not uploaded";
  }
  //If everything is ok we try to upload it
  else{
   if(move_uploaded_file($_FILES['Attachedfile']['tmp_name'], $target_path)){
    echo "Success!";
   }
   else{
    echo "Sorry, there was a problem uploading your file.";
   }
  }
}
// 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</a>\n";
  echo "</td>\n";
  echo "</tr>\n";
  echo "</table>\n";
  echo "</body>\n";
  echo "</html>";
}
// FUNCTION
// Email with Attachment
function Sendmail_3(){
  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}$";
   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".">"; 
   //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}$";
  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

The attached script should do 4 things:

1. Upload a file to the server [function FileUp ()].

2. Display users input to the browser [function Sendmail_1()].

3. Send an email with an attachment [function Sendmail_2()], if a file has been attached.

4. Send an email with out attachment [function Sendmail_3()] .

 

The script will UPLOAD the file [function FileUp()], because it echos 'Success!'.

It will not DISPLAY THE USERS INPUT TO THE BROWSER [function Sendmail_1.

It will not attach the uploaded file to the email [function Sendmail_2].

 

Please tell me what I am doing wrong that prevents the script from displaying the users input to the browser and why it will not attached the uploaded 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
  FileUp();
  Sendmail_2();
  Sendmail_1();
}
else {  
  Sendmail_3();
  Sendmail_1();
}
// FUNCTIONS 
// Upload File
function FileUp(){ 
  global $Date, $Email, $Email_2, $Subject, $Text, $Attachedfile; 
  $target_path = "/xxxx/xxxxxxxx/public_html/upload";
  $target_path = $target_path . basename($_FILES['Attachedfile']['name']) ;
  $ok=1;
  //This is our size condition
  if ($Attachedfile > 2097152){
   echo "Your file is too large.<br>";
   $ok=0;
  }
  //This is our limit file type condition
  if ($Attachedfile == "text/php"){
   echo "No PHP files<br>";
   $ok=0;
  }
  if ($Attachedfile == "application/octet-stream"){
   echo "No EXE files<br>";
   $ok=0;
  }
  //Here we check that $ok was not set to 0 by an error
  if ($ok==0){
   Echo "Sorry your file was not uploaded";
  }
  //If everything is ok we try to upload it
  else{
   if(move_uploaded_file($_FILES['Attachedfile']['tmp_name'], $target_path)){
    echo "Success!";
   }
   else{
    echo "Sorry, there was a problem uploading your file.";
   }
  }
}
// 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</a>\n";
  echo "</td>\n";
  echo "</tr>\n";
  echo "</table>\n";
  echo "</body>\n";
  echo "</html>";
}
// FUNCTION
// Email with Attachment
function Sendmail_3(){
  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}$";
   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".">"; 
   //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}$";
  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

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.