PHP maile utf-8 message without polish sings - php

I have problem with UTF-8 coding in mail.
I have simple code, but every time if send mail its problem with polish sings.
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$message = $_POST['message'];
$date = $_POST['date'];
$time = $_POST['time'];
$select = $_POST['select'];
$subject = $_POST['email'];
$mailTo = "moje#op.pl";
$headers = "From: ".$mailFrom;
$headers = "Content-Type: text/html; charset=UTF-8";
$body = "Dane kontaktowe: $name.\n".
"Email: $mailFrom.\n".
"Data: $date.\n".
"Godzina: $time.\n".
"Zabieg: $select.\n";
mail($mailTo, $subject, utf8_encode($body), $headers);
header("Location: reserv.php?mailsend");
}
?>

Related

How to add a cc in a php contact form?

I have a contact form for a HTML page. Right now the recipient is only one. How can I add multiple recipient. Any help on this would be a great help.
Code is below.
Thanks.
<?php
$to = "support#domain.in";
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = "Message from $name ($email)";
$replyto = $email;
$headers="";
$headers = "From: test <domain#one.com>\n";
$headers .= "Reply-To: $replyto";
$result = mail($to,$subject,$message,$headers);
?>
Here is your sample code
$to = "xyz#somedomain.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:abc#somedomain.com \r\n";
$header .= "Cc:afgh#somedomain.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
echo "Message sent successfully...";
}else {
echo "Message could not be sent...";
}

Reply-to email in PHP error

When a user inputs their email on a form on my site, I get an email with their info. I'd like to be able to reply to that email and have their email autofill in "To:" but I'm having trouble. I found this question and tried the solution: reply-to address in php contact form but it's not working for me, and I'm not sure why.
Here is my PHP:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$to = 'amanda#myemail.com';
$headers = "BCC: clients#myemail.com\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$subject = '*** Quote Request';
$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];
$message = <<<EMAIL
Quote submission from: $name
Name: $name
Phone Number: $phone
Date: $date
Time: $time
Pickup Location: $pickup
Drop Off Location: $dropoff
Total Passengers: $passengers
Service needed: $service
Email: $email
EMAIL;
if($_POST) {
mail($to, $subject, $message, $headers);
}
header('Location: thankyou.html');
exit();
?>
And this is the error message I'm getting, summed up:
Undefined variable: email in /contact-form-handler.php on line 9 Warning: Cannot modify header information - headers already sent by (output started at /contact-form-handler.php:9) in /contact-form-handler.php on line 44
The problem is the variable $email because if I put a Reply-To and specify an email, it works. I thought maybe it was because the variable is defined after I call it in the header, but adding it to the bottom didn't work. I'm a rookie with PHP so I'm not sure why this variable isn't working.
I also tried:
$headers = "BCC: clients#myemail.com\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Any help would be appreciated!
You use a variable before you define it!
Move this block to the top of your script:
$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];
Like that:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$name = $_POST['name'];
$phone = $_POST['phone'];
$date = $_POST['date'];
$time = $_POST['time'];
$pickup = $_POST['pickup'];
$dropoff = $_POST['dropoff'];
$passengers = $_POST['passengers'];
$service = $_POST['service'];
$email = $_POST['email'];
$to = 'amanda#myemail.com';
$headers = "BCC: clients#myemail.com\r\n";
$headers .= 'Reply-To: ' . $email . "\r\n";
$subject = '*** Quote Request';
$message = <<<EMAIL
Quote submission from: $name
Name: $name
Phone Number: $phone
Date: $date
Time: $time
Pickup Location: $pickup
Drop Off Location: $dropoff
Total Passengers: $passengers
Service needed: $service
Email: $email
EMAIL;
if($_POST) {
mail($to, $subject, $message, $headers);
}
header('Location: thankyou.html');
exit();
?>
seems like you don't receive $_POST['email']; from your contact form (re: "Undefined variable: email ")
check the name of your mail input field...

