Hello Guys, i am currently using my localserver dont have a hosting where i can test it, so i would like to know if this basic idea will work or not. Here is the code do you think if i enter the ID it will get the email and then send an email? Also can some one please give me a basic idea on how i would do it so it used the email in the To variable ?

<html>
<head>
</head>
<body>
<?php
$con = mysql_connect("localhost","t","123456");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("t",$con);

if(isset($_POST['ID'])){
$result = mysql_query("SELECT Email FROM t_table WHERE ID = '".$_POST['ID']."'LIMIT 1");
} 
$row = mysql_fetch_assoc($result);

//mail stuff here ?

mysql_close($con);

?>  
<form method="post" action="">
<font>ID</font> <input type="text" name="ID"/>
<input type="Submit" value="Submit" name="Submit">
</form>
</body>
</html>

Thank you

Recommended Answers

All 14 Replies

<html>
<head>
</head>
<body>
<?php
if(isset($_POST['ID'])){
$con = mysql_connect("localhost","t","123456");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("t",$con);

$result = mysql_query("SELECT Email FROM t_table WHERE ID = '".$_POST['ID']."' LIMIT 1");
$row = mysql_fetch_assoc($result);

if(strlen($row['Email']))
{
   $message = "You are getting an email from someone\n Wheeeee.";
    mail($row['Email'], 'You've got mail.  yayyyy', $message);
}

mysql_close($con);
} 

?>  
<form method="post" action="">
<font>ID</font> <input type="text" name="ID"/>
<input type="Submit" value="Submit" name="Submit">
</form>
</body>
</html>

I'd move the if on $_POST around the whole thing. Only reason to connect is if there's something to get.

Also I'd put an if around the mail function to make sure your query returned a result.

you can test mail locally. example: if you're on Windows, you'll have to install a program like stunnel http://www.google.com/search?q=php+mail+stunnel&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a

Hello

Thank you for your reply and help, much apericiated,

just would like to say would this work?

$to = $Email

Because i did some searching around and i was wondering if that would work or not.

but the only problem is i dont know how to nest it on the if statment, would you be kind enough to elabarate on that for me.

Its just i dont want to copy and paste and then say i know PHP, i would like to understand how you would do it.

Thank you

I'm not really sure I'm understanding your question, but I'll give it a shot :)


you have information coming from your database.


that's the email address.


the row returned from the database is held in an associative array.
eg. "Email" => "joe@someplace.com"

In your code that array is held in the variable $row

by specifying $row, we are letting PHP know that we would like 1 element out of that array where the key is "Email".

so $to = $row will set the $to variable to "joe@someplace.com.


Now that we have the email address stored in the $to variable, we can use it in the mail function.

NOT php code. mail(to email address, this is the subject line, your email message);

so mail is looking for 3 strings in this case.

mail($to,$subject,$message);

http://us2.php.net/manual/en/function.mail.php

here's another example taken from the php mail page of mail with a few more options.

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

Hello,

thank you for trying :)

I currently have this

td><a href=\"sendemail.php?Email\">Send email</a></td>

I then want the sendemail.php to have

$to = $Email
//the rest of the form

would that work?

you could use a GET method like you are doing. But I wouldnt suggest using email as one of your variables as hackers will eventually find your site and use it as spam.

You could use ID and send it to your initial page you put in your first post

<a href=\"sendemail.php?ID=123456\">Send email</a>

but that's also hackable. it's better if you make sure it's coming from your server which can be done by checking

$_SERVER['HTTP_REFERER'];

then in your sendemail.php you can get the value with $_GET or $_REQUEST

$id = $_GET['ID'];

Hey,

Check out this youtube video on simple email php script.

I guess you will learn something from the video.
Click Here

Hello Guys,

Thank you so much for your help,

The emails will be sent from admin side that i have, so hackers/spam wont get emails of individual peoples

so for example when i get the item, i click on Send it then gets the email and sends the email,

Can i do it like this

$result = mysql_query("SELECT email FROM t_table WHERE ID = '".$_POST['ID']."' LIMIT 1");

$email1 = $_GET['email'];
to = $email1

Would that work?

Thank you

$result = mysql_query("SELECT email FROM t_table WHERE ID = '".$_POST['ID']."' LIMIT 1");

$email1 = $_GET['email'];
to = $email1

Would that work?

Thank you

you can, however I dont think that it's doing what you want. Maybe it is.

$_GET is the response from a previous HTML request. http://www.w3schools.com/PHP/php_get.asp

