Can anyone suggest corrections in these few line of code. I am very much disturbed that it is not working and time elapsing. Please help me with corrections or suggest a new one.
<?php
$email_to = 'info#khawabnama.com';
$subject = "Contact US";
$name =$_POST['flname'];
$email_from = $_POST['email'];
$message = $_POST['message'];
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success=#mail($email_to, $subject, $body, $headers);
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
// $success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
//header("Location: index.php");
//header('Location: http://www.khawabnama.com/index.php');
//die;
Related
I have included my PHP code and hopefully someone can spot the error!
I have replaced my email for privacy and spam reasons.
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contacting us. We will get back to you as soon as possible. '
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'test#email.com';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
The issue I am having is in the message portion of the email. The email sends just fine, the outline is there but no name email or message is printed from the variable. I can't seem to see what the issue is. Any help is appreciated.
<?php session_start();
$name=$_POST['name'];
$email=$_POST['email'];
$text=$_POST['text'];
$to= 'noreply#noreply.com';
$subject = 'Request';
$message='Hello, my name is:' . $name . "\r\n" . 'My Email is: ' . $email . "\r\n" . 'My Message is: '. $text;
$headers = 'From: Request' . "\r\n" .
'Reply-To: noreply#noreply.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo 'Mail Sent';
?>
From the comment you are passing the data as query string not as payload in the POST. SO change
$name=$_POST['name'];
$email=$_POST['email'];
$text=$_POST['text'];
to
$name=$_GET['name'];
$email=$_GET['email'];
$text=$_GET['text'];
And should work !
You sent form using GET method, data are in $_GET:
<?php
session_start();
$name = $_GET['name']; // here
$email = $_GET['email']; // here
$text = $_GET['text']; // here
$to = 'noreply#noreply.com';
$subject = 'Request';
$message='Hello, my name is:' . $name . "\r\n" . 'My Email is: ' . $email . "\r\n" . 'My Message is: '. $text;
$headers = 'From: Request' . "\r\n" .
'Reply-To: noreply#noreply.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo 'Mail Sent';
?>
To use your current script version, just change method attribute in form to post, like <form method=post ...>
complete beginner at PHP and was wanting a little direction for a website I am creating. I want the admin of the website to receive an email with all of the form information aswell as it being stored in the database. The database is storing the information fine, just need an email notification. How is this achieved. My PHP code is:
<?php
session_start();
include('connection.php');
$product = $_POST['product'];
$productcomments = $_POST['productcomments'];
$name = $_POST['name'];
$address = $_POST['address'];
$age = $_POST['age'];
$delivery = $_POST['delivery'];
mysql_query("INSERT INTO orderform(product, productcomments, name, address, age, delivery)VALUES('$product', '$productcomments', '$name','$address', '$age', '$delivery')");
header("location: google.com");
$to = 'j_bussey#live.co.uk';
$subject = 'Order';
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
mysql_close($con);
?>
this is the very basic example of mail function. read more about mail() manual here.
$email = $_POST['email'];
$subject = "Email Subject";
$message = "Email Message Body";
mail($email, $subject, $message, "from: admin#yourdomain.com");
PHP has an awesome mail() function. I'm taking this from the documentation page here: http://us2.php.net/manual/en/function.mail.php
<?php
$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, $message, $headers);
?>
So since you're already grabbing the variables, you would just edit the $message variable in the code above to look something like this:
$message = 'Product: ' . $product . '<br /> Product Comments: ' . $productcomments . '<br /> ';
etc, etc. If you don't want to assume that they have html emails enabled, you would use \n instead of <br />
Edit:
You also need to change your header('Location: google.com'); to header('Location: http://www.google.com'); or wherever you want to redirect after the email has been sent off.
The following code is sending an email from my website, but the email comes from cgi-mailer#kundenserver.de, how do i change this to the sender's email address, which i have given the variable $email:
<?php
if(isset($_POST['submit'])) {
$msg = 'Name: ' .$_POST['FirstName'] .$_POST['LastName'] ."\n"
.'Email: ' .$_POST['Email'] ."\n"
.'Message: ' .$_POST['Message'];
$email = $_GET['Email'];
mail('me#example.com', 'Message from website', $msg );
header('location: contact-thanks.php');
} else {
header('location: contact.php');
exit(0);
}
?>
Adding the header From: to my mail command seems to allow me to change the email address, but i can't work out how to do it to the variable.
<?php
$to = "someone#example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
For more reference
http://php.net/manual/en/function.mail.php
Declare the variable in the headers..
<?php
$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, $message, $headers);
?>
Edit:
<?php
if(isset($_POST['submit'])) {
$msg = 'Name: ' .$_POST['FirstName'] .$_POST['LastName'] ."\n"
.'Email: ' .$_POST['Email'] ."\n"
.'Message: ' .$_POST['Message'];
$email = $_GET['Email'];
$headers = 'From: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('me#example.com', 'Message from website', $msg, $headers );
header('location: contact-thanks.php');
} else {
header('location: contact.php');
exit(0);
}
?>
Add this to the header
$headers .= 'From: ' . $from . "\r\n";
$headers .='Reply-To: $from' . "\r\n" ;
mail($to,$subject,$message,$headers);
It should set the sender.
where
$from= "Marie Debra <marie.debra#website.com>;"
$from = $_POST['email'];
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
here is my code....
$subject = "This is Subject";
$headers .= 'Content-type: text/html; charset=iso-8859-1';
$to = 'foo#foo.com';
$body = 'Mail Content Here';
mail($to, $subject, $body, $headers);
but when i open this file it sends a mail to $to successfully but with wrong headers....and my hosting server default address i.e mars.myhosting.com, instead of mydomain#domain.com how can i fix that
Look at this from php.net
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: Webmaster <webmaster#example.com>' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Add the from header
Here is what I would do via PHP:
<?PHP
$to = 'email#address.com';
$subject = 'desired subject';
$message = 'desired message';
$headers = 'From: example#email.com' . "\r\n" .
'Reply-To: example#email.com' . "\r\n" .
'Return-Path: example#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
I hope that helps some :)