Adding Cc to PHP mail

So I am mailing the contents of a form to a client and he would like the person who sent the form to be Cc'd in.
I have done some research and it appears I need to use the header code to set the from, subject and cc but my code is set up differently - please see below:
<?php
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
$recipient = "email#mydomain.com";
$subject = "More information regarding $relatedproduct";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
Would it be possible to do it like this?
<?php
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = $_POST['email'];
$subject = "More information regarding $relatedproduct";
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
// Mail it
mail($to, $subject, $formcontent, $headers);
?>
php.net/mail
Yes, as you have it:
...
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthday#example.com' . "\r\n";
mail($to, $subject, $content, $headers);
?>
Incidentally, please sanitise your POST variables before you inject them (e.g. $to = $_POST['email'];).
As #Rhopercy says in their comment, perhaps an email library will help you as it takes care of most things for you. Take a look at PHPMailer or SwiftMail.
Should be like this:
$relatedproduct = $_POST['related-product'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = "From: $email\r\nReply-To: $email";
$headers .= 'Cc: test#test.com\r\n';
$headers .= 'Bcc: test#test.com\r\n';
$headers .= "Return-Path: <info#premierinspectiontn.com>\r\n";
$to = $_POST['email'];
$subject = "More information regarding $relatedproduct";
$formcontent="From: $name \n Phone: $phone \n About: $relatedproduct \n Message: $message";
// Mail it
mail($to, $subject, $formcontent, $headers);

Turkish characters to contact form

How to add the Turkish characters to this form:
in the $mailed = mail($to, '=?utf-8?B?'.base64_encode($message).'?=', $message, $headers);
I already added '=?utf-8?B?'.base64_encode which only fixed the subject. but all the body still appear something like this: ĞÜLŞÖÇI ğüşiöçı if typed ĞÜLŞÖÇI ğüşiöçı for example.
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message']) && isset($_POST['subject'])) {
$companyname = $_POST['company-name'];
$name = $_POST['name'];
$email = $_POST['email'];
$areacode = $_POST['areacode'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$state = $_POST['state'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = "";
$subject = "$subject";
$message = "Name: $name\nPhone Number: $areacode $phone\nCity: $city\nState: $state\n\n$message";
$headers = "From: $email";
$mailed = mail($to, '=?utf-8?B?'.base64_encode($message).'?=', $message, $headers);
if (isset($_POST['ajax'])) {
$response = ($mailed) ? "1" : "0";
} else {
$response = ($mailed) ? "<h2>Success!</h2>" : "<h2>Error! There was a problem with sending.</h2>";
}
echo $response;
} else {
echo "Form data error!";
}
You should also encode your message (UTF8).
You can do it using the headers parameter of the mail function.
additional_headers
String to be inserted at the end of the email header. This is
typically used to add extra headers (From, Cc, and Bcc). Multiple
extra headers should be separated with a CRLF (\r\n). Validate
parameter not to be injected unwanted headers by attackers.
Replace the next code:
$headers = "From: $email";
With:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: '.$email . "\r\n";
Should work.
Update me if not.

adding some text into message in mail

$contactname = $_POST['contactname'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = 'Ձեզ գրել են ձեր կայքից';
$to = 'stereoshoots#gmail.com';
$headers = "From: ".$email;
mail($to,$sub,$message,$headers);
I got $contactname (client name). My task is the text like this :
From : $contactname
$message (the text that client wrote).
How should i implant name into text?
You can costumise, the below, but from contact name is in this case, $from
<?php
$to = "someone#example.com";
$subject = "Ձեզ գրել են ձեր կայքից";
$message = "Hello! This is a simple email message.";
$from = "someonelse#example.com";
$headers = "From:" . $from;
if(mail($to,$subject,$message,$headers)){
echo "Mail Sent.";}
else{
echo "Mail not sent";
}
?>

Categories