Let say I have a class, for example:
class User{
function callname(){
$user = $_SESSION['id'];
$query = ("SELECT * FROM user WHERE user.id='$user'");
while ($result=mysql_fetch_array($query)){
echo ($result['username']);}}}
And then, make instance for User object:
$user = new User;
Code to send mail:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = trim($_POST['username']);
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
if ($check==TRUE){
$name = $user->callname();
$to = "myemail#domain.com";
$subject = "Example Subject";
$headers = "From: My Domain".'\r\n'.
"MIME-Version: 1.0".'\r\n'.
"Content-Type: text/html; charset=ISO-8859-1".'\r\n'.
'X-Mailer: PHP/' . phpversion();
$message = "Hai $name, this is the new message.";
mail($to, $subject, $message, $headers);
} else {
?>
<script type="text/javascript">
alert("Sorry, username not exist !");
</script>
<?php
}
}
Mail function was working correctly and I have received an email too. The problem is
$name didn't print the name of user in the email. I've tried this $name = $user->callname(); on different page without the if() and it was working.
\r\nMIME-Version: 1.0 and so on was print in the From header.
I think you need to return $result['username'] instead of echoing it.
For special characters be read as such you need them in double quotes (").
A attempt to correct your code:
class User{
function callname(){
$user = $_SESSION['id'];
$query = mysql_query("SELECT * FROM user WHERE user.id='$user'");
while ($result=mysql_fetch_array($query)){
return $result['username'];}}}
Next part:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$username = mysql_real_escape_string(trim($_POST['username']));
$check = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username'"));
if ($check==TRUE){
$name = $user->callname();
$to = "myemail#domain.com";
$subject = "Example Subject";
$headers = "From: My Domain"."\r\n".
"MIME-Version: 1.0"."\r\n".
"Content-Type: text/html; charset=ISO-8859-1"."\r\n".
'X-Mailer: PHP/' . phpversion();
$message = "Hai $name, this is the new message.";
mail($to, $subject, $message, $headers);
} else {
?>
<script type="text/javascript">
alert("Sorry, username not exist !");
</script>
<?php
}
Related
I cant seem to find a solution to this on the site. What I would like to do is to have the user click on a button. This will then send a mail to me or someone in the office and the mail would then contain the logged in user details. I am sending the mail successfully, but I am unsure on how to get the code to send me the users info. Here is my code:
include 'contact/config.php';
require_once 'class.user.php';
$user_home = new USER();
if(!$user_home->is_logged_in())
{
$user_home->redirect('index.php');
}
$stmt = $user_home->runQuery("SELECT * FROM tbl_client_info WHERE UCODE=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
$name = $row['firstname'];
$email = $row['billingemail'];
$subject = "myDraadloze Package Contact Card";
$message = stripslashes($_POST['Please contact user regarding example']);
$error = '';
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,
"From: ".$name." <".$email.">\r\n"
."Reply-To: ".$email."\r\n"
."X-Mailer: PHP/" . phpversion());
if($mail)
{
echo 'OK';
}
}
}
?>
Any help would really be appreciated.
Thanks
include 'contact/config.php';
require_once 'class.user.php';
$user_home = new USER();
if(!$user_home->is_logged_in())
{
$user_home->redirect('index.php');
}
else
{
$stmt = $user_home->runQuery("SELECT * FROM tbl_client_info WHERE UCODE=:uid");
$stmt->execute(array(":uid"=>$_SESSION['userSession']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
error_reporting (E_ALL ^ E_NOTICE);
$post = (!empty($_POST)) ? true : false;
if($post)
{
$name = $row['firstname'];
$email = $row['billingemail'];
$subject = "myDraadloze Package Contact Card";
$email_to = " email#gmail.com";
$message = .= "Firstname: ".($name)."\n\n";
$message = .= "Email: ".($email)."\n\n";
$message = .= "Firstname: ".($name)."\n\n";
$error = '';
if(!$error)
{
$headers = 'From: your Website/email'."\r\n".
'Reply-To: Email.#ste.com'."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(#mail($email_to, $subject, $message, $headers))
{
echo 'OK';
}
}}
?>
Hope this helps!
the reply-to and from format doesnt look right. I would just create a variable for them and then stick it in the code.
$name = $row['firstname'];
$email = $row['billingemail'];
$subject = "myDraadloze Package Contact Card";
$message = stripslashes($_POST['Please contact user regarding example']);
$error = '';
$replyemail="'Reply-To: ".$name."<".$email.">'"."\r\n"; //looks likes this 'Reply-to: customer name <customeremail#whatever.com'\r\n
$fromemail="'from: ".$name."<".$email.">'"."\r\n"; //looks likes this 'from: customer name <customeremail#whatever.com'\r\n
//yours looked like this
//from:customername <customeremail#whatever.com\r\n
//these are headers and need to be seperated with '
//the ' should be at start and before \r\n
if(!$error)
{
$mail = mail(WEBMASTER_EMAIL, $subject, $message,$fromemail.$replyemail
."X-Mailer: PHP/" . phpversion());
I am making a database validation system. Upon validation, the handler is supposed to send the data to an email address, while simultaneously updating the data to another database. The values of id, name, board, query and rand are received from the previous page.
Now, with the given code, I am unable to update the table named 'answers' within my database. The line of code for this functionality is in bold.
<?php
$servername = "localhost";
$username = "root";
$password = '';
$dbname = "bsp";
$conn1 = new mysqli($servername, $username, $password, $dbname);
$id="";
$value = "";
$name = "";
$board = "";
$query="";
$id = $_POST['id'];
$name = $_POST['name'];
$board = $_POST['board'];
$query = $_POST['query'];
$rand = $_POST['rand'];
$value=$_POST['option'];
$to="";
$subject="";
$msg="";
$headers="";
if($value=='delete')
{
$sql="DELETE FROM rti WHERE ID=$id";
if ($conn1->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn1->error;
}
}
else if($value=='validate')
{
**$sql="INSERT INTO answers (name, board, query, rand) VALUES (\"$name\", \"$board\", \"$query\", \"$rand\")";**
if($board=='BRCA')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = "Your code is " . $rand . ".";
$msg .= " Visit localhost/BSP/landing.php and post the code, and subsequently your answer.";
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSW')
{
$to = 'email#rediffmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSA')
{
$to = 'email#iitd.ac.in';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BSP')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
else if($board=='BHM')
{
$to = 'email#gmail.com';
$subject = 'RTI Query: Do not reply to this email.';
$msg = $query;
$headers = 'From: contact#yoursite.com';
mail($to, $subject, $msg, $headers);
}
}
if(mail($to, $subject, $msg, $headers)==TRUE)
{
echo "The mail was sent successfully.";
}
$conn1->close();
?>
I am struggling with the following.When a user fills in a form looking like this, the inquiry gets sent to us to follow up the lead, but I need to find a way to modify my email script to send a confirmation email to the user aswell
My email script looks like this:
MY QUESTION HOW DO I MODIFY THIS TO SEND A THANK YOU CONFIRMATION TO THE USER
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$eventType = $_POST['eventType'];
$nrGuests = $_POST['nrGuests'];
$state = $_POST['state'];
$suburb = $_POST['suburb'];
$msg = $_POST['msg'];
$refferal_page = $_POST['page'];
$ip = $_SERVER['REMOTE_ADDR'];
if(!isset($refferal_page)){
$refferal_page="Source Unknown";
}
$to_email = "unknown#yahoo.co.nz"; //Recipient email
//TODO Sanitize input data using PHP filter_var().
$suburb = filter_var($_POST["suburb"], FILTER_SANITIZE_STRING);
$subject = "You Have a New Booking!";
//Add booking INFO here;
$message = "A new Enquiry has been received from http://www.xxxxx.com.au. Please find details below and follow up:";
//email body
$message_body = $message."\r\n\r\n".$name."\r\nEmail : ".$email."\r\nPhone Number : ". $mobile."\r\nEvent Type: ".$eventType."\r\nNumber Guests:".$nrGuests."\r\nState: ".$state."\r\nSuburb:".$suburb."\r\nIP ADDRESS:".$ip."\r\nMessage:".$msg."\r\nReferal Page Source:".$refferal_page;
//proceed with PHP email.
$headers = 'From: '.$name.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if($send_mail)
{
echo '<h1 class="page-header">Enquiry Successfully submitted</h1>';
}
else if(!$send_mail){
echo'<h1 class="page-header" style="color:RED">OOPS...SOMETHING WENT WRONG, PLEASE TRY AGAIN!</h1>';
}
}//server request method
else{
echo'<h1 style="color:red">SUBMIT A FORM PLEASE!</h1>';
}
?>
EMAIL THAT COMES TO US
A new Inqury has been received from www.xxxx.com.au. Please find details below and follow up:
FROM: Ryan XXX
Email : aass#testms.com
Phone Number : 334533
Event Type: Aniversary
Nr Guests: 34
State: Melbourne
Suburb: XYZ
IP ADDR: 162.111.255.111
MSG: this is a test. please disregard
Referal Page Source:aniversary
Obviously I dont want user to see this I just want to add something like "Thank you we will get back to you soon"
Any idea how I can modify the above script for us to get inquiry form but also for user to get an confirmation email?
Any help greatly appreciated
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$eventType = $_POST['eventType'];
$nrGuests = $_POST['nrGuests'];
$state = $_POST['state'];
$suburb = $_POST['suburb'];
$msg = $_POST['msg'];
$refferal_page = $_POST['page'];
$ip = $_SERVER['REMOTE_ADDR'];
if(!isset($refferal_page)){
$refferal_page="Source Unknown";
}
$to_email = "unknown#yahoo.co.nz"; //Recipient email
//TODO Sanitize input data using PHP filter_var().
$suburb = filter_var($_POST["suburb"], FILTER_SANITIZE_STRING);
$subject = "You Have a New Booking!";
//Add booking INFO here;
$message = "A new Enquiry has been received from http://www.xxxxx.com.au. Please find details below and follow up:";
//email body
$message_body = $message."\r\n\r\n".$name."\r\nEmail : ".$email."\r\nPhone Number : ". $mobile."\r\nEvent Type: ".$eventType."\r\nNumber Guests:".$nrGuests."\r\nState: ".$state."\r\nSuburb:".$suburb."\r\nIP ADDRESS:".$ip."\r\nMessage:".$msg."\r\nReferal Page Source:".$refferal_page;
//proceed with PHP email.
$headers = 'From: '.$name.'' . "\r\n" .
'Reply-To: '.$email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$send_mail = mail($to_email, $subject, $message_body, $headers);
if($send_mail)
{
echo '<h1 class="page-header">Enquiry Successfully submitted</h1>';
$EmailTo = $email;
$subject = "recipient subject";
$message_body = "Hi ".$name."recipient message";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: yourname<$youremail>\n";
$send_cust_mail = mail($to_email, $subject, $message_body, $headers);
}
else if(!$send_mail){
echo'<h1 class="page-header" style="color:RED">OOPS...SOMETHING WENT WRONG, PLEASE TRY AGAIN!</h1>';
}
}//server request method
else{
echo'<h1 style="color:red">SUBMIT A FORM PLEASE!</h1>';
}
?>
I am sending email using php class. It's successfully send a email message but in my Gmail email account it's showing me a empty message. I don't understand why it's showing me empty message content. It's showy unknown sender and a empty message body. Here is the pictures what is shwoing in my Gmail account. Can you please help me about it.
Your help is much more appreciate :)
and
Here is my full code which I'm using to send email using php.
<?php
$name="";
$from="";
$message="";
if(isset($_POST['submit'])){
$name=mysql_real_escape_string($_POST['name']);
$from=mysql_real_escape_string($_POST['email']);
$message=mysql_real_escape_string($_POST['comments']);
$to="myemail#gmail.com"; // Add your e-mail here
include("library/send_email.php");
}
?>
send_emaiil.php page
<?php
require_once("class_email_sender.php");
$send_email= new class_email_sender($name,$from,$to,$message);
$send_email->send_email();
?>
class_email_sender.php page
<?php
class class_email_sender{
private $name;
private $from;
private $to;
private $to_email;
private $subject;
private $headers;
private $mail_message;
public function __construct($name,$from,$to_email,$mail_message){
$this->name=$name;
$this->from=$from;
$this->to_email=$to_email;
$this->mail_message=$mail_message;
}
public function send_email(){
$this->to=$this->to_email;
//$this->headers="From : $this->from<$this->from>";
$this->headers = 'From:'.$this->from. "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$this->subject="Contact Message From your site";
$this->message="Name :" .$this->name. "\n";
$this->message.="Email :" .$this->from."\n\n";
$this->message.="Message :" . $this->mail_message."\n\n";
mail($this->to_email,$this->subject,$this->message,$this->headers);
echo '<script>alert("Thanks for your mail.We will notify you shortly")
</script>';
}
}
?>
for example here is the code which worked for me
<?php
$username = $_POST["username"]; //Email address you want it to 'appear' to come
$email = $_POST["email"];
if(strlen($username) && strlen($email))
{
$mailText ="The Contact Details: <br>";
}
$mailText=$mailText."<table border=1 cellspacing=0 cellpadding=0>";
while(list($Key, $Val)= each($_POST))
{
$mailText=$mailText."<tr><td width=50%>";
$mailText=$mailText."<b>".$Key."</b></td>";
$mailText=$mailText."<td width=50%>";
$mailText=$mailText.$Val;
$mailText=$mailText."</td></tr>";
}
$to = "youremailid.com";
if(strlen($username) && strlen($email))
{
$subject = "your subject"; //Subject Line
$headers .= "From:yourmail.com\n";
$headers .= "X-Sender: yourheader.com\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n"; // Mime type
$mailforms = mail($to, $subject, $mailText, $headers);
}
?>
try to keep like this acc to ur code and check
I want to be able to put the name of the person in the email. But I don't know how to retain the value outside the loop. Also, I'm wondering if this will work if there is more than one result.
Here is the php:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
}
if (!empty($mail_to)){
sendEmail($mail_to);
}
}
// SEND EMAIL
function sendEmail($mail_to) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
In that example above, the mail never reads their name, as the value seems to be forgotten outside of the while loop. Can someone help me?
Currently, your code overwrites the email id stored in $mail_to in each iteration of the while loop. So, it will have only the email id of the last user. And you need name of each user (stored in $name and used in the function). You need to send the email whenever a user is found. Try this:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'];
$name = $row['name'];
if (!empty($mail_to))
sendEmail($mail_to, $name);
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
To solve your problem you need to put the send mail function inside the loop and add a name parameter in the function since you are using it inside the function.
Something like this might do the trick:
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
$name = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
if (!empty($mail_to) and !empty($name)){
sendEmail($mail_to, $name);
}
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
If you use the phpmailer, you could do some like:
function joinDateFilter(){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "webmaster#mydomain.com";
$mail->Subject = "Congratulations";
$mail->Body = "Happy Anniversary!";
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
$mail->AddBCC($row['email'],$row['name']);
}
$mail->Send();
$mail->ClearAddresses();
}
Or if you want to have diffrent Subjects than you could do this...
function joinDateFilter(){
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "webmaster#mydomain.com";
$mail->Body = "Happy Anniversary!";
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
while ($row = mysql_fetch_array($query)){
$mail->Subject = "Congratulations " . $row['name'];
$mail->AddAddress($row['email'],$row['name']);
$mail->Send();
$mail->ClearAddresses();
}
}
Anyway, you save 1 function and its kind of handy to :D
And its also very easy to add attachments or sending html emails and stuff like this. How to do most things can be googled. So you may need a small tutorial.
If you download the mailer pac, you really only need the class.phpmailer.php the rest is not necessary :)
This should work:
<?php
// WHO HAS BEEN A MEMBER FOR ONE YEAR
function joinDateFilter(){
$query = mysql_query("SELECT * FROM user WHERE date_joined = DATE_SUB(curdate(), INTERVAL 1 YEAR)");
$mail_to = "";
while ($row = mysql_fetch_array($query)){
echo $row['name']." - ".$row['email']."\n";
$mail_to = $row['email'].", ";
$name = $row['name'];
if (!empty($mail_to)){
sendEmail($mail_to, $name);
}
}
}
// SEND EMAIL
function sendEmail($mail_to, $name) {
$from = "webmaster#mydomain.com";
$message = "Happy Anniversary!";
$headers = 'From: '.$from."\r\n" .
'Reply-To:'.$_POST['email']."\r\n" .
"Content-Type: text/html; charset=iso-8859-1\n".
'X-Mailer: PHP/' . phpversion();
mail($mail_to, "Congratulations ".$name."!", $message, $headers);
}
?>
Keep in mind that mail function can be sometimes problematic. Google about that issue. What you can use as an replacement for the mail function is SwiftMailer