Kindly seeking your help with the below code:
if( !$error ) {
move_uploaded_file($file_loc,$folder.$file);
$cuser = $userRow['userName'];
$res = mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow = mysql_fetch_array($res);
$query = "INSERT INTO postoffers(postedby,reqName,reqEmail,reqHotel,reqOutlet,reqCnum,reqPostType,reqPostHead,reqPostDet,offerStarts,offerEnds,file,type,size) VALUES('$cuser','$rname','$remail','$rhotel','$routlet','$rcnum','$rposttype','$rposthead','$rpostdet','$rbdate','$redate','$file','$file_type','$file_size')";
$res = mysql_query($query);
if ($res) {
$errTyp = "success";
$errMSG = "Successfully Posted!";
unset($rname);
unset($remail);
unset($rhotel);
unset($routlet);
unset($rcnum);
unset($rposttype);
unset($rposthead);
unset($rpostdet);
unset($rbdate);
unset($redate);
unset($file);
$to = ;
$subject = "Your Post Offer";
$message = "Thank you " . $cuser . " for the Post Request.";
$headers = 'From: AFP Webmaster' . "\r\n" .
$headers = "MIME-Version: 1.0" . "\r\n" .
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n" .
'Reply-To: AFP Webmaster' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later...";
}
}
The code WORKS PERFECTLY on the following:
1. Storing the value on DB.
2. If i place a valid email like "name#gmail.com" in $to
What I want to happen now is assigning the value of the "reqEmail" from the database or the value from the textfield "remail" to $to.
So the email will go to the email who filled up the form.
i hope i made myself clear and understandable.
Thanks!
You're clearing your variables before sending the email, so PHP doesn't know where to send the email to. You need to place your "unset" functions after the PHP Mailer call. Try this:
if (!$error) {
move_uploaded_file($file_loc, $folder.$file);
$cuser = $userRow['userName'];
$res = mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']);
$userRow = mysql_fetch_array($res);
$query = "INSERT INTO postoffers(postedby,reqName,reqEmail,reqHotel,reqOutlet,reqCnum,reqPostType,reqPostHead,reqPostDet,offerStarts,offerEnds,file,type,size) VALUES('$cuser','$rname','$remail','$rhotel','$routlet','$rcnum','$rposttype','$rposthead','$rpostdet','$rbdate','$redate','$file','$file_type','$file_size')";
$res = mysql_query($query);
if ($res) {
$to = ;
$subject = "Your Post Offer";
$message = "Thank you " . $cuser . " for the Post Request.";
$headers = 'From: AFP Webmaster' . "\r\n".
$headers .= "MIME-Version: 1.0" . "\r\n".
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n" . 'Reply-To: AFP Webmaster' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
$errTyp = "success";
$errMSG = "Successfully Posted!";
unset($rname);
unset($remail);
unset($rhotel);
unset($routlet);
unset($rcnum);
unset($rposttype);
unset($rposthead);
unset($rpostdet);
unset($rbdate);
unset($redate);
unset($file);
} else {
$errTyp = "danger";
$errMSG = "Something went wrong, try again later...";
}
}
Related
I've got stuck trying to echo multiple rows from a MySql database into the $message part of the email. The attached code only returns one result.
I want to send 1 email, in the email I want to output the 10 rows from the SQL query.
I actually receive the same email ten separate times rather than one email with ten rows.
$sql = "SELECT * FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$message = $row["Maxim"]. "</br>";
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Move the mail call out of the loop
// don't select all (*) fields if you only need one
$sql = "SELECT Maxim FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
$message = '';
while($row = $result->fetch_assoc()) {
$message .= $row["Maxim"]. "</br>";
}
// prepare email headers
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// then send
mail($to, $subject, $message, $headers);
I haven't tested the code but give this a try:
$to = 'email#email.com';
$subject = 'Daily Stoic Email';
$sql = "SELECT * FROM stoic_quotes Order By rand() Limit 0,10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$message = $row["Maxim"]. "</br>";
}
$headers = 'From: email#email.com' . "\r\n" .
'Reply-To: email#email.om' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
I have placed the to, subject, header and mail function out of the loop.
Cheers
After successful execution of command I want to send two different mails to two different person through if else function in PHP. I am able to send a mail using below code. How to send another mail with different headers & contents.
// if new reservation has been successfully added to reservation table
// send notification to admin via email
if($result){
$to = $email;
$subject = $reservation_subject;
$message .= $reservation_message."\r\n\n";
$message .= "Customer name:" .$provinsi."\r\n";
$message .= "Special Request :" .$comment."\r\n";
$from = $admin_email;
$headers = "From:" . $from."\r\n".
mail($to,$subject,$message,$headers);
echo "OK";
}else{
echo "Failed";
}
This is all you need to do, just set the new reciever and sender and message and call mail() again
Mail is much like any PHP function, you set up its parameters and call it.
if($result){
$to = $email;
$subject = $reservation_subject;
$message .= $reservation_message."\r\n\n";
$message .= "Customer name:" .$provinsi."\r\n";
$message .= "Special Request :" .$comment."\r\n";
$from = $admin_email;
$headers = "From:" . $from."\r\n".
if ( mail($to,$subject,$message,$headers) ) {
echo "OK message 1 sent";
} else {
echo "FAILED message 1 sent";
}
$to = $email_2;
$subject = $reservation_subject_2;
$message = $reservation_message_2."\r\n\n";
$from = $admin_email_2;
$headers = "From:" . $from."\r\n".
if ( mail($to,$subject,$message,$headers) ) {
echo "OK message 2 sent";
} else {
echo "FAILED message 2 sent";
}
}else{
echo "Failed";
}
Try This code :
function send_mail($to_email,$from_email,$subject,$message){
$nameToBeDisplayed = "XYZ";
$headers = 'From: ' . $nameToBeDisplayed . '<' . $from_email . '>' . "\r\n";
$headers .= 'Reply-To: ' . $from_email . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message = "";
mail($to_email, $subject, $message, $headers);
}
if($result){
send_mail('to1#gmail.com','from1#gmail.com','your subject','your message');
send_mail('to2#gmail.com','from2#gmail.com','your subject','your message');
}else{
echo "Failed";
}
if($result){
createEmailOne();
createEmailTwo();
}else{
echo "Failed";
}
function createEmailOne(){
$to = $email;
$subject = $reservation_subject;
$message = $reservation_message."\r\n\n";
$message .= "Customer name:" .$provinsi."\r\n";
$message .= "Special Request :" .$comment."\r\n";
$from = $admin_email;
$headers = "From:" . $from."\r\n".
mail($to,$subject,$message,$headers);
echo "OK";
}
function createEmailTwo(){
$to = "john#snow.com";
$subject = $reservation_subject;
$message = "Something went wrong in this form, here is the info: \r\n\n";
$message .= $reservation_message."\r\n\n";
$message .= "Customer name:" .$provinsi."\r\n";
$message .= "Special Request :" .$comment."\r\n";
$from = $admin_email;
$headers = "From:" . $from."\r\n".
mail($to,$subject,$message,$headers);
echo "OK";
}
I want to make a birthday reminder and from here I don't know what to do.
In my MySQL users DB U have birthdate with [month/day/year] And is not working,no error, i updated my bday with this day and still no activity.
This is what I have so far:
<?php
$conn = new PDO('mysql:host=localhost;dbname=tbl', 'user', 'pass');
$today = date("m.d.y");
$sqlb = "SELECT `birthdate`, `name`, `surename` FROM `mls_users` WHERE birthdate = '$today'";
$userz = $conn->query($sqlb);
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
echo 'Todays is'.$name.' '.$surename.' birthday';
}
?>
I suppose you want to create an email notification.
$mail_content = '';
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
$mail_content .= 'Todays is'.$name.' '.$surename.' birthday. <br>';
}
// send mail to you
// mail code
if($mail_content){
$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, $mail_content, $headers);
}
If you have the users email IDs saved in your Db you can send email to them
foreach ($userz as $row) {
$name = $row['name'];
$surename = $row['surename'];
//mail code
//email message
$mail_content = 'Happy birthday '.$name.' '.$surename.' !. <br>';
$to = $row['user_email']; //users email field
$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, $mail_content, $headers);
}
You can automate the script executing process using CRON Job
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to send an email using php but unable to do it, whereas I am getting data into MySQL database.
I have posted my whole code, please check and let me know where's the mistake.
$objConnect = mysql_connect("localhost","username","pwd");
$objDB = mysql_select_db("database");
$strName = $_POST["name"];
$strEmail = $_POST["email"];
$to = $strEmail;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot save data!";
}
else
{
mail($to, $subject, $message, $headers);
$arr['StatusID'] = "1";
$arr['Message'] = "Data saved successfully";
}
mysql_close($objConnect);
echo json_encode($arr);
This code is really working ...
<?php
$to = 'to_email#gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$errLevel = error_reporting(E_ALL ^ E_NOTICE); // suppress NOTICEs
if(!mail($to, $subject, $message, $headers)){
error_reporting($errLevel);
}
?>
You have not added mail() function in your code then how will it send the mail and to send email from localhost it should be properly configured. Below code will work on the server.
$objConnect = mysql_connect("localhost","username","pwd");
$objDB = mysql_select_db("database");
$strName = $_POST["name"];
$strEmail = $_POST["email"];
$to = $strEmail;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: mymail#gmail.com' . "\r\n" .
'Reply-To: mymail#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(!mail($to, $subject, $message, $headers)){
echo 'Mail cant be send';
}
else
{
$strSQL = "insert into data (name, email) VALUES ('".$strName."','".$strEmail."')";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Message'] = "Cannot save data!";
}
else
{
mail($to, $subject, $message, $headers);
$arr['StatusID'] = "1";
$arr['Message'] = "Data saved successfully";
}
echo json_encode($arr);
}
mysql_close($objConnect);
I'm clueless when it comes to PHP and have a script that emails the contents of a form. The trouble is it only sends me the comment when I want it to also send the name and email address that is captured.
Anyone know how I can adjust this script to do this?
A million thanks in advance!
<?php
error_reporting(E_NOTICE);
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comment'])>1)
{
$to = "me#me.com";
$headers = 'From: '.$_POST['email'].''. "\r\n" .
'Reply-To: '.$_POST['email'].'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = "Contact Form";
$message = htmlspecialchars($_POST['comment']);
if(mail($to, $subject, $message, $headers))
{
echo 1; //SUCCESS
}
else {
echo 2; //FAILURE - server failure
}
}
else {
echo 3; //FAILURE - not valid email
}
?>
You could do
$extra_fields = 'Name: '.$_POST['name'].'<br>Email: '.$_POST['email'].'<br><br>Message:<br>';
$message = $extra_fields.$_POST['comment'];
Not exactly clean, but you get the point. Just concatenate the data in with the $message.
Change this line:
$message = htmlspecialchars($_POST['comment']);
to
$message = htmlspecialchars($_POST['name'] . $_POST['email'] . "\r\n" . $_POST['comment']);
Or something to that effect
The problem is your $message = ... line, its only including the $_POST['comment']) variable. You need to add the $_POST['name'] and $_POST['email'] like something below:
$message = '';
$message .= htmlspecialchars($_POST['name']) . "\n";
$message .= htmlspecialchars($_POST['email']) . "\n";
$message .= htmlspecialchars($_POST['comment']) . "\n";