Jump to content

Grab a line as a subject


webstar

Recommended Posts

How can i Make this code below grab the 3rd line of the file I am feeding through it and make it the subject line of the email?  I'm kinda php dumb and this is likely really simple.

#!/usr/bin/php
<?php


# E-mail recipient(s).  Separate each address with a comma and a space.
$emailrecip = "REMOVED";

# E-mail subject line.
$emailsubject = "REMOVED";

# E-mail From name.
$emailfromname = "REMOVED";

# E-mail From address.
$emailfromaddr = "REMOVED";



$fp = fopen('php://stdin','r');
  $data = "";
  while(!feof($fp)) {
      $data .= fgets($fp,4096);
  }
  fclose($fp);




	$timestamp = "REMOVED";
$timestamp .= date("D, j M Y G:i:s O  ");
$timestamp .= "\n";
$timestamp .= "Emailed to you at: %EMAILSTAMP%";
$timestamp .= "\n";


# Send the e-mail.
			$subject = $emailsubject;
	        $recipient = $emailrecip;
	        $body_of_email = $timestamp;
			$body_of_email .= trim($data);

	        $header = 'From: "' . $emailfromname . '" <' . $emailfromaddr . '>';
	        $header .= "\n"; 
	        $header .= "X-Priority: 1";
	        $header .= "\n";
		
 	        mail ($recipient, $subject, $body_of_email, $header);
	
	        echo ("E-mail sent.");
	
?>
Link to comment
https://forums.phpfreaks.com/topic/296977-grab-a-line-as-a-subject/
Share on other sites

You could try turning $data into an array of lines and accessing the third entry

 

$dataByLines = explode("\r\n", $data);
print_r($dataByLines[2]);

 

That's assuming a windows linebreak format...and that explode addresses special char's which I can't remember off the top of my head if it does or not.

Use fgets, each time it is called it returns the next line. Everytime you get a new line increment a counter, when the counter hits 3, this will be the 3rd line then set the $subject

$data = '';
$i = 0;

while (false !== ($line = fgets(STDIN)))
{
    $data .= $line;

    /// if we are on the 3rd line, set the subject
    if(++$i == 3)
       $subject = $line;
}

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.