Sending random email via PHP

Hello Everyone,

I’ve already put together a script (basically using the mail function in PHP) to send out emails. That was step one of this project. I need help with the next two steps:

First, I want to have a list of email addresses that it would send the messages to. I’m guessing this would require a list of email addresses, and a loop function?

Second, I have four different emails - where the content is slightly different. I want to send out any one of those four emails randomly the users.

I’m still very early in my learning phase of PHP but this is a project at this time and would greatly appreciate any help that you can provide. My simple code to actually send the email is below:


set_time_limit (300);

$Name = $_POST[‘name’]; //senders name
$email = $_POST[‘email’]; //senders e-mail adress
$recipient = $_POST[‘recipient’]; //recipient’s email address
$mail_body = $_POST[‘mail_body’]; //mail body
$subject = $_POST[‘subject’]; //email subject
$header = "From: “. $Name . " <” . $email . “>\r\n”; //optional headerfields

// $emailList
// foreach ($emailList as $thisEmail) {
// mail($thisEmail);
mail($recipient, $subject, $mail_body, $header); //mail command

echo "Email sent to $recipient on behalf of $Name


you should invest in a php book if i read this.

  1. place your code in a function
  2. make a array with emails
  3. make a while loop which takes one emailadress at a time and calls your function with that emailadress.


this is not ment to send loads of emails.


Thank you for your response. Obviously I still have a lot to learn. I thank you for your direction. I have also gone out and got a book for this. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service