Jump to content

Email script


CloudBreaker

Recommended Posts

I'm trying to put together a script that will take the user emails associated with a specific project and list them out (pre-checked) and then simply send them off.  I have this listing part working.  I'm just having trouble getting my head wrapped around how to place the emails into to the mail() function.   My head is fried and I can't of how to tie that email value to the to: part of the mail function.  Here's what I have.

 

thanks, 

CB

!DOCTYPE html>

<?php
	$conn = mysqli_connect("localhost","root","","hsa_project_hub");
	$project_id = 215016;
	$sql = "SELECT * FROM user_project WHERE project_id='$project_id'";
	$result=mysqli_query($conn,$sql);

	



	
	if(isset($_POST['send_emails'])){
		
	$_Chk_box_value = $_POST['send_emails'];
	
	$to 		= implode(',',$_Chk_box_value);
	$subject	="Project Hub Activity";
	$txt		="Action require for project xxxx";
	$headers	="From: webmaster@domain.com";
	
	mail($to,$subject,$txt,$headers);
	}
?>
	<form name="email_form" action="checkbox.php" method="post">
<?php
		echo "<table border='2' align=center >
		<tr>
		<th>First Name</th>
		<th>last Name</th>
		<th>Discipline</th>
		<th>Email</th>
		</tr>";
		while($row = mysqli_fetch_array($result))
		{
		echo "<tr>";

		echo "<td> <input type='checkbox' name='check[][]' checked>" . $row['first_name'] . "</td>";
		echo "<td>" . $row['last_name'] . "</td>";
		echo "<td>" . $row['user_discipline'] . "</td>";
		echo "<td>" . $row['user_email'] . "</td>";
		echo "</tr>";
		}
		echo "</table>";
?>	

		<input name="send_emails" type="submit">
		
	</form>

<html>
	<body>
		
	
	
	
	</body>	
</html>
Link to comment
Share on other sites

<?php

if($_SERVER['REQUEST_METHOD'] == "POST")
{

	$to = $_POST['to'];
	$subject = $_POST['subject'];
	$message = $_POST['message'];
	$headers = "From: webmaster@domain.com";

	$to = htmlspecialchars($to, ENT_QUOTES);
	$subject = htmlspecialchars($subject, ENT_QUOTES);
	$message = htmlspecialchars($message, ENT_QUOTES);
	
	$to = htmlentities($to, ENT_QUOTES);
	$subject = htmlentities($subject, ENT_QUOTES);
	$message = htmlentities($message, ENT_QUOTES);
	
	mail($to, $subject, $message, $headers);

}

?>

<html>

<form action="" method="POST">

<h2>To Recipient:</h2><br>
<input type="text" name="to" placeholder="To Recipient" />
<br><h2>Subject:</h2><br>
<input type="text" name="subject" placeholder="Subject" /><br>
<h2>Message:</h2><br>
<textarea name="message"></textarea><br>

</form>

</html>

Also make sure that you are sending the email on a live web server, If you try to send a email from your wamp or xampp localhost server it will not work.

 

Hope this helps :)

Link to comment
Share on other sites

You should be validating that the $to is a valid email address, and that it only contains emails that it should. You don't want a malicious user setting that to anything they want, otherwise they can use your server to send spam.

 

Also, do not use htmlspecialchars() and htmlentities() at the same time.

Link to comment
Share on other sites

It looks like that code handles one email using a form.  I have a mysql query that takes the proper emails out of the database.  Once I get this small script figured out, I'll put it into the larger script which handles questions from a contractor and posts answers to those questions from an architect or engineer (in other words a request for information- RFI).  Any part of that RFI is edited by any of the users they will hit "modify" and the form will be submitted (I have all of this coded already) and team members which are pulled from the database (got that working) should receive an email alerting them that there has been some activity on that particular RFI.  So, there will be no input from a user.  I'm trying to figure out how to get the email address that are pulled from the database into the "To:" line.  See attached file. 

 

https://drive.google.com/file/d/0B06KJO0YEuzxWFliamQ3OFpfSFk/view?usp=sharing

 

thanks,

 

