PDA

View Full Version : PHP conditional email??



hombr3w311
December 18th, 2006, 12:20 AM
Hey all - I am looking to do the following. After my client adds a user to the database, he wants an email sent to them containing a welcome message, their userID and password. The only problem is that it cant be automated, because he approves them before they get added.

Here is what I need, I have a php form where he enters the newly added users email address. On 'Submit' if there is a match with the entered email, id like an email sent to the user containing the information that cooresponds with the email address in the mysql database (userName and userpass)

The mysql database already exists.

Please advise, thanks.!!

hombr3w311
December 18th, 2006, 12:58 AM
ok, here is the code i want to implement into the php file.


<?
$link = mysql_connect("server", "database", "password") or die("Could not connect : " . mysql_error());
$DB = "database";
$table = "users";
mysql_select_db($DB) or die ("Database $DB not select.." . mysql_error());
$result = mysql_query("SELECT * FROM users") or die(mysql_error());
$row = mysql_fetch_array( $result );
echo "Name: ".$row['first_name'];
$to = "abc@xyz.com";
$subject = "Test mail";
$message = "Test if message includes ".$row['first_name'];
$from = "ddd@hhh.com";
$headers = "From: $from";
if (mail($to,$subject,$message,$headers))
echo "Mail Sent";
else
echo "Mail message failed";
?>



So basically it should say, if $email = '$email' then send this form with the appropriate data.

Any suggestions???

Thanks.

Seb Hughes
December 18th, 2006, 05:48 AM
Your code is not displayed well, try putting them in the php tags

JoshuaJonah
December 18th, 2006, 08:23 AM
or you could just do it seb:D


<?
$link = mysql_connect("server", "database", "password") or die("Could not connect : " . mysql_error());
$DB = "database";
$table = "users";
mysql_select_db($DB) or die ("Database $DB not select.." . mysql_error());
$result = mysql_query("SELECT * FROM users") or die(mysql_error());
$row = mysql_fetch_array( $result );
echo "Name: ".$row['first_name'];
$to = "abc@xyz.com";
$subject = "Test mail";
$message = "Test if message includes ".$row['first_name'];
$from = "ddd@hhh.com";
$headers = "From: $from";
if (mail($to,$subject,$message,$headers))
echo "Mail Sent";
else
echo "Mail message failed";
?>

raz
December 18th, 2006, 09:59 AM
<?
$link = mysql_connect("server", "database", "password") or die("Could not connect : " . mysql_error());
$DB = "database";
mysql_select_db($DB);

if($_POST['submit']) {

$check = mysql_query("SELECT * FROM `users` WHERE `email`='$_POST[email]' LIMIT 1");
$num = mysql_num_rows($check);
if ($num > 0) {
// send email
$get = @mysql_num_rows($check);
$to = $_POST['email'];
$from = "ddd@hhh.com"; // what kind of email is this? o_O
$subject = "Test mail";
$body = "Enter whatever your email message\n";
$body .= "contains and whatever details you want to include...";
$headers = "From: $from";
$mail = mail($to, $subject, $message, $headers);
if ($mail) {
echo "Email sent.";
}
else {
echo "Error sending mail.";
}
}
else {
echo "Couldnt find " . $_POST['email'] . " in the database.";
}

}
else {
die("Need to submit form.");
}
?>



Hope that helps...

hombr3w311
December 18th, 2006, 12:52 PM
Thank, I really appreciate that code, it got me going in the right direction. Sooner or later I will learn PHP! I modified it a bit and got it all working properly.
Thanks so much!

Here is the modified code:



<?
$link = mysql_connect("mysql.*.com", "database", "password") or die("Could not connect : " . mysql_error());
$DB = "srdes001";
$table = "users";
mysql_select_db($DB);

if($_POST['submit']) {

$check = mysql_query("SELECT * FROM $table WHERE `userMail`='$_POST[email]' LIMIT 1");
$num = mysql_num_rows($check);
$result = mysql_query("SELECT * FROM $table") or die(mysql_error());
$row = mysql_fetch_array( $result );
if ($num > 0) {
// send email
$get = @mysql_num_rows($check);
$to = $_POST['email'];
$from = "user_accounts@srdesign.com";
$subject = "SR Designs User Account Activated";
$body = "Thank you for registering with SR Designs. Your account has been approved and your log in information follows:\n";
$body .= "Your current user ID is: ".$row['userName'];
$body .= " and your current password is: ".$row['userPassword'];
$headers = "From: $from";
$mail = mail($to, $subject, $body, $headers);
if ($mail) {
print 'Thanks, you have successfully activated: '.$row['userName'].'s account. An email has been sent to this user via this email adress: '.$row['userMail']. '. Please close this window.';
}
else {
echo "Error sending mail.";
}
}
else {
echo "Couldnt find " . $_POST['email'] . " in the database.";
}

}
else {
die("Need to submit form.");
}
?>