PHP: Mail not sent - php

I am trying on the following code but it keeps saying Mail not sent. How do I find the real issue? Code given below:
$full_name = htmlspecialchars(trim($_POST['full_name']));
$email = htmlspecialchars(trim($_POST['email']));
$phone = htmlspecialchars(trim($_POST['phone']));
$message = htmlspecialchars(trim($_POST['message']));
$to = "mail#example.com";
$subject = $full_name . ' is interested to have a business discussion with you';
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['email']) . "\r\n";
// $headers .= "CC: susan#example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h3>Full Name: </h3>: ' . $full_name . '<br>';
$message .= '<h3>Phone: </h3>: ' . $phone . '<br>';
$message .= '<h3>Message: </h3>: ' . $message . '<br><br>=======================<br>';
$message .= '<h3>IP Address: </h3>: ' . $ip . '<br>';
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers))
{
echo "Mail Sent Successfully";
}else{
echo " Mail Not Sent";
}

Try this code hope this helpful.
<?php
//print_r($_POST);
//$fname = $_POST['fname'];
//$lname = $_POST['lname'];
//$email = $_POST['email'];
//$message = $_POST['message'];
if(isset($_POST['fname']) && $_POST['fname'] != ''){
$fname = $_POST['fname'];
}
if(isset($_POST['lname']) && $_POST['lname'] != ''){
$lname = $_POST['lname'];//phone number
}
if(isset($_POST['email']) && $_POST['email'] != ''){
$email = $_POST['email'];
}
if(isset($_POST['message']) && $_POST['message'] != ''){
$com = $_POST['message'];
}
$to = 'noreply#noreply.com';
$subject = 'Site Visiter.';
// message
$message = sprintf("Site visiter details: <br>Name:- %s <br> Phone:- %s <br> Email:- %s<br> Message:- %s",$fname,$lname,$email,$com);
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <nb158f#gmail.com>' . "\r\n";
$headers .= 'From: mysite.com <admin#site.com>' . "\r\n";
//$headers .= 'Cc: divakar.k#gmail.com' . "\r\n";
// Mail it
$flag = mail($to, $subject, $message, $headers);
echo '<script>alert("Mail Sent :");</script>';
echo '<script>window.location.href="index.php";</script>';

Related

Php form handler/formatting of email message lost

I've got a form on my website and when I added htmlspecialchars function to the php form handler, it seemed I also had to change the character encoding to utf-8 so that letters with accents would come through. Now that I've added $headers, the characters are shown correctly but the formatting of the email message ($message) is lost with no line breaks. This is what my code looks like:
$surname = htmlspecialchars($_POST["surname"], ENT_COMPAT, 'UTF-8');
$firstname = htmlspecialchars($_POST["firstname"], ENT_COMPAT, 'UTF-8');
$address = htmlspecialchars($_POST["address"], ENT_COMPAT, 'UTF-8');
$age = htmlspecialchars($_POST["age"], ENT_COMPAT, 'UTF-8');
$message = "
Website form:
Name: " . $firstname . " " . $surname . "
Address: " . $address . "
Age: " . $age . ";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
if (mail("me#me.com", $_POST['firstname'] ." ". $_POST['surname'], $message, $headers)) {
header("Location: ...");
}
I tried to fix the issue with
$message = "Website form\n";
$message .= "Name: " . $firstname . " " . $surname . "\n";
$message .= "Address: " . $address . "\n";
$message .= "Age: " . $age . "\n";
but the problem persists. I also tried to change content-type to text/plain but the email message appeared as an attachment file. I am at a loss and don't know what I'm doing wrong as the formatting is still lost. I'm a beginner with php so any help will be appreciated.
You should try message format like this:
// email message
$message ="";
$message .= 'Website Form:'."\r\n";
$message .= 'Name:'. $firstname ." ". $surname . "\r\n";
$message .= 'Address:'. $address . "\r\n";
$message .= 'Age:'. $age . "\r\n";
Here is complete code:
<?php
$surname = htmlspecialchars($_POST["surname"], ENT_COMPAT, 'UTF-8');
$firstname = htmlspecialchars($_POST["firstname"], ENT_COMPAT, 'UTF-8');
$address = htmlspecialchars($_POST["address"], ENT_COMPAT, 'UTF-8');
$age = htmlspecialchars($_POST["age"], ENT_COMPAT, 'UTF-8');
$to = 'youemail#email.com';
$subject = 'Subject Line';
$from = 'info#example.com';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// email message
$message = "";
$message .= 'Website Form:'."\r\n";
$message .= 'Name:'. $firstname ." ". $surname . "\r\n";
$message .= 'Address:'. $address . "\r\n";
$message .= 'Age:'. $age . "\r\n";
// Sending email
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
echo 'Unable to send email. Please try again.';
}
?>

