Jump to content

Send Contents of Array Via Email


phpnewbieca

Recommended Posts

I am trying to send the contents of an array $lines_array via email.

 

I receive the following error message:

 

Parse error: syntax error, unexpected '=' in /home/xgasq39z/public_html/valid_contact.php on line 29

 

I cannot find an error on line 29. So, I am assuming the error is on line 30.

 

I cannot determine what I am doing wrong.

 

Help, Please!

 


<?php
$Date = date("D  d M Y - H:i:s ");
$confirmation_number = $_POST['confirmation_number'];
$from = "[email protected]";
// validation expected data exists
  if(!isset($_POST['confirmation_number'])) {
  died('We are sorry, but there appears to be a problem with the form your submitted.');
  }
// Call Function
Send_Contact();
//FUNCTION
function Send_Contact() {
  global $Date, $confirmation_number, $from;
  $MyFile = "data.txt";

  $fh = fopen($MyFile,"r");
  rewind($fh);
  if(!$fh) {
  die("couldn't open file <i>$MyFile</i>");
  }
  else {
  while(!feof($MyFile)) { 
    $lines_array = file($MyFile);
  }
  fclose($fh);
  if(in_array("Confirmation Number: $confirmation_number",$lines_array,true)){
    $subject = " Valid - Contact Us";
    $email_message = " Contact Information has been validated.\n";
    $email_message.= " Contact requestor within 48 hours.\n";
[b]*** 29  [/b]  $email_message.= " Confirmation Number: $confirmation_number\n";
[b]*** 30  [/b]  $email_message.= " $lines_array";
    // create email headers
    $headers = 'From: '.$from."\r\n".
    'Reply-To: '.$from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    $formsent = mail($from, $subject, $email_message, $headers);
    if ($formsent) {
      echo $formsent;
    }
    else {
      echo " "email was not sent. The CONFIRMATION NUMBER you enter is not in or records.";
      die;
    }
  }
}
?>

[/Code]

 

Link to comment
https://forums.phpfreaks.com/topic/181542-send-contents-of-array-via-email/
Share on other sites

Where would I put this code (I am a newbie to php)?

Will this line of code load the file as a string?

$lines = file(myfile.txt);

If yes

Would it print the contents of the string by using this line of code?

$email_message.= " $lines";

Where would I put this code (I am a newbie to php)?

Just to clarify, in this line...

 

> $email_message.= " $lines_array";

 

...you are attempting to append the array $lines_array onto the string $email_message.

 

I suggest you use the implode command to create a string from your array, and append that to your string.

What if I do this:

$file = file_get_content($MyFile);

email_message.= "$file";

 

file_get_content() isn't a function (you're thinking about file_get_contents()) so you'd get a syntax error.

 

Other than that, I can't see anything else wrong with what you're doing.

The code below allows me to send  the contents of a txt file via email using the mail() function  :D :D :D

 

<?php
// Processess Forml
$Date = date("D  d M Y - H:i:s ");
$confirmation_number = $_POST['confirmation_number']; 
$MyFile = "myfile.txt";
$from = "[email protected]";
// validation expected data exists
  if(!isset($_POST['confirmation_number'])) {
   died('We are sorry, but there appears to be a problem with the form your submitted.');		
  }
// Call Function
Appointment_1();
Appointment_2();

//FUNCTION 
function  Appointment_1() {
  global $Date, $confirmation_number, $from, $MyFile;
  $fh = fopen($MyFile,"r");
  rewind($fh);
  if(!$fh) {
   die("couldn't open file <i>$MyFile</i>");
  }
  else {
   $file = file_get_contents("$MyFile");
  }
  fclose($fh);
  if(strstr($file,$confirmation_number)) { 
   $subject = "Valid - Appointment Request";
   $email_message = "Appointment Request has been validated.\n";
   $email_message.= "Contact requestor within 48 hours.\n";
   $email_message.= "Confirmation Number: $confirmation_number\n";
   $email_message.= " $file";
   // create email headers
   $headers = 'From: '.$from."\r\n".
   'Reply-To: '.$from."\r\n" .
   'X-Mailer: PHP/' . phpversion();
   $formsent = mail($from, $subject, $email_message, $headers);
   if ($formsent) {
    echo $formsent;
   }
   else {
    echo "email was not sent. The CONFIRMATION NUMBER you enter is not in or records.";
    die;
   }
  }
}
function  Appointment_2() {
  $Send_to = '[email protected]'; 
  $From = '[email protected]'; 
  $Subject = 'Valid - Appointment Request';
  //supply the text and html versions of your email message 
  $text = '';
  $html = ''; 
  //provide path to the file to be attached 
  $file = '/home/xgasq39z/public_html/appointment.txt';
  //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".">"; 
  //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"
  . "{$text}\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($Send_To, $Subject, $body, $headers ); 
  echo " $mail_sent";
  if ($mail_sent){
   echo "Mail sent";
  }
}
?>

 

Thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.