php mail() sending two different messages on submit to different parties - php

I modified the following script. Everything seems to work except the person who submits the form('to2') gets an email with ('message2' - with single space lines) followed by ('message' - with double space lines).
The recipients of ('message') works as it should in that they only get ('message' - with single space lines).
MY OBJECTIVE is for ('to2') only to receive ('message2') without being followed by ('message'). I tried positioning mail('message2') string in different locations of the scripts' logic, but, I keep getting the same results or broken. Any help with logic is appreciated.
Here is the script...
<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=500">
<title>EXAMPLE</title>
</head>
<body>
<?php
if(isset($_POST['Submit'])) {
$ip_address = $_SERVER['REMOTE_ADDR'];
$Subject = 'TEST';
$Subject2 = 'SUCCESS';
$A1 = $_POST['A1'];
$Name = $_POST['Name'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$message = $_POST['message'];
$message2 = $_POST['message2'];
// Type in your Email address to receive the mail
$to = 'me#example.com';
$to2 = $Name.' <'.$Email.'>';
if($Name == "" or $Phone == "" or $Email == "" ) {
echo 'One or more fields has not been filled out.<br>
Click on your browser back button once and try again.';
}
elseif(!filter_var($Email, FILTER_VALIDATE_EMAIL)) {
echo 'The Email address could not be validated.<br>
Click on your browser back button once and verify your Email address.';
}
else { // All checks passed
$headers = "From: ".$Name." <".$Email.">\r\n";
$headers .= "Bcc: [VENDOREMAILS]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers2 = "From: Support <me#example.com>\r\n";
$headers2 .= "MIME-Version: 1.0\r\n";
$headers2 .= "Content-Type: text/html; charset=utf-8\r\n";
$message = "
Subject: $Subject<br>
Question: $A1<br>
Name: $Name<br>
Phone: $Phone<br>
Email: $Email
";
$message2 = "
Subject: $Subject2<br>
Question: $A1<br>
Name: $Name<br>
Phone: $Phone<br>
Email: $Email
"
.nl2br($message);
$sendMail = mail($to, $Subject, $message, $headers);
if($sendMail) {
echo "THANK YOU FOR YOUR SUBMISSION";
mail($to2, $Subject2, $message2, $headers2);
}
else {
echo "An error occured and the mail could not be sent.<br>
Please try again";
}
}
}
else {
header("location:example.html");
}
?>
</body>
</html>

Remove the code
.nl2br($message);
from the end of the definition of $message2. I think that should do it.

Related

PHP email form doesnt work correctly

I have on the web email form, when I type all the informations it sends me an email, however when I type some czech characters (the site is czech) it doesnt work. Some of the characters shows as "čřžýáí" for example. I downloaded free template from some website, which I edited with my friend who knows HTML, but not PHP. I tried to find out some advice, but it says nothing to me, because I dont know almost anything about PHP and when I tried to copy some part of the code and hoped that it will worked it havent... Can you please help me?
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'xxxx#pl.cz'; // Add your email address inbetween the ''
replacing yourname#yourdomain.com - This is where the form will send a
message to.
$email_subject = "Website Contact Form: $name";
$email_body = "Nova zprava z webu.\n\n"."Udaje:\n\nJmeno: $name\nEmail:
$email_address\n\nMessage:\n$message";
$headers = "From: noreply#plantaen.cz\n"; // We recommend using something
like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
Add header to mail
Try this:
$body = '<html><head><title>Title</title></head>'."\r\n";
$body .= 'Test';
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=utf-8'."\r\n";
$headers .= 'From: YourName <"from#yoruname.com">';
mail("to#yourname.com","title",$body,$headers);
Edit :
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
echo "No arguments Provided!";
exit();
} else {
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
$email_subject = "Website Contact Form: $name";
$email_body = '<html><head><title>plantaen</title></head>'."\r\n";
$email_body .= "Nova zprava z webu.\n\n"."Udaje:\n\nJmeno: $name\nEmail: $email_address\n\nMessage:\n$message";
$headers = 'MIME-Version: 1.0'."\r\n";
$headers .= 'Content-type: text/html; charset=utf-8'."\r\n";
$headers .= 'From: plantaen <"noreply#plantaen.cz">';
mail("noreply#plantaen.cz", $email_subject, $email_body, $headers);
echo "Done!";
exit();
}

Mail() - message body with utf-8 and html tags

