Reputation: 54
Thank you in advance for reading.
I have an email function that partially works: it does send an email, with the correct message, subject, and headers, when I hard code in the email address. The SQL insertion above it works fine, so I know it is saving all the variables correctly to the database.
It does not send through the $email
, $first_names
, or $pass variables
, however. They come up as blanks in the message and $email does not work in the mail($email
, etc) function. Right now, mail()
only works when I hard code in an email.
The code, from the PHP file:
$email = $_POST['email'];
$first_names = str_replace("'","",$_POST['fname']);
$last_names = str_replace("'","",$_POST['lname']);
$password = $_POST['password'];
$user_type = 2;
$status = 1;
$last_login = '0000-00-00 00:00:00';
$sql_insert = "INSERT INTO `cb_sub_users` (`id`, `email`, `password`, `firstName`, `lastName`, `user_type`, `super_user`, `last_login`, `status`) VALUES (NULL, '$email', '$password', '$first_names', '$last_names', '$user_type', '$super_user_id', '$last_login', '1');";
$check_user = mysql_num_rows(mysql_query("SELECT * FROM `cb_sub_users` WHERE email='".$email."'"));
$message = 'Hi, ' + $first_names + '! Your team has invited you to join Our Site. Your username is ' . $email . ' and your password is ' . $password . '. Log in at blank for analytics you will actually use. If you have any questions, get in touch at [email protected].';
$subject = 'Welcome to Our Site';
$headers = array("From: [email protected]",
"Reply-To: [email protected]",
"X-Mailer: PHP/" . PHP_VERSION );
$headers = implode("\r\n", $headers);
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail('[email protected]', $subject, $message, $headers);
ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);
Thanks so much! Any help is much appreciated.
Upvotes: 1
Views: 1274
Reputation: 30899
Your code is vulnerable to SQL Injection and is using deprecated MySQL Functions. Here are some things you can do to improve your code:
<?php
$email = isset($_POST['email']?$_POST['email']:"";
$first_name = isset($_POST['fname'])?str_replace("'","",$_POST['fname']):"";
$last_name = isset($_POST['lname'])?str_replace("'","",$_POST['lname']):"";
$password = isset($_POST['password'])?$_POST['password']:"";
if(empty($email)){
echo "Email is empty.";
exit();
}
$name = "$first_name $last_name";
$message = "Hi, $first_name! Your team has invited you to join Our Site. Your username is $email and your password is $password. Log in at http://www.help.com/login for social media analytics you will actually use. If you have any questions, get in touch at [email protected].\r\n";
$subject = 'Welcome to Help';
$headers = array('From: "Help" <[email protected]>',
"Reply-To: [email protected]",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if(!ini_set("mail.log", "/tmp/mail.log")){
echo "<p>Mail Log not set.</p>";
}
if(!ini_set("mail.add_x_header", TRUE)){
echo "<p>X-Headers not enabled.</p>";
}
$result = mail($email, $subject, $message, $headers);
if(!$result){
echo "<p>Message Failed to Send.</p>";
)
?>
Since it's not clear how you're using your SQL, I just glossed over it since you're asking about mail()
here.
Upvotes: 1