/    Sign up×
Community /Pin to ProfileBookmark

Pull email from database

I have a very basic form to email users on my site. What I would like to do is be able to select a username from a select box and email that user.

My html form is this

[code]
<table align=”center” width=”80%”><tr><td>
<b>Send Email</b><br><br>
<form action=”action.php” method=”post”>
E-mail:<br><select name=”email”>
<OPTION VALUE=””>Select User
<OPTION VALUE=”User Email Goes Here”>Username
</SELECT> <br>
Message:<br>
<textarea rows=”4″ name=”message” cols=”25″>
</textarea><br>
<input type=”submit” name=”submit” value=”Send Email”>
</form>
</td></tr></table>
[/code]

and the php file to send the email is this

[code]
<?
$email=$_POST[’email’];
$msg=$_POST[‘message’];
$msg=stripslashes($msg);
$headers = “From: [email protected]: $emailrn”;
PRINT “Email Sent”;
mail(“$email”, “Email Subject”, “$msg”, $headers);
?>
[/code]

I’d like to be able to show the username in the select box on the html form and have the users email address inserted into the php file for emailing.

The user’s info is stored in a MySql table called reguser and the email address field is Email and the user name field is Username. Is there a simple way of doing this?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@KevlynMay 05.2005 — Sure there is....
<i>
</i>&lt;table align="center" width="80%"&gt;&lt;tr&gt;&lt;td&gt;
&lt;b&gt;Send Email&lt;/b&gt;&lt;br&gt;&lt;br&gt;
&lt;form action="action.php" method="post"&gt;
E-mail:&lt;br&gt;&lt;select name="email"&gt;
&lt;OPTION VALUE=""&gt;Select User

// HERE'S WHERE THE NEW ADDITION STARTS
&lt;?php
mysql_pconnect( "host", "user", "pass" ); // Double check my syntax on that function.
mysql_select_database( "database name", $database );
$sql = "SELECT * FROM reguser ORDER BY Username ASC";
$result = mysql_query( $sql, $database );
while( $entry = mysql_fetch_assoc( $result ) )
echo '&lt;OPTION VALUE="' . $entry['Email'] . '"&gt;" . $entry['Username'];
?&gt;

// END THE NEW ADDITION.


&lt;/SELECT&gt; &lt;br&gt;
Message:&lt;br&gt;
&lt;textarea rows="4" name="message" cols="25"&gt;
&lt;/textarea&gt;&lt;br&gt;
&lt;input type="submit" name="submit" value="Send Email"&gt;
&lt;/form&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
Copy linkTweet thisAlerts:
@webdeveloperWLMay 25.2011 — #PLEASE SCROLL TO THE BOTTOM FOR SOLUTION


<?php

$host = "localhost:3306"; // Host name //assuming locally from server

$username = "db_user"; // Mysql username

$password = "db_password"; // Mysql password

$db_name = "db_name"; // Database name


// Connect to server and select databse.

$connection = mysql_connect("$host", "$username", "$password")or die("cannot connect");

$dbselect = mysql_select_db("$db_name")or die("cannot select DB");



// username and password sent from html form

$ttimmee = new DateTime("now", new DateTimeZone('Europe/London'));

$id_fault_report="NULL";

$fault_time = $ttimmee->format("h:i:s");

$fault_date = $ttimmee->format("Y-m-d");

$fault_description = $_POST["fault_description"];

$fk_room = $_
POST["fk_room"];

$fk_equipment = $_POST["fk_equipment"];

$fk_user = $_
POST["fk_user"];

$fk_fault_status = "reported";

$fk_technition = "NULL";



//To protect MySQL injection (more detail about MySQL injection)

$fault_time = stripslashes($fault_time);

$fault_time = mysql_real_escape_string($fault_time);

$fault_date = stripslashes($fault_date);

$fault_date = mysql_real_escape_string($fault_date);

$fault_description = stripslashes($fault_description);

$fault_description = mysql_real_escape_string($fault_description);

$fk_room = stripslashes($fk_room);

$fk_room = mysql_real_escape_string($fk_room);

$fk_equipment = stripslashes($fk_equipment);

$fk_equipment = mysql_real_escape_string($fk_equipment);

$fk_user = stripslashes($fk_user);

$fk_user = mysql_real_escape_string($fk_user);

$fk_fault_status = stripslashes($fk_fault_status);

$fk_fault_status = mysql_real_escape_string($fk_fault_status);

$fk_technition = stripslashes($fk_technition);

$fk_technition = mysql_real_escape_string($fk_technition);





//-------------------------------------------------------------------

//EMAIL BIT THIS WILL LOOP UNTILL THERE WILL BE NO MORE EMAIL IN THE TABLE CALLED BY SQL.

//YOU CAN SPECIFY WHICH USER YOU WANT TO SEND IT TO BY ADDING||| where fk_user = 'wiktor'";


$sql_e = "SELECT user_email FROM user_table";

$sql_results = mysql_query($sql_e, $connection) or die ("<br /> email ? ... sql ");


$from = '[email protected]';
$subject = "Fault in room $fk_room";
$msg = "
n THANK YOU $fk_user FOR SUBMITTING FAULT!
n SOON OUR TECHNITION WILL RECIEVE YOUR MESSAGE
n
n
n fault status: reported,
n fault in room: $fk_room
n fault time $fault_date , $fault_time
n broken equipment $fk_equipment id
n technition not yet assigned.
n
n
n
n Submited fault:
n $fault_description

";



//LOOPING IN THE TABLE USER_EMAIL FOR EMAILS

while ($row = mysql_fetch_array($sql_results))

{

//ASSIGNING EACH VALU TO A ROW ONCE DONE JUMPING TO NEXT ONE

$to =$row["user_email"];

print"<br />$to";

//SENDING EMAIL !

if (mail($to, $subject, $msg, $from)) {

echo("<p>Message successfully sent! ?</p>");

} else {

echo("<p? Message delivery failed...</p>");

}


}

mysql_free_result($sql_results);

mysql_close($connection);

//--------------------------------------------------------------------

?>
×

Success!

Help @wvmlt spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...