Forum Moderators: coopster

Message Too Old, No Replies

mysql send email

         

loki

11:47 am on Jan 6, 2012 (gmt 0)

10+ Year Member



i have been trying to edit scripts and make diffent scripts

to use a dropdown box from a database so i pick the username and when i click on send it will send it to the user i have picked from the drop down box can anyone help me this has been doing my hed in for weeks

<?php

include "../connect.php"; //connection string
include("../include/session.php");

print "<link rel='stylesheet' href='style.css' type='text/css'>";

print "<table class='maintables'>";

print "<tr class='headline'><td>Post a message</td></tr>";

print "<tr class='maintables'><td>";

// Write out our query.
$query = "SELECT username,email FROM users";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());


$dropdown = "<select name='username'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['username']}'>{$row['username']}</option>";
}
$dropdown .= "\r\n</select>";

if(isset($_POST['submit']))

{

$name=$session->username;

$yourpost=$_POST['yourpost'];

$subject=$_POST['subject'];

$to=$_POST['username'];

$tom1=$_POST['email'];



$to666 = '$tom1';
//define the subject of the email

//define the message to be sent. Each line should be separated with \n
$message666 = "new message from $name\nMessage:\n$yourpost\n\nwww.#*$!#*$!xx";
//define the headers we want passed. Note that they are separated with \r\n
$headers666 = "From: office@#*$!#*$!x\r\nReply-To: office@#*$!#*$!x";
//send the email
$mail_sent = @mail( $to666, $subject, $message666, $headers666 );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent\n" : "Mail failed";


if(strlen($name)<1)

{

print "You did not type in a name."; //no name entered

}

else if(strlen($yourpost)<1)

{

print "You did not type in a post."; //no post entered

}

else if(strlen($subject)<1)

{

print "You did not enter a subject."; //no subject entered

}

else

{

$thedate=date("U"); //get unix timestamp

$displaytime=date("F j, Y, g:i a");

//we now strip HTML injections

$subject=strip_tags($subject);

$name=strip_tags($name);

$yourpost=strip_tags($yourpost);
$to=strip_tags($to);



print "Message posted, go back to <A href='forum.php'>Forum</a>.";

}



}

else

{

print "<form action='mail.php' method='post'>";

print "Your name:<br>";

print "$session->username<br>";

print "User to send to:<br>";

print "$dropdown<br>";

print "Subject:<br>";

print "<input type='text' name='subject' size='20'><br>";

print "Your message:<br>";

print "<textarea name='yourpost' rows='5' cols='40'></textarea><br>";

print "<input type='submit' name='submit' value='submit'></form>";



}

print "</td></tr></table>";



?>


all i want it to do is select the name i have picked and the send it to the e-mail address when i click submit

rocknbil

5:08 pm on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard loki.

First, you have an invalid email address in $to666. It needs to be a full email address, tom1@example.com.

$dropdown .= "\r\n<option value='{$row['email']}'>{$row['username']}</option>";

Also note the "to" is the first parameter of mail, and single quoted $tom1 will not interpolate $POST_['email']. Also you're not doing anything to validate either email address, so it's a point of errors and injection.

Second, your conditions are out of order. It should be roughly like

if (field not entered) { print error. }
else if (field2 not entered) { print error. }
else if (field3 not entered) { print error. }
else { send mail and print success message. }

You have it attempting to send mail first, then errors.

Third, note the @ character.

$mail_sent = @mail( $to666, $subject, $message666, $headers666 );

@ suppresses errors or warnings from whatever function/program you are calling (mail() ). Errors are good . . they help. The way it's written, it's not all that helpful, mail will return true (1) or false (0).

A better way might be

if (mail( $to666, $subject, $message666, $headers666 )) {
print "Success message";
}
else { print "Mail error: mail could not be sent."; }

Next, the best way to debug is do an echo to make sure all your values are being posted properly. All together, I've added a few things to gather all the errors at once.

// Set this to 1 to print out values and NOT send mail
// this is the printout I mentioned. See $debug below
$debug = null;
$errors = null;
if(strlen($name)<1) {
$errors .= "<li>You did not type in a name.</li>"; // note the list tags
}
if(strlen($yourpost)<1) {
$errors .= "<li>You did not type in a post.</li>";
}
if(strlen($subject)<1) {
$errors .= "<li>You did not enter a subject.</li>";
}
//
if ($errors) {
print "<p>Your post has the following errors, please try again:</p><ul>$errors</ul>";
}
else {
$thedate=date("U"); //get unix timestamp
$displaytime=date("F j, Y, g:i a");
$name=$session->username;
$yourpost=strip_tags($_POST['yourpost']);
$subject=strip_tags($_POST['subject']);
$to=strip_tags($_POST['username']);
$tom1=strip_tags($_POST['email']);
$message666 = "new message from $name\nMessage:\n$yourpost\n\nwww.#*$!#*$!xx";
$headers666 = "From: $tom1\r\n";
if ($debug) {
print "<p>Mail to: $to From: $tom1 Headers $headers666 Subject: $subject Name: $name message: $message666</p>";
}
else {
if (mail( $to, $subject, $message666, $headers666 )) {
print "<p>Message posted, go back to <a href=\"forum.php\">Forum</a>.</p>";
}
else { print "<p>Mail error: mail could not be sent.</p>"; }
}
}

Note that this is not "copy and paste" code and may have errors but should get you started in the right direction.

An aside, to avoid displaying the user's email address in the dropdown list, you might want to use the first method you had with the username as the value, then look up the email address again only on submit

select email from table where username='username'

loki

7:22 pm on Jan 6, 2012 (gmt 0)

10+ Year Member



omg wow ty that has done it and it is now working well i have been trying to get that working for god know how long ty agen for ya help