<!DOCTYPE html>
<html lang="hu">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="reg.css"/>
</head>
<body>
<h1>Send a mail to everybody...</h1>
<?php
require_once('constants.php');

if(isset($_POST['submit'])){
 $showTheForm=false;
 $subject=$_POST['subject'];
 $message=$_POST['message'];
 $from=$_POST['sender'];
 
  if(!empty($subject) && !empty($message) && !empty($from)){
  	$dbc=mysqli_connect(HOST, USER, PASSWD, DB) or die('Error connection...');
	$query="SELECT * FROM participants";
	$result=mysqli_query($dbc, $query) or die('Error...');
 	 while($row=mysqli_fetch_array($result)){
 	 	$to=$row['email'];
 	 	mail($to, $subject, $message, 'From: '.$from);
 	 	echo '<h3>Mail has been sent to the following address: '.$row['email'].'</h3>';
    }
	mysqli_close($dbc); 
  }else{echo '<h3>Each field has to be filled!</h2>'; $showTheForm=true;}

}else{$showTheForm=true;}


if($showTheForm)
{
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>Sender: <input type="text" name="sender" value="<?php if(!empty($sender)){echo $sender;} ?>"/></p>
<p>Subject: <input type="text" name="subject" value="<?php if(!empty($subject)){echo $subject;} ?>"/></p>
<p>Message: <textarea cols="40" rows="10" name="message"><?php if(!empty($message)){echo $message;} ?></textarea></p>
<p><input type="submit" name="submit" value="Send"/></p>
</form>
<?php
}
?>
<h2><a href="main.html">&lt;&lt;Back to the main page</a></h2>
</body>
</html>