Getting empty email message using PHP - php

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

Related

Why tags in contact form are not working?

On my webpage, I have a contact form, which sends messages directly to my mail. Unfortunately, I receive everything in one line, tags are not working and also when I want to reply to that mail, instead of mail there is:
To:my#mail.comrnReply-TornContent-Typetext/html
To: PHP/7.2.7 <>
I am totally lost, where should be a problem. Does anyone know what's going on, please?
session_start();
if (!empty($_POST)) {
if ( (isset($_POST['email'])) && (strlen($_POST['message']) > 5) ) {
$receiver = "my#mail.com";
$to = $receiver;
$subject = 'New message - mypage.com';
$message = 'New e-mail sent by contact form, from mypage.com.<br> Contact: ' . $_POST['email'] . '<br>Text :<br> ' . $_POST['message'] . '';
$headers = 'From: ' .$receiver."rn" .
'Reply-To: ' .$email."rn" .
"Content-Type: text/html; charset=UTF-8" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
$warning = '<div class="sent"><p class="success">Your message was successfully sent. Thanks for your time.</p></div>';
$_SESSION['hlaska'] = $warning;
} else {
$warning = '<p class="error">Unexpected error.</p>';
}
} else {
$warning = '<p class="error">You did not fill all fields.</p>';
}
}

PHP Confirmation Email after filling out Contact Form

I've created a contact page for a project im working on and im having trouble getting it to send a confirmation email to the user. I have the function defined on my emailer page along with the code that sends the contact form to me
public function sendEmail(){ //this will format and send an email to the smtp server
$to = $this->getSenderEmail();
$subject = $this->getSubject();
$message = $this->getMessage();
$headers = "From: <contact#tristonnearmyer.com >";
$from = $this->getCustomerInfo();
//it will use the php mail()
return mail($to,$subject,$message,$from);
}
public function sendConfEmail(){
$userEmail = $this->getRecipientEmail();
$confSubject = "confirmation";
$confMessage = "thank you";
return mail($userEmail, $confSubject, $confMessage);
}
then the functions are used on the contact page
echo $emailTest->sendEmail(); //send email to SMTP server
echo $emailTest->sendConfEmail(); //send confirmation email
I can't seem to figure out why one is working while the other isn't
Please try this code, you need to call the function sendConfEmail after sendEmail success. Also from here you need to modify the header. But base on user3783243 commend better change to PHPMailer or use swift
public function sendEmail(){ //this will format and send an email to the smtp server
$to = $this->getSenderEmail();
$subject = $this->getSubject();
$message = $this->getMessage();
$from = $this->getCustomerInfo();
$headers = 'From: '. $from . "\r\n" .
'Reply-To: youremailhere#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sendMail = mail($to,$subject,$message,$headers);
if($sendMail){
// Call the function send confirmation
$this->sendConfEmail();
}else{
echo 'failed sent the email';
}
}
public function sendConfEmail(){
$userEmail = $this->getRecipientEmail();
$confSubject = "confirmation";
$confMessage = "thank you";
$from = $this->getCustomerInfo();
$headers = 'From: '. $from . "\r\n" .
'Reply-To: youremailhere#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
return mail($userEmail, $confSubject, $confMessage,$headers);
}

php send mail to client and business

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>';
}
?>

PHP - need help in php form element (Header part)

I am a beginner in php.
I am trying php to send mail.
My mail goes to mail client but it does not show the header part well as I wanted.
I am using the following codes ----
<?php
//validation expected data exists
if(!isset($_POST["name"]) ||
!isset($_POST["city"]) ||
!isset($_POST["email"]) ||
!isset($_POST["phone"]) ||
!isset($_POST["message"])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
//Subject
$name = $_POST["name"];
$subject = "NO REPLY";
$email = $_POST["city"];
$phone = $_POST["email"];
$website = $_POST["phone"];
$message = $_POST["message"];
$header = "from: $name <$email>";
$to = 'info#mishmihillsadventure.in';
$send_contact=mail($to,$subject,$message,$header);
//Check, if message sent to your email
// Display message "We've recived your information"
if($send_contact){
echo "We've received your contact information";
}
else{
echo "ERROR";
}
?>
$email = $_POST["city"];
$phone = $_POST["email"];
Is this really what you want? Shouldn't it be:
$email = $_POST["email"];
And try the following headers:
$header = 'From: ' . $name . '<' . $email . '>' . "\r\n";
Use (atmost) following headers while sending mail via PHP -
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header .= "From: $name <$email>" . "\r\n";
//If want to `CC` someone add
$header .= 'Cc: abc#email.com' . "\r\n";
Using variables in Double quotes is fine.
You can try something like in code mentioned below,
<?php
$to = 'test#to.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: test#from.com' . "\r\n" .
'Reply-To: test#from.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo 'Msg send..';
} else {
echo 'Msg Not send..';
}
?>

