PHP send mail with question marks although using utf-8 - php

Using the following php file to send mail.
But the text that is not English inside the mail body get as ???? and the $subject of the mail get as "‚º ëÃ"
what do I need to check/add/change in order to fix it?
<?php include('config.php');
header('Content-Type: text/html; charset=utf-8');
class messenger
{
public $name;
public $number;
public $email;
public $address;
public $category;
public $tool;
/*----------user registration start--------------*/
function signup()
{
$name = $_REQUEST['name'];
$number = $_REQUEST['number'];
$email = $_REQUEST['email'];
$address = $_REQUEST['address'];
$category = $_REQUEST['category'];
$tool = $_REQUEST['tool'];
$datetime = date("Y-m-d H:i:s");
//Generate the email message:
$to = 'someEmailAddress#gmail.com';
$subject = "הזמנת חדשה";
$msg = "In time: " . $datetime . "\n New Power Tool Rental: \n\n Name: " . $name . "\r\n Email: " . $email . "\n Mobile Number: " . $number . "\n Address: " . $address . "\n Category: " . $category . "\n Tool: " . $tool . "\n\n Regards\n " . $name . " \n " . $email;
$headers = "From: $email" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8";
$headers .= "X-Mailer: PHP/" . phpversion();
//Send the email
mail($to, $subject, $msg, $headers);
//Enter data to the SQL table
$sql = "INSERT INTO user (name, number, email, address, category, tool, dateTime)
VALUES ('$name', '$number', '$email', '$address', '$category', '$tool','$datetime')";
//Check if entered to SQL and if Yes send back in json message
$result = mysql_query($sql);
if (!$result) {
$message["result"] = "Problemmm";
} else {
$mysql_insert_id = mysql_insert_id(); //get the new ID number from sql table
$message["id"] = $mysql_insert_id; //add the new ID data to the response message
$message["name"] = $_REQUEST['name'];
$message["number"] = $_REQUEST['number'];
$message["email"] = $_REQUEST['email'];
$message["address"] = $_REQUEST['address'];
$message["result"] = "successfully";
}
//Send back the json message
echo json_encode($message);
die;
}
/*----------user registration end--------------*/
}
?>
just tried to change the subject to:
$subject = "הזמנת כלי להשכרה";
$encSubject = '=?UTF-8?B?'.base64_encode($subject).'?=';
now I get the subject of the mail as:
���ú ��é ��

Setting the headers is not enough.
Both subject (must be RFC 2047) and
message (must be UTF-8 in your case) have also be in their respecive correct encoding, the php::mail doc + user-notes covers most of what you miss.

Related

Data is not storing in mysql database table from PHP site

I want to store that value in a database after computing the domain. I have done everything right as per my knowledge but data is not being sent to the table.
Note : My mysql server is in AWS and I've opened the mysql port
My code is below,
<?php
$con = mysqli_connect("localhost","user_name","passwd","db_name");
$sql = "INSERT INTO contact_form (name, mobile, email, message) VALUES ('".$_POST['name']."', '".$_POST['phone']."', '".$_POST['email']."', '".$_POST['message']."')";
if (mysqli_query($con, $sql)) {
echo "New record created successfully";
$to = 'contact#xxx.com';
$subject = 'Contact Form';
$from = 'contact#xxx.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();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi!</h1>';
$message .= '<p>Name :'. $_POST['name'] .' </p>';
$message .= '<p>Contact Number :'.$_POST['phone'] .'</p>';$message .= '<p>Email :'.$_POST['email'] .'</p>';
$message .= '<p>Message :'.$_POST['message'] .'</p>';
$message .= '</body></html>';
// Sending email
mail($to, $subject, $message, $headers);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
?>
Please help me out. I'm not good in mysql. Thanks in advance !!
Ahamed, first of all you need to check if you have set your $_POST and it is not empty. Then you need to assign this data to custom variables.
I hope it helps.
$con = mysqli_connect("localhost","user_name","passwd","db_name");
if (isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['email']) && isset($_POST['message'])){
$username = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$sql = "INSERT INTO contact_form (name, mobile, email, message) VALUES ('$username', '$phone', '$email', '$message')";
$result = $con->query($sql);
}

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

Contact form php script sending spam to Gmail