CB

Link to comment
Share on other sites

You should have an ID primary key in your database table. When you print out the emails, use the ID as the value. Then when processing the form, you can use a WHERE IN() clause in your query to select the rows that match the checked checkboxes.

 

Where in my script above would I utilize the the clause?

 

thanks

Edited by CloudBreaker
Link to comment
Share on other sites

I'm getting close (I think).  The problem is that I can't figure out why my imploded value $strMail with not echo.

<!DOCTYPE html>

<?php
	$conn = mysqli_connect("localhost","root","","hsa_project_hub");
	$project_id = 215016;
	
	$Project_name = "MD Anderson";
	
	if (isset($_POST['send_emails']))
	{
		if(isset($POST['user_email']))
		{
			$strMail = implode(";",$_POST['user_email']);
			
			$to = "$strMail";
			$subject = "action required on HeitkampSwift Project Hub";
			$message = "Action is require for $project_id";
			$headers = "From: webmaster@webmaster.com";
					
			mail($to, $subject, $message, $headers);
		
			
						
	
		}
		else
		{
			$strMail = "";
		}
		
			echo "Selected emails are: " . $strMail;

		}

		

?>
	
	
	<form name="email_form" action="checkbox2.php" method="post">
<?php

		$sql = "SELECT * FROM user_project WHERE project_id='$project_id'";
		$result=mysqli_query($conn,$sql);
		
		echo "<table border='2' align=center >
		<tr>
		<th>First Name</th>
		<th>last Name</th>
		<th>Discipline</th>
		<th>Email</th>
		</tr>";
		while($row = mysqli_fetch_array($result))
		{
		echo "<tr>";

		echo "<td> <input type='checkbox' name='mail[]' checked>" . $row['first_name'] . "</td>";
		echo "<td>" . $row['last_name'] . "</td>";
		echo "<td>" . $row['user_discipline'] . "</td>";
		echo "<td>" . $row['user_email'] . "</td>";
		echo "</tr>";
		}
		echo "</table>";
?>	

		<input name="send_emails" type="submit">
		
	</form>

<html>
	<body>
		
	
	
	
	</body>	
</html>
Link to comment
Share on other sites

Forget what I have up above...Still having some issues...

<!DOCTYPE html>

<?php
	$conn = mysqli_connect("localhost","root","","hsa_project_hub");
	$project_id = 215016;
	
	$Project_name = "MD Anderson";
	$sql = "SELECT * FROM user_project WHERE project_id='$project_id'";
	$result=mysqli_query($conn,$sql);
	$i=0;
?>

<?php

	if (isset($POST['send_emails']))
	{
		if (isset($_POST['mail']))
		{
			$strMail = implode(",",$_POST['mail']);
		}
		else
		{
			$strMail = "";
		}
		echo "The choose emails are: " . $strMail;
		exit();

		}
?>

		
<html>
	<body>

<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}

th, td {
    padding: 3px;
    text-align: left;
}
</style>	
		
		<form id="Email" name="Email" method="post" action="checkbox2.php" enctype="multipart/form-data"><br>
			<table align="center">
				<tr align="center">
					<th>First Name</th>
					<th>Last Name</th>
					<th>Discipline</th>
					<th>Email</th>
				</tr>
		
		
<?php
			
		//$strMail = implode(";",$row['user_email']);  
		
		
		
		$run = mysqli_query($conn, $sql);
		
		while($row=mysqli_fetch_array($run)){
				$firstName		=$row["first_name"];
				$lastName		=$row["last_name"];
				$discipline		=$row["user_discipline"];
				$email			=$row["user_email"];
				$i++;
				
				
		
			
?>			
		
	
		
		
					
			<tr align="center">
				<td> <input type='checkbox' name='mail[]' checked> <?php echo $firstName;?> </td>
				<td><?php echo $lastName;?></td>
				<td><?php echo $discipline;?></td>
				<td><?php echo $email;?></td>
			</tr>			
			
<?php } ?>		
			
		</table>
	</form>
