webstar Posted June 23, 2015 Share Posted June 23, 2015 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."); ?> Quote Link to comment https://forums.phpfreaks.com/topic/296977-grab-a-line-as-a-subject/ Share on other sites More sharing options...
Muddy_Funster Posted June 23, 2015 Share Posted June 23, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296977-grab-a-line-as-a-subject/#findComment-1514676 Share on other sites More sharing options...
Ch0cu3r Posted June 23, 2015 Share Posted June 23, 2015 (edited) 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; } Edited June 23, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/296977-grab-a-line-as-a-subject/#findComment-1514679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.