How to send different message to admin and user in php - php

I want to send diifferent email to both admin and user.I dont know how to do that.
The code which i am sharing with you is sending same email to both admin and user.Please help me ..
Here is my code ;
$car = $_POST['category'];
$pick = $_POST['text1'];
$drop = $_POST['text2'];
$source = $_POST['text3'];
$email= $_POST['text4'];
$to="$email";
$subject="Web Enquiry";
$message="Hi,". "\r\n" . "\r\n" .
"You've received an email with following details, from the inquiry made at the website- mail#silvertaxi.com" ."\r\n"."\r\n".
"Car Category:"." "."$car"."\r\n".
"Source Location:"." "."$pick"."\r\n".
"Destination Location:"." "."$drop"."\r\n".
"Day and Time.:"." "."$source". "\r\n".
"Email:"." "."$email". "\r\n" ."\r\n".
"Thanks & Regards,". "\r\n" .
"Web Admin"."\r\n" ;
$headers ="From:$email\n";
$headers .= 'Cc: admin#email.com' . "\r\n";
$headers.="MIME-Version: 1.0\n";
$headers.="Content-type: text/html; charset=iso 8859-1";
if(mail($to, $subject, $message,$headers))
{
echo "Your Message has been sent." ;
} else {
echo "";
}

You should call mail() function twice; one for user and another for admin. Remove the line
$headers .= 'Cc: admin#email.com' . "\r\n";
from your code.
Then, define different messages for user and admin as you want.
$to="$email";
$subject="Web Enquiry";
$message="....." //your message to user goes here
$to_admin = "admin#email.com";
$subject_admin = "...."; //subject for mail to admin
$message_admin = "....." //your message to admin goes here
Use the mail() function twice to send different emails.
if((mail($to, $subject, $message,$headers) && mail($to_admin, $subject_admin, $message_admin, $headers))
{
echo "Your Message has been sent." ;
} else {
echo "";
}

firstly you need to check he is user or admin. If he is user you can add a condition statement for its another for admin. Like this:
if($user_role == "user"){
//mail for user
.....
}
if($user_role == "admin"){
//mail for admin
.....
}
If you have any question feel free to ask.

Related

How to run mail function only once?

I want to send mail (only once) using php when $cstt == 10. I want to prevent sending mail on every time I refresh the page. here is my code
if ($cstt == 10) {
echo "Great";
// SENDING EMAIL
$to = "user#example.com";
$subject = "$tracking_id_user is in transit.";
$message =' Hi user, Simple html message. Best Regards!';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Company <info#company.com>' . "\r\n";
if(mail($to,$subject,$message,$headers)){
echo "mail send";
}else{
echo "email not sent";
}
}
I did the following tasks to achieve the result.
Created a Table mail_record.
added id and mail_sent columns.
after creating the table, wrote the following code.
Connectin to database
$conn = new mysqli('localhost', 'user_id', 'password', 'databasename');
Accessing the data
$sql = "SELECT * FROM mail_record";
$query = $conn->query($sql);
$row = $query->fetch_assoc();
Accessing database to get mail_sent record.
// GETTING MAIL SENT / NOT RECORD (BLANK = NOT SENT & yes10 = Sent)
$mail_status = $row(['mail_sent']);
// CREATING THE CONDITION
if($cstt == 10 and $mail_status == ''){
// SENDING EMAIL if $cstt = 10 & email is not sent (blank)
$to = "admin#example.com, user#example.com";
$subject = "email subject";
$message ='Hi user, email text';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: Web admin <admin#example.com>' . "\r\n";
if(mail($to,$subject,$message,$headers)){
// ADDING The RECORD THAT EMAIL HAS BEEN SENT FOR ($CSTT == 10)
$sql = "INSERT INTO `mail_record ` (`mail_sent`) VALUES ('yes10')";
if ($conn->query($sql) === TRUE) {
echo "Email sent and record added successfully.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
by adding a if condition / logic we can write a code to execute mail send action only once.
now if you refresh the page the first if condition will give result FALSE because $cstt == 10 TRUE but $mail_status == '' FALSE.
using this method you can be very sure that the mail will be sent only once. no matter how many times page is being refreshed.

Can we include 2 Mail() functions in 1 Page?

I want to sent 2 different E-Mails to 2 different persons on 'Send Message' Button. I have few questions regarding this.
Is it possible to send Multiple Emails from same Page.
I have written the following code but its not sending Any Email and returning Error Message in return.
<?php
include 'connect.php';
$subject = $_REQUEST['subject'] ; // Subject of your email
$personal_email= "abc#hotmail.com";
$message .= 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= $_REQUEST['product_type']. "<br>";
$message .= $_REQUEST['message'];
$subject_to_sender= "Confirmation";
$message_to_sender = "Thanks for contacting us, Our representative will contact you shortly.";
$name= mysql_real_escape_string($_REQUEST['name']);
$email= mysql_real_escape_string($_REQUEST['email']);
$message= mysql_real_escape_string($_REQUEST['message']);
$product_type= mysql_real_escape_string($_REQUEST['product_type']);
$address= mysql_real_escape_string($_REQUEST['address']);
if($address== "")
{
$address= "No Address is given.";
}
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['email'] . "\r\n"; // Sender's E-mail
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (#mail($email, $subject_to_sender, $message_to_sender, $headers))
{
mail($personal_email, $subject, $message, $headers);
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
To answer your former question, I can say Yes, its possible to use mail() function multiple times.
For the later question, the opening bracket on last if() statement is not closed. Add } after echo 'sent'; statement.

want to send session values in url

hi i am taking values from a page and getting values in second page whihch is working correctly but i have to send the values in url
<?php
session_start();
$_SESSION["email_address"] = $_REQUEST["email_address"];
echo "Favorite color is " . $_SESSION["email_address"] . ".<br>";
//$errors = '';
$myemail = 'info#abcdef.com';//<-----Put Your email address here.
$to_go="abcdef#gmail.com";
$to = $to_go;
$email_subject = "Contact form submission: aaname";
$email_body = "You have received a new message. ".
" Here are the details: Kindly <a href='localhost/ci/email_wait.php?email_address='.echo $email_address.'&password'=.$password.'>
Click Here</a> to reset your password";
$headers = "From:". $myemail;
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
echo "Mail Sent. Thank you we will contact you shortly.";
?>
NOTE: This code will not work on localhost.
Check out PHP's documentation on string interpolation
You want something like:
$email = "something#somewhere.com";
$body = "Your email address is: $email, hooray!";
echo $body;
Which will output Your email address is: something#somewhere.com, hooray!
Why you echo $email_address and where you initialize your email_address proper way to do like this
$email_body = "You have received a new message. Here are the details: Kindly<a href='localhost/ci/email_wait.php?email_address='".$email_address."'&password='".$password."'>Click Here</a> to reset your password";
and you can also send mail from localhost through smtp settings and put headers also before mail like this
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

PHP - Email is not sending

There are two steps first the email i sent to the person who wants the newsletter and the second email needs to be sent to info#dirtytrend.com. The problem is that the email is sent in the first step but the email in the second step is never getting through but the code seems to carry on without any errors.
The code is below
<?php
$name = $_POST["nameofperson"];
$to = $_POST["emailofperson"];
$subject = "Hi!";
$body = "Hi " . $name . ",<br><br>Thank you for subscribing?\n\nWe have logged your email to process your newsletter and you will recieve an email from us confirming your subscription.";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if (mail($to, $subject, $body, $headers)) {
$body2 = "Name: " . $name . "\n";
$body2 .= "Email: " . $to;
if(mail("info#dirtytrend.com", "Subscription Request", $body2)){
header("Location: http://www.dirtytrend.com/events.html");
}
else{
}
} else {
echo "ERROR: Email not sent please contact the system administrator";
}
?>
In the second mail() you have only 3 parameters and you're missing the 4th one with the headers so maybe it is recognized as spam?

coding autoresponder into phpmail

I've tried a few people's answer, none worked. I have a good working phpmail function, I just want to add an autorespond function to it, so that the sender can be immediately notified that submission was received.
My code is this:
<?php
$mail_to_send_to = "contact#matejkadesign.com";
$your_feedbackmail = "form#matejkadesign.com";
$sendflag = $_REQUEST['sendflag'];
if ( $sendflag == "send" )
{
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $your_feedbackmail" . "\r\n" . "Reply-To: $name <$email>" . "\r\n" ;
$a = mail( $mail_to_send_to, "Contact request", $message, $headers );
if ($a)
{
print("Message sent, thank you! You can send another");
} else {
print("Message wasn't sent, please check that your email was filled in properly");
}
}
?>
I want to say something like this in the autorespond:
Thank you for contacting us. This is an automatically generated reply confirming your submission. We will get back to you as soon as possible.
Sincerely,
John Doe
It is not clearly what you want but I doing a wild guess. You want to e-mail the user when he/she used this form. Use this code:
<?php
$mail_to_send_to = "contact#matejkadesign.com";
$your_feedbackmail = "form#matejkadesign.com";
$sendflag = $_REQUEST['sendflag'];
if ( $sendflag == "send" ) {
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $your_feedbackmail" . "\r\n" . "Reply-To: $name <$email>" . "\r\n" ;
$a = mail( $mail_to_send_to, "Contact request", $message, $headers );
if ($a)
{
print("Message sent, thank you! You can send another");
$message_back = "Thank you for contacting us. This is an automatically generated reply confirming your submission. We will get back to you as soon as possible.";
mail( $email, "Contact request", $message_back, $headers );
} else {
print("Message wasn't sent, please check that your email was filled in properly");
}
}
?>

Categories