Question

I am asking for help, after hours of trying to figure this out myself.

I have the following code, which I would like to email results to email.

Here is my code:

$emailme = "myemail@somewhere.com";

$subject = "Randomly selected from array";
$headers = "From: $emailme\n";

$message = "Here is the Randomly selected from array.\n
Random text: $r_array";

$r_array=file('file.txt'); 
shuffle($r_array); 
$output = "<p><center><b>The Randomly Selected Text is:</b></p><b>" .  
$r_array[0] . "All done, echoing results.";

mail($emailme,$subject,$message,$headers);

So far, I am able to echo the results to screen, but am unable to send results via email.

Was it helpful?

Solution

Sending an email is pretty straight forward, example:

<?php
$r_array=file('file.txt');   
shuffle($r_array);   

$to = "recipient@example.com";
$subject = "Random Selected Text";
$body = "<p><center><b>The Randomly Selected Text is:</b></p><b>" . $r_array[0] . "All done, echoing results.";
if (mail($to, $subject, $body)) {
  echo("<p>Message successfully sent!</p>");
} else {
   echo("<p>Message delivery failed...</p>");
 }
?>

Something like this should work, if not, the mail server might not be properly configured on the web server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top