My server's name shows up as submitter's Reply-to email address from PHP contact Form

I'm not versed in PHP and had this form built for me. It works 95% of the time, but sometimes the reply-to email will be johnsmith#hadrian.lunarpages.com
I'm reading up a similar issue on this page, but not exactly sure what to change in my form...
<?php
if (isset($_REQUEST['btnSubmit-group'])) {
$date = $_REQUEST['date'];
$time = $_REQUEST['time'];
$count = $_REQUEST['count'];
$name = $_REQUEST['name'];
$organization = $_REQUEST['organization'];
$email = $_REQUEST['email'];
$grouptype = $_REQUEST['grouptype'];
$phone = $_REQUEST['phone'];
$upgrademus = $_REQUEST['upgrademus'];
$upgradeowo = $_REQUEST['upgradeowo'];
$comments = $_REQUEST['comments'];
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
if ($count == "" || $name == "" || $email == "" || $phone == "") {
echo "All fields are required, please fill out again. ";
} else {
$to = 'info#example.com';
$subject = "Group Tour Inquiry from $name ($date, $count people)";
$from = $to;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = nl2br("Private Tour:<br>" .
"<b>Name:</b> $name\r\n" .
"<b>Email:</b> $email \r\n" .
"<b>Phone:</b> $phone \r\n" .
"<b>Organization:</b> $organization\r\n" .
"<b>Date:</b> $date\r\n" .
"<b>Time:</b> $Time\r\n" .
"<b>Count:</b> $count\r\n" .
"<b>Include:</b> $upgrademus\r\n" .
"<b>Include:</b> $upgradeowo\r\n" .
"<b>Comments:</b> $comments\r\n" .
"");
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name<$email> \r\n";
$headers .= "Reply-To: $email \r\n";
if (mail($to, $subject, $body, $headers)) {
echo "<div class=\"thankyou\"></div><meta http-equiv='refresh' content='0;url=http://www.example.com/private-group-tours'>";
}
}
} else {} ?>
You're trying to send e-mail's to your self FROM the person whose entering this in, essentially attempting to spoof their email, and sometimes this will(should) get blocked. more can be read here - problem with php mail 'From' header
Change:
$headers .= "From: $name<$email> \r\n";
$headers .= "Reply-To: $email \r\n";
to
$from = 'Admin#mysite.com';
$headers .= "From: $from\r\n";
$headers .= "Reply-To: $from \r\n";
and then edit that static $from variable. Then take the users email out of the BODY of the email you receive, and send them an email.

Include Variables in PHP mail command

I'm sure I'm wrong about something basic, but can not find where the error is. I will appreciate your help.
I have a php page with a single 4 fields contact form.
I need send the name and email as variable, so i can see it properly when email is received.
IT WORKS - BUT DONT SEND NAME AND EMAIL AS VARIABLE:
All messages arrives as "no-reply#luchodomain.com" and doesnt look professional (and can't do reply from my outlook)
if ($_POST) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "lucho#hotmail.com";
$subject = "Website CONTACT";
$headers = "From: no-reply#luchodomain.com" . "\r\n" ;
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Subject:</strong> </td><td>" . strip_tags($_POST['subject']) . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['message']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
mail($to,$subject,$message,$headers);
}
IT DOENST WORKS (but have the format and variables as i need)
I would like receive the messages like is normal Name, so i can do a quick reply. -This code don't send email, doesn't work-
if ($_POST) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "lucho#hotmail.com";
$subject = "Website CONTACT";
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>";
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Subject:</strong> </td><td>" . strip_tags($_POST['subject']) . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . strip_tags($_POST['message']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
mail($to,$subject,$message,$headers);
}
I will appreciate your help with this "headers" issue!
Thank you.
Make sure that you are receiving data in your post array. Make sure your form action. You have declared $message variable to blank after assigning the post value in it before. That's not a good trick enough. Please check updated code and let me know.
if ($_POST) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$subject = strip_tags($_POST['subject']);
$message = strip_tags($_POST['message']);
$to = "lucho#hotmail.com";
$subject = "Website CONTACT";
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
//$message = "";
//$message .= '';
$message .= "\r\nName: " . $name . "";
$message .= "Email: " . $email . "\r\n";
$message .= "Subject: " . $subject . "\r\n";
$message .= "Message: " . $message . "";
//$message .= "";
//$message .= "";
mail($to,$subject,$message,$headers);
}
$to = 'pawanvikasitha2001#gmail.com, aravindasamapath87#gmail.com'; $subject = 'NEW ORDER HAS PLACED'; $message = "$name"; mail ($to,$subject,$message);
When you use the double quotes, You can simply add variables to it. Look at the $message