<?php

	
	
	$run = mysqli_query($conn, $sql);
	while($row=mysqli_fetch_array($run)){
				$firstName		=$row["first_name"];
				$lastName		=$row["last_name"];
				$discipline		=$row["user_discipline"];
				$email			=$row["user_email"];
				$i++;
	
				$strMail 		= array($row['user_email']);
				
				
				
		
		
	}
	
	
		$to 		= "$strMail";
		$subject	= "action required on HeitkampSwift Project Hub";
		$message 	= "Action is require for $project_id";
		$headers 	= "From: webmaster@webmaster.com";
					
		mail($to, $subject, $message, $headers);
?>
			
		
	
						
					
		
		<input name="send_emails" type="submit">

		
	</body>	
</html>	
Link to comment
Share on other sites

Can you post the full schema for your user_project table? Run this query and post the output:

SHOW CREATE TABLE user_project

Thanks Guru

 

Column Type Null Default Comments id int(100) No  0    first_name varchar(100) No      last_name varchar(100) No      user_email varchar(100) No      projects_id int(30) No  0    project_name varchar(50) No      project_id int(11) No      user_discipline varchar(100) No      user_company varchar(100) No     

Edited by CloudBreaker
Link to comment
Share on other sites

Okay, so you do have an ID. You should be using it as the checkbox value.

<td> <input type='checkbox' name='mail[]' value="<?php echo $row['id']; ?>" checked> <?php echo $firstName;?> </td>
Then, you can do this:
if (!empty($_POST['mail'])) {
    $ids = array_map('intval', array_values($_POST['mail']));

    $sql = "SELECT user_email FROM user_project WHERE project_id='$project_id'" AND id IN(implode(',', $ids))";
    $result=mysqli_query($conn,$sql);

    $to = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $to[] = $row['user_email'];
    }

    mail(implode(', ', $to), $subject, $message, $headers)
}
This will select the emails chosen from the database, and also ensure that valid options were chosen.
Link to comment
Share on other sites

scootstah,

 

I think this works... I'm not getting any on the local host.  I'll have to test it on a web server.

<!DOCTYPE html>

<?php
	$conn = mysqli_connect("localhost","root","","hsa_project_hub");
	$project_id = 215016;
	
	$Project_name = "MD Anderson";
	$sql = "SELECT * FROM user_project WHERE project_id='$project_id'";
	$result=mysqli_query($conn,$sql);
	$i=0;
	
?>



		
<html>
	<body>

<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}

th, td {
    padding: 3px;
    text-align: left;
}
</style>	
		
		<form id="Email" name="Email" method="post" action="checkbox2.php" enctype="multipart/form-data"><br>
			<table align="center">
				<tr align="center">
					<th>First Name</th>
					<th>Last Name</th>
					<th>Discipline</th>
					<th>Email</th>
				</tr>
		
		
<?php
			
			
		$run = mysqli_query($conn, $sql);
		
		while($row=mysqli_fetch_array($run)){
				$firstName		=$row["first_name"];
				$lastName		=$row["last_name"];
				$discipline		=$row["user_discipline"];
				$email			=$row["user_email"];
				$i++;		
			
?>		
		
					
			<tr align="center">
				<td> <input type='checkbox' name='mail[]' value="<?php echo $row['id']; ?>" checked> <?php echo $firstName;?> </td>
				<td><?php echo $lastName;?></td>
				<td><?php echo $discipline;?></td>
				<td><?php echo $email;?></td>
			</tr>			
			
<?php } ?>		
			
				</table>
			</form>
	
	
<?php			
				if (!empty($_POST['mail'])) {
				$ids = array_map('intval', array_values($_POST['mail']));

				$sql = "SELECT FROM user_project WHERE project_id='$project_id' AND id IN(implode(',', $ids))";
				$result=mysqli_query($conn,$sql);
				$to = array();
				while ($row = mysqli_fetch_assoc($result)) {
				$to[] = $row['user_email'];
				}

				mail(implode(', ', $to), $subject, $message, $headers);
				}
				
				
?>	
	

			
		
	
						
					
		
		<input name="send_emails" type="submit">

		
	</body>	
</html>		
	
Edited by CloudBreaker
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.