I created a php script (for a contact form) to send emails to my Gmail account.
If I use the sender email in the header ($headers = "From: " . $email;), Gmail reports the received message as spam.
If I don't use the email in the header (e.g. the sender name $headers = "From: " . $name;) the message is not reported as spam.
Do you have any suggestion to let me use the email in the header?
Thanks!
<?php
/* Check if the url field is empty (antispam) */
if ($_POST['leaveblank'] != '' or $_POST['dontchange'] != 'http://') {
$name = $_POST['name'];
$faillink = "xxx.php";
header("Location: $faillink");
} else {
$name = $_POST['name'];
$email = $_POST['email'];
$subject_prefix = "[ContactForm]: ";
$subject = $subject_prefix . $_POST['subject'];
$message = $_POST['message'];
$to = "myemail#gmail.com";
$body = "From: " . $name . "\n";
$body .= "Email: " . $email . "\n";
$body .= "Message: " . $message . "\n";
$headers = "From: " . $email;
$oklink = "yyy.php";
$faillink = "xxx.php";
if ( preg_match( "/[\r\n]/", $name ) || preg_match( "/[\r\n]/", $email ) ) {
header("Location: $faillink");
}
$retmail = mail($to, $subject, $body, $headers);
if ($retmail) {
header("Location: $oklink");
} else {
header("Location: $faillink");
}
}
?>
I solved the issue as Iain suggested so I replaced the mail headers as follows:
$headers = "From: " . "noreplay#mydomain.com" . "\r\n";
$headres .= "Reply-To: " . $email . "\r\n";

Why won't my PHP mail() function send linebreaks?

Here's the script:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$recipient = "me#christianselig.com";
$subject = "Message From Website";
$body = "[From: " . $name . "]\r\n\r\n" . $message;
$headers = "From: " . $email . "\r\n";
$headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
$success = mail($recipient, $subject, $body, $headers);
echo $success;
?>
On this page: christianselig.com/contact.html
My message will send without any linebreaks. I have it set to separate the [From: xxx] section from the message with a double linebreak, so it should look like this:
[From: John]
Hey.
But it doesn't.
If I make the message multiple lines long, it also concatenates them on one line. How do I prevent this behaviour? My code used to allow it, but I broke it somehow...
Because you're sending it with an html formatting. Try:
$body = nl2br("[From: " . $name . "]\r\n\r\n" . $message);

sending all data from email with require field

I'm creating a user require form in my site. For this I put some validation on compulsory fields, and when a user fills in the form and presses submit and validation is correct then I receive a email on my email address.
But now I would like all user information in the email, like name, city, budget etc... so what changes do I need to make in my email.php script?
If some fields are not compulsory and the user doesn't fill them in, can they affect my script?
My script is:
<?php
$to = "test#networkers.in";
$subject = "a new requiremnet come!";
$message = "Hi,\n\nyou get a new require";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "Sender-IP: " . $_SERVER["SERVER_ADDR"] . "\r\n";
$headers .= "From: " . stripslashes($name) . " <" . $email . ">" . "\r\n";
$headers .= "Priority: normal" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$sent = mail($to, $subject, $message, $headers) ;
if ($sent) {
echo "Your mail was sent successfully";
} else {
echo "We encountered an error sending your mail";
}
?>
and the data I recieve is:
$name = $_POST['fname'].' '.$_POST['lname'];
$email = $_POST['mail'];
$phone = $_POST['ph'];
$country = $_POST['country'];
$pt = $_POST['pt'];
$cwl = $_POST['cwl'];
$dyhyows = $_POST['dyhyows'];
$pb = $_POST['pb'];
$bpd = $_POST['bpd'];
$hdyhau = $_POST['hdyhau'];
You can add fields to the message body by concatenating them like so:
$message = "Hi,\n\nyou get a new require";
$message .= "\n Name: " . $name;
$message .= "\n Email: " . $email;
$message .= "\n Phone: " . $phone;
$message .= "\n Country: " . $country;
$message .= "\n pt: " . $pt;
$message .= "\n cwl: " . $cwl;
$message .= "\n dyhyows: " . $dyhyows;
$message .= "\n pb: " . $pb;
$message .= "\n bpd: " . $bpd;
$message .= "\n hdyhau: " . $hdyhau
Any fields that weren’t filled in by the user will simply be blank.

Categories