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.
Related
<?php
$path = 'data.txt';
if (isset($_POST['email']) ) {
$fh = fopen($path,"a+");
$string = $_POST['email'].' - '.$_POST['field2'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
$to = $_POST['email'];
$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);
?>
Can someone tell me what is wrong with this code?
I want that the script to send email to $_POST['email'] that came from a previous html file.
So first,store that email,and after that sent an email to it.
Thank you
Edit: The script store the email with success but the email is not sent.
Thanks again
I am a PHP novice here. I have a send mail script that looks like this:
$to = 'example#email.com';
$from = 'noreply#email.com';
$subject = 'Test Submission';
$message = 'This is just another test.';
$headers = 'From: example#email.com' . "\r\n" .
// 'Reply-To: example#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
This script works fine and sends an e-mail when the page is loaded. The problem is that when I alter the script just enough so that the form is not submitted until the Submit button is clicked then all of a sudden nothing works anymore. Here is the altered code I have been trying to use (and which seems to go right along with what the PHP site suggests):
if(isset($_POST['submit'])){
$to = 'example#email.com';
$from = 'noreply#email.com';
$subject = 'Test Submission';
$message = 'This is just another test.';
$headers = 'From: example#email.com' . "\r\n" .
// 'Reply-To: example#email.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Make sure your submit button on your form has a NAME attribute. The value of that NAME attribute is what gets sent to the server, so:
<input type="submit" name="btnSubmit" value="Go!">
...would result in this variable and value:
$_POST["btnSubmit"] = "Go!";
...and you would check it like this:
if(isset($_POST["btnSubmit"]))
{
...
}
I have some affiliate script that I buy, however this script doesn't have function to
"Send password and login id to all my affiliate" weekly or maybe once per 2 week.
This is because in my experience he/she do not have many members not coming back because not only forgot password but also forgot username.
I want every month the members get email example
To: cubaan04#hotmail.com
From : admin#admin.com
Subject : Reminder : Your login name and password for website
Hi Firstname,
We worry if you forgot your login name and password. We love to see you back and active in our network. Here the detail...
your login name : Cubaan04
your password : Cubaan04
please login to website
Your truely
admin website
I found this script but dont know how to modified it
<?php
$db = mysql_connect("localhost", "plazacen_t", "plazacentrey");
mysql_select_db("plazacen_rak",$db);
$result = mysql_query("SELECT `email` FROM `affiliate_users`");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0]);
}
mysql_free_result($result);
function sendMail($to){
$subject = 'the subject';
$message = 'hello Shahrul';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
it not work
and how to put password and login in massage from the database?
Thanks you for read this, iam just noob for this
hope get help from you guys
updated
From our friend answer, i modified it. Yes it send email now but not work like i want, i put
<?php
$db = mysql_connect("localhost", "plazacen_rak", "rakan852");
mysql_select_db("plazacen_rak",$db);
$result = mysql_query("SELECT email,password,name FROM `affiliate_users`");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0],$row[1],$row[2]);
}
mysql_free_result($result);
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = 'your login name:". $id ." password: ". $pwd ."';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
?>
what I received is email like this;
From : webmaster#example.com
the subject
04/04/2015 9.59 PM
your login name:". $id ." password: ". $pwd ."
it is not change the $id to username in database, so do the $pwd
don't know whats wrong, any further help. TQ
The problem is with your sendMail function, You cannot take variables inside single quotes modify it as
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = "your login name: {$id} password: {$pwd}";
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
another alternative could be
function sendMail($to,$pwd,$id){
$subject = 'the subject';
$message = 'your login name:' . $id .' password: '. $pwd;
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Both will work like a charm.
First change your query to:
$result = mysql_query("SELECT email,password,userid FROM `affiliate_users`");
Then change your code like this:
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
sendMail($row[0],$row[1],$row[2]);
}
mysql_free_result($result);
function sendMail($to,$pwd,$id){
$subject = 'the subject';
**$message = "your login name:". $id ." password: ". $pwd ."";**
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Note
I do not know what your field names are in the affiliate_users table so replace this "email,password,userid" with your actual field name.
If you want to execute this script every month then you can use cronjob.
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 ...>
i m using a php form for sending the contact email from my website. the code is ok and working fine on one website but not working in second website. both website having difference server space and hosting. Not showing any error on page
code is below :
<?php
$name = $_REQUEST['rohini_name'] ;
$contact = $_REQUEST['rohini_contact'] ;
$email = $_REQUEST['rohini_email'] ;
$remark = $_REQUEST['rohini_message'] ;
$MailTxt = "Following are Details" . "\r\n" .
"============================" . "\r\n" .
"Name : " . $name . "\r\n" .
"Mobile : " . $contact . "\r\n" .
"Email : " . $email . "\r\n" .
"Remark : " . $remark . "\r\n";
$to = "ballu9868#gmail.com";
$subject = "Enquiry from rohiniseeds.com";
$headers = "From: www.rohiniseeds.com";
mail($to,$subject,$MailTxt,$headers);
?>
Please ask your hosting to make sure that mail funstion is supported.
Try adding an IF statement around the mail($to,$subject,$MailTxt,$headers) function as follows:
if(mail($to,$subject,$MailTxt,$headers)){
echo 'Success!';
} else {
echo 'Error!';
}
If it says Success then you know that it's being sent by the server, and the problem is when it's trying to hit the inbox. If it says error, then the problem related to the server.
Also, please use email#rohiniseeds.com in the from header, which is a valid email address, instead of the URL of the website.
Please try this
$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);