Contact form on site use script below. It send 2 messages - to me and to the person filling in the form. In second mail I have problem with characters like "ąźćęś"....I would like to use utf-8. I set it in header, but it doesn't work. I need to use in message body html tags also.
What wrong in the code?
<?php
// Check for empty fields
if(empty($_POST['email']) ||
empty($_POST['phone']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$email_address = $_POST['email'];
$phone = $_POST['phone'];
// Create the email and send the message
$to = 'me#test.com';
$email_subject = "Mail to you";
$email_body = "Hello\n\n"."Test\n\nEmail: $email_address\n\nPhone: $phone";
$headers = "From: test#test.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
$to2 = "$email_address";
$email_subject2 = "=?UTF-8?B?".base64_encode("Thanks for you message")."?=";
$email_body2 = "Test mail ążźćęśó\n\nBest regards\nTester";
$headers2 .= "Content-Type: text/html; charset=UTF-8";
$headers2 = 'From: Tester <test#test.com>' . "\r\n";
mail($to2,$email_subject2,$email_body2,$headers2);
return true;
?>

Php contact form keeps going to spam and has an html attachement

I experiencing a problem with a php contact form that I normal use on our products sites.
<?php
$to = "martin#puritech.co.za";
$subject = "Reverse Osmosis";
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['tel'];
$mail = $_POST['message'];
$headers = "From: $name \r\n";
$headers .= "Email: $email \r\n";
$headers .= "MIME-Version: 1.0 \r\n";
$headers .= "Content-type: text/html charset=iso-8859-1 \r\n";
$message = "<html><body style='font-family: Arial;'>";
$message .= "<h1>Hello Guys! You have a request from ".$name."</h1>";
$message .= "<p>".$mail."</p>";
$message .= "<p><span style='font-weight:bold;'>From:</span> ".$name."</p>";
$message .= "<span style='font-weight:bold;'>Email:</span> ".$email."</p>";
$message .= "<span style='font-weight:bold;'>Contact Number:</span> ".$number."</p>";
$message .= "</body></html>";
$send = mail ($to, $subject, $message, $headers);
if ($send==true) {
header('Location: ../index.html');
exit;
}else {?>
<script type="text/javascript">
alert('Please check your details and resend. Thank you.');
window.location = '../index.html';
</script>
<?php
}
?>
When I click on the send button, the mail goes straight to spam and has an html doc attachment.
How do I fix this? really draining me and its starting to annoy me.

Html email not sending using php

I am trying to send email in html format using php file but each time i receive email with this html code instead of html format of text ... if i move header code above message its not sending email so i put it at bottom
please help ... thank you
<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = "
<html>
<head>
<body>
<h1>
<center>Meeting invitation</center>
</h1>
</body>
</head>
</html>
";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=ISO-8859-1\r\n";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
You forget ".", for string
$headers = "From:" . $from;
must be
$headers .= "From:" . $from;
Please add this
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
Try below code..
<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = "
<html>
<head>
<body>
<h1>
<center>Meeting invitation</center>
</h1>
</body>
</head>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <gaurav#example.com>' . "\r\n";
mail($to,$subject,$message,$headers);
?>
Here is working example of mail code in PHP:
<?php
if($_GET["submit"] = "Send" )
{
$namezz = $_GET["name"];
$emailzz = $_GET["email"];
$cityzz = $_GET["city"];
$phonenozz = $_GET["phoneno"];
$subj = $_GET["subject"];
$commentszz = $_GET["msg"];
}
ini_set("SMTP","www.cccccc.in");
// email address to whoon to send
$email = "ccc#gmail.com";
// The subject
if( $subj == "No-Subject")
{
$subj = "New Enquiry form form ". $namezz;
$subject = $subj;
}
else
{
$subject = $subj;
}
// The message
$msgg = "Name :".$namezz ."\n
Email :".$emailzz ."\n
City :".$cityzz ."\n
Phone Number :".$phonenozz ."\n
Subject:".$subj ."\n
Message :".$commentszz;
$headers = "from :".$emailzz;
mail($email, $subject, $msgg, $headers);
echo 'thanks for sending email';
?>
Hope this will help you.
You could something along the lines of
<?php
$val=mail('your email address', $_POST['subject'], $_POST['message']);
?>
<p>Your email has been sent.</p>
With the html page containing
<input type="text" id="inputEmail" placeholder="Email" name="subject">
<textarea id="textarea" rows="10" name="message" placeholder="Entermsg:"></textarea>
Using the mail function in PHP is simple and straight forward
In your code you'll need to modify
$headers .= "From:" . $from;
You forgot to put a . in this piece of code
$headers = "From:" . $from;
So this is your final code
$headers .= "From:" . $from;
Because the . is used to concatenate in PHP.

PHP Contact Form not Sending?

Ive got a contact form that isnt sending but is outputting that the message is sent? Can anybody see a problem?
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#email.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Subject: $subject,
Message: $message ";
//end of message
// To send the HTML mail we need to set the Content-type header.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Website Enquiry";
if (isset($_POST['name'])) {
// now lets send the email.
mail($to, $subject, $message, $headers);
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=Thankyou, we will be in touch shortly.');
} else {
header('Location: ' . $_SERVER['HTTP_REFERER'] . '?e=There was an error sending your message, Please try again.');
}
?>
The "From" header should have a syntactically correct email address. You also need to check the return value of the "mail" function.
$header .= "From: Website Enquiry <enquiry#website.com>";
PS: Please improve your code formatting.
Try to enter an email at From: in $headers.
Like $headers .= "From: youremail#provider.com" or
$headers .= "From: Website Enquiry <youremail#provider.com>"
And you should change it to
if(mail(...)) {
//success
}
else {
//email failed
}

Categories