to get the value from a mysql query, you'd want to get the query result. This page should help for that http://us3.php.net/manual/en/function.mysql-query.php

it looks to me from that snippet that you want to get a query result. not a variable from an HTML request.

By reading the first post.
The code below should work and meet the criteria asked in the first post. There are some checks in play that will not allow an empty email to send, also if an empty ID is passed the script will just step over it. Also the script will force the ID value to be an integer, since we are checkign an ID. If need be, you can use the $headers from kireol's post above, for plain text they aren't really needed, it depends on the quality of email you want.

<html>
<head>
</head>
<body>
	<?php
	$saidID = isset($_POST['ID']) ? (INT)$_POST['ID'] : 0;
	if($saidID){
		$con = mysql_connect("localhost","t","123456");
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
	
		mysql_select_db("t",$con);
	
		$result = mysql_query("SELECT Email FROM t_table WHERE ID = $saidID LIMIT 1");
		$row = mysql_fetch_assoc($result);
	
		if($row['Email'])
		{
			$to = $row['Email'];
			$subject = 'Test Email';
			$message = "Testing";
			
			if (mail($to, $subject, $message)) {
				echo 'Email was sent';
			} else {
				echo 'Email Failed';
			}
		}
		mysql_close($con);
	}
	?>  
	<form method="post" action="">
	<font>ID</font> <input type="text" name="ID"/>
	<input type="Submit" value="Submit" name="Submit">
	</form>
</body>
</html>

After reading the rest of the post:
If you want to use a sendmail.php file:
in sendmail.php

<?php
	$saidID = isset($_POST['ID']) ? (INT)$_POST['ID'] : 0;
	if($saidID){
		$con = mysql_connect("localhost","t","123456");
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
	
		mysql_select_db("t",$con);
	
		$result = mysql_query("SELECT Email FROM t_table WHERE ID = $saidID LIMIT 1");
		$row = mysql_fetch_assoc($result);
	
		if($row['Email'])
		{
			$to = $row['Email'];
			$subject = 'Test Email';
			$message = "Testing";
			
			if (mail($to, $subject, $message)) {
				echo 'Email was sent';
			} else {
				echo 'Email Failed';
			}
		}
		mysql_close($con);
	}
	?>

index.html?

<html>
<head>
</head>
<body>
	<form method="post" action="./sendmail.php">
	<font>ID</font> <input type="text" name="ID"/>
	<input type="Submit" value="Submit" name="Submit">
	</form>
</body>
</html>

use a form with post, get is too easy for users to manipulate. Post is easy to change as well but most noobs can't mess with it.
if you insist on using get change the fetch for $saidID to use $_GET instead of $_POST.

Hello Guys,

Iv just tried what all you did for me much apericiated, but none of it works, i made sure that every thing is correct and please note that i have got this now on my hosting which definatley works with my normal contact form. When i enter the ID it says sent but when i check my email to see if it has worked, and i dont receive anything

The thing is.

i have a list of all peoples email address that i have registered next to this i have a link which is so far

<td><a href=\"sendemail.php?ID=$ID\">Send</a></td>

What i then want to do is, click on send which directs to sendemail.php and reads whats in that which english it says

"Connect to database, select email from table where id = the one in the link and then send the email"

Now the question is how do i convert that to PHP, would some one be kind enough to share some knowledge.

Thank you

What email are you testing with? Some email, will put the email in spam if the headers do not meet a criteria. Which method are you using?

Your post above is hard to understand, at least for me. I am not sure what you asking.

is your web server that you are running the script on have an SMTP server for mail? I addressed this in my first response in this thread

Hello,
Sorry for confusing you lol, what i want is that i have section in my table which has a row called Email which stores all the emails that i enter, i then click on veiw.php which outputs all data and emails and then i have for example

ID | Address | Email | To

1 1Address e@e.com Send

The send link is set up like this

<td><a href=\"sendemail.php?Email=$Email\">Send</a></td>

which outputs the link in the address bar as

http://www.site.com/sendemail?Email=e@e.com

I then want an email sent to e@e.com

to this i have been playing around with the codes that you have posted but unfortatnely none of it worked but in english i want "get email from link and send them an email"

after searching around i noticed that if i have it like this and use your code it may work can you confirm this as well.

<td><a href=\"sendemail.php?Email=$Email&&ID=$ID\">Send</a></td>


Thank you for helping me out on this issue, much apericiated.

Take care
Have a good day
:)

$target_path = $target_path;
$strFrom ="$email";
$strTo= $_REQUEST['txtemailto'];

Would that work? can some one please help how to use that now.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.