Sending email headers

I have such code for send email:
$email = $_POST['message_email'];
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
before that I check whether the email is correct so it has to be.
Then I send an email:
$sent = wp_mail($newTo, $subject, $companyName . "\n" . $email . "\n" . $phoneNumber . "\n" . strip_tags($message) . "\n" . $outputMail, $headers);
and it doesn't work. I've tried change headers to:
$headers = 'From: My Name <myname#mydomain.com>' . "\r\n\\";
and then it works. Why my code does't work? I need to provide that email retreived from form.
Try like this:
****Ive edited the code****
$from = "your#mail.com";
$to = "to#mail.com";
$message = "Message";
$subject = 'subject';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "From: ".$from."\r\n";
$headers .= "MIME-Version: 1.0\r\n";
if(mail($from, $subject, $message, $headers)){
//success
}else{
//error
}

Why is PHP's mail() sending me dozens of blank emails?

I'm using PHP's mail() function within a WordPress theme to send form submissions to my email.
However, mail() has automatically sent me at least 20 blank emails,
within the last five minutes, without being submitted.
Could someone shed some light on what I've done to create this situation?
Here is the code from my (functions.php file)
function send_my_form(){
$form = array();
$form['fstname'] = $_POST['fstname'];
$form['lstname'] = $_POST['lstname'];
$form['email'] = $_POST['email'];
$form['message'] = $_POST['message'];
$send_to = 'fakeemail#gmail.com';
$subject = 'You\'ve recieved an email from' . $form['fstname'] . $form['fstname'];
$return = "-f" . $send_to;
$message = "First Name: " . $form['fstname'] . "\r\n";
$message .= "Last Name: " . $form['lstname'] . "\r\n";
$message .= "Email: " . $form['email'] . "\r\n";
$message .= "Message: " . $form['message'] . "\r\n";
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$headers .= 'From: ' . $send_to . '\r\n';
$headers .= 'Reply-To: ' . $form['email'] . '\r\n';
$headers .= 'Return-Path: ' . $send_to . '\r\n';
$headers .= '\r\nX-Mailer: PHP/' . phpversion();
mail($send_to, $subject, $message, $headers, $return);
}
add_action('wp_head', 'send_my_form');
This is what I mean by running it in a conditional. Verify the fields are set before running the mail script.
function send_my_form(){
if(isset($_POST['fstname']) && isset($_POST['lstname']) && isset($_POST['email']) && isset($_POST['message']))
{
$form = array();
$form['fstname'] = $_POST['fstname'];
$form['lstname'] = $_POST['lstname'];
$form['email'] = $_POST['email'];
$form['message'] = $_POST['message'];
$send_to = 'fakeemail#gmail.com';
$subject = 'You\'ve recieved an email from' . $form['fstname'] . $form['fstname'];
$return = "-f" . $send_to;
$message = "First Name: " . $form['fstname'] . "\r\n";
$message .= "Last Name: " . $form['lstname'] . "\r\n";
$message .= "Email: " . $form['email'] . "\r\n";
$message .= "Message: " . $form['message'] . "\r\n";
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type: text/html; charset=iso-8859-1' . '\r\n';
$headers .= 'From: ' . $send_to . '\r\n';
$headers .= 'Reply-To: ' . $form['email'] . '\r\n';
$headers .= 'Return-Path: ' . $send_to . '\r\n';
$headers .= '\r\nX-Mailer: PHP/' . phpversion();
mail($send_to, $subject, $message, $headers, $return);
}
}

Categories