PHP Send-Mail form to multiple email addresses

I'm very new to PHP and am using a basic template 'send-mail' form on a contact page.
It's been requested that I send the email out to multiple email addresses when the "Submit" button is clicked. I've searched around & haven't quite found what I needed. What code do I need to add into the form below in order to send this out to multiple email addresses?
<?php
$mail_to = 'daniel30293#gmail.com'; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$mail_from = $_POST['sender_email'];
$phone = $_POST['sender_phone'];
$web = $_POST['sender_web'];
$company = $_POST['sender_company'];
$addy = $_POST['sender_addy'];
$message = $_POST['sender_message'];
// Construct email subject
$subject = 'Web Prayer Request from ' . $name;
// Construct email body
$body_message = 'From: ' . $name . "\r\n";
$body_message .= 'E-mail: ' . $mail_from . "\r\n";
$body_message .= 'Phone: ' . $phone . "\r\n";
$body_message .= 'Prayer Request: ' . $message;
// Construct email headers
$headers = 'From: ' . $name . "\r\n";
$headers .= 'Reply-To: ' . $mail_from . "\r\n";
$mail_sent = mail($mail_to, $subject, $body_message, $headers);
if ($mail_sent == true){ ?>
<script language="javascript" type="text/javascript">
alert('Your prayer request has been submitted - thank you.');
window.location = 'prayer-request.php';
</script>
<?php } else { ?>
<script language="javascript" type="text/javascript">
alert('Message not sent. Please, notify the site administrator admin#bondofperfection.com');
window.location = 'prayer-request.php';
</script>
<?php
}
?>
Your help is greatly appreciated.
You implode an array of recipients:
$recipients = array('jack#gmail.com', 'jill#gmail.com');
mail(implode(',', $recipients), $submit, $message, $headers);
See the PHP: Mail function reference - http://php.net/manual/en/function.mail.php
Receiver, or receivers of the mail.
The formatting of this string must comply with ยป RFC 2822. Some examples are:
user#example.com
user#example.com, anotheruser#example.com
User <user#example.com>
User <user#example.com>, Another User <anotheruser#example.com>
Just add multiple recipients comma seperated in your $mail_to variable like so:
$mail_to = 'nobody#example.com,anotheruser#example.com,yetanotheruser#example.com';
See
mail() function in PHP
Here is a simple example:
<?php
// Has the form been submitted?
// formSubmit: <input type="submit" name="formSubmit">
if (isset($_POST['formSubmit'])) {
// Set some variables
$required_fields = array('name', 'email');
$errors = array();
$success_message = "Congrats! Your message has been sent successfully!";
$sendmail_error_message = "Oops! Something has gone wrong, please try later.";
// Cool the form has been submitted! Let's loop through the required fields and check
// if they meet our condition(s)
foreach ($required_fields as $fieldName) {
// If the current field in the loop is NOT part of the form submission -OR-
// if the current field in the loop is empty, then...
if (!isset($_POST[$fieldName]) || empty($_POST[$fieldName])) {
// add a reference to the errors array, indicating that these conditions have failed
$errors[$fieldName] = "The {$fieldName} is required!";
}
}
// Proceed if there aren't any errors
if (empty($errors)) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES, 'UTF-8' );
$email = htmlspecialchars(trim($_POST['email']), ENT_QUOTES, 'UTF-8' );
// Email Sender Settings
$to_emails = "anonymous1#example.com, anonymous2#example.com";
$subject = 'Web Prayer Request from ' . $name;
$message = "From: {$name}";
$message .= "Email: {$email}";
$headers = "From: {$name}\r\n";
$headers .= "Reply-To: {$email}\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if (mail($to_emails, $subject, $message, $headers)) {
echo $success_message;
} else {
echo $sendmail_error_message;
}
} else {
foreach($errors as $invalid_field_msg) {
echo "<p>{$invalid_field_msg}</p>";
}
}
}

Categories