contact form not working well with non English characters - php

I have the following contact form:
(HTML with PHP)
<form method="post" action="index.php">
<p>
<label>Namn</label>
<input name="name" placeholder="Skriv här">
<label>Epost adress</label>
<input name="email" type="email" placeholder="Skriv här">
<label>Meddelande</label>
<textarea name="message" placeholder="Skriv här"></textarea>
<label>Hur mycket är 2+2? (Anti-spam)</label>
<input name="human" placeholder="Skriv här">
</p>
<p>
<input id="submit" name="submit" type="submit" value="Submit">
</p>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Från: Forall.se';
$to = 'info#forall.se';
$subject = 'Ny meddelande';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Ditt meddelande har skickats!</p>';
} else {
echo '<p>Någonting gick fel. Var vänlig och försök igen!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>Du har gett fel svar på Anti-Spam frågan!</p>';
}
} else {
echo '<p>Du behöver att fylla alla fält i formuläret!</p>';
}
}
?>
</form>
Everything is woring as it should, except non English characters like ã, õ, ä, å, ö, ç and so on. This shouldn't be a problem if it wasn't a swedish website.
Here's a sample of the last e-mail I received:
"Jag hoppas du har förståelse för detta och att du hittar någon annan marknadsförare!
Jag återkommer när och om jag startar upp min firma, just nu är det lite osäkert hur det blir med dettas"
How do I solve this issue?

you need to add headers
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Från: Forall.se';
$to = 'info#forall.se';
$subject = 'Ny meddelande';
$human = $_POST['human'];
$headers = "MIME-Version: 1.0" . PHP_EOL;
$headers .= "From: $from <$email> ". PHP_EOL;
$headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;
$name = str_replace( '[at]','#', $name);
$message = str_replace( '[at]','#', $message);
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && filter_var($email, FILTER_VALIDATE_EMAIL)) {
if ($human == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Ditt meddelande har skickats!</p>';
} else {
echo '<p>Någonting gick fel. Var vänlig och försök igen!</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>Du har gett fel svar på Anti-Spam frågan!</p>';
}
} else {
echo '<p>Du behöver att fylla alla fält i formuläret!</p>';
}
}
?>

PHP mail function 4th parameter isn't exclusive to send a from value. Acording with PHP's manual:
-This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n).
Including charset which will encode the text of the message. In your case, as the comments says, you'll have to use UTF-8.
So, your code would be like this:
$header = "From: somebody#example.com\r\nContent-Type: text/plain; charset=UTF-8";
$message = $_POST['message'];
$to = 'info#forall.se';
$subject = 'Ny meddelande';
mail ($to, $subject, $body, $header)

Related

Norwegian letters "æ, ø, å" not showing when contact form is submitted

I have created a contact form, you can view it here. When I fill out the contact form and go to my inbox folder - the Norwegian letters æ, ø, å aren't shown in the message.
This is what I have currently added:
<meta http-equiv="content-type" content="text/html" charset="ISO-8859-1">
I have also tried <form accept-charset="ISO-8859-1">, but with no luck.
Here is a screenshot from the e-mail I receive after contact form has been submitted
as you can see there are no æ, ø, å letters.
Is there any way I can fix this?
PHP Code:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$number = $_POST['number'];
$from = 'Ny melding sendt fra kontaktskjema på Helsespesialisten.no';
$to = 'test#test';
$subject = 'Helsespesialisten | Du har motatt en ny melding';
$body = "Fra: $name\n E-post: $email\n Telefonnummer: $number\n Melding: $message\n";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Vennligst skriv inn ditt navn';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vennligst skriv inn din e-post';
}
//Check if message has been entered
if (!$_POST['number']) {
$errNumber = 'Vennligst skriv inn ditt telefonnummer';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Vennligst skriv en melding';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage) {
if (mail ($to, $subject, $body, $from, $number)) {
$result='<div class="alert alert-success">Takk for din henvendelse! Vi tar kontakt i løpet av kort tid!</div>';
} else {
$result='<div class="alert alert-danger">Beklager, en feil skjedde! Kontakt oss på: +47 35 11 15 40</div>';
}
}
}
Try this:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$number = $_POST['number'];
$from = 'Ny melding sendt fra kontaktskjema på Helsespesialisten.no';
$to = 'test#test';
$subject = 'Helsespesialisten | Du har motatt en ny melding';
$headerFields = array(
"From: $from",
"MIME-Version: 1.0",
"Content-Type: text/html;charset=utf-8"
);
$body = "Fra: $name\n E-post: $email\n Telefonnummer: $number\n Melding: $message\n";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Vennligst skriv inn ditt navn';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vennligst skriv inn din e-post';
}
//Check if message has been entered
if (!$_POST['number']) {
$errNumber = 'Vennligst skriv inn ditt telefonnummer';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Vennligst skriv en melding';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errNumber && !$errMessage) {
if (mail ($to, $subject, $body, implode("\r\n", $headerFields))) {
$result='<div class="alert alert-success">Takk for din henvendelse! Vi tar kontakt i løpet av kort tid!</div>';
} else {
$result='<div class="alert alert-danger">Beklager, en feil skjedde! Kontakt oss på: +47 35 11 15 40</div>';
}
}
}

Processing a form on the same page

So, I've been messing with this for a day now and I've read a lot tutorials and even posts on stackoverflow. I know it's possible to have all your php form processing steps on the same page as your form, but for some reason, my form won't send an email. Any help?
<div id="mc_embed_signup">
<form method="post" action="index.php" enctype="text/plain" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#address.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if($_POST['submit']) {
mail ($to, $subject, $body, $headers); //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
Remove the enctype="text/plain" that's a main factor.
Plus, you also need to check if fields were left empty using (if)empty().
Not doing so, you may receive emails that either do not contain the user's email, name, message or a combination of all.
Assuming this file is called index.php if not, use action=""
<div id="mc_embed_signup">
<form method="post" action="index.php" name="emailform">
<input type="text" name="name" placeholder="First Name or Alias"><br>
<input type="text" name="email" placeholder="Email Address"><br>
<textarea name="message" rows="10" size="50" placeholder="Placeholder Text."></textarea><br>
<input type="submit" value="Submit →" name="submit" class="button round">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'email#example.com'; //set to the default email address
$subject = 'Hello';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $email" . "\r\n" .
"Reply-To: $email" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
if(isset($_POST['submit']) && !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) ){
// check if mail was sent
if(mail ($to, $subject, $body, $headers)){ //mail sends it to the SMTP server side which sends the email
echo "<p>Your message has been sent!</p>";
}
else {
echo "<p>Mail was not sent. Check your logs.</p>";
}
}
else {
echo "<p>Something went wrong, go back and try again!</p>";
}
?>
</div>
This might help,
if(isset($_POST['submit'])) {
if(empty($_POST[name]) && empty($_POST[email]) && empty($_POST[message])){
echo 'Fields cannot be empty';
} else{
$name = $_POST['name'];
$to = $_POST['email'];
$subject = "Hello";
$message = $_POST['message'];
$from = $_POST['email'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
mail ($to, $subject, $body);
}
}else{
echo "<p>Something went wrong, go back and try again!</p>";
}

Error in creating contact form with php

I created a simple contact form as follows.
<form method="post" action="mail_receive.php">
<label>Name *</label>
<input name="name" placeholder="Type Here">
<label>Email *</label>
<input name="email" type="email" placeholder="Type Here">
<label>Phone No *</label>
<input name="phone" placeholder="Type Here">
<input id="submit" name="submit" type="submit" value="">
And php file called mail_receive.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$from = 'From: Someone';
$to = 'admin#gmail.com';
$subject = 'Ticket Ordering';
$body = "From: $name\n E-Mail: $email\n Phone:\n $phone";
if (isset($_POST['submit'])) {
if ($name != '' && $email != '' && $phone != '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>Please fill all required fields !!</p>';
}
}?>
}
And i uploaded to my hosting.
Although I can fill up the form and submit, I can't receive email to my inbox.
Is there any wrong?
With mail, it's meant to be
mail($to, $subject, $message, $headers);
Try something like this:
$to = "youremailaddress#whatever.com";
$subject = "Your tickets yo";
$headers = "From: someone#someone.com\r\n";
$headers .= "Reply-To: someone#someone.com\r\n";
$headers .= "MIME=VERSION:1.0\r\n";
$headers .= "Content-Type: text/plain; charset=UTF-8\r\n;"
$message = "Nice tickets bro.";
mail($to, $subject, $message, $headers);
And please I hope it's just for example purposes, but please don't actually be sending it to admin#gmail.com because obviously (unless you ARE actually admin#gmail.com, you wont recieve the e-mail.

Contact form is not working?

I'm trying to create a simple contact page however when I submit the form, it leads me to the blank page contact-form.php. I used this article as reference: http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/
What am I doing wrong?
Code on index.php (corner divs are purely for styling purposes)
<form action="contact-form.php" method="post" autocomplete="off" id="contact">
<div class="corner"></div>
<input type="text" required name="name" placeholder="Name" value="">
<div class="corner"></div>
<input type="text" required name="email" placeholder="Email" value="">
<div class="corner"></div>
<input type="text" required name="check" placeholder="Question of the day: What's 2 + 2 ?" value="">
<div class="corner"></div>
<textarea name="message" rows="25" cols="50" placeholder="Drop me a line!"></textarea>
<button class="send" type="submit" name="submit">Send</button>
</form>
Code on contact-form.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MessageAnita';
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
change the button tag to an input tag
< input type ="submit" name="submit"/>
In your form, change:
<button class="send" type="submit" name="submit">Send</button>
to:
<input type="submit" name="submit" value="Submit">
Plus, you may want to use this PHP mailing method, since the mail came into my SPAM when testing.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
And if you want to send as HTML use:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";

Php contact form errors

I'm building a php contact form that checks if some fields are filled it or not (no ajax).
The form is not working for 100%. I will try to explain. When I submit my form, The user navigates to http://mydomain.com/send.php
There is receive my error messages (if there are any). When I refresh the send.php page I receive the following errors: Notice: Undefined index: name in /Library/WebServer/Documents/~aledvertising/send.php
Here is my code
html form
<form method="post" action="send.php">
<div id="form-top">
</div>
<div id="form-left">
<label>Naam:<span class="star">*</span></label>
<input name="name" placeholder="Uw naam">
<label>Email:<span class="star">*</span></label>
<input name="email" type="email" placeholder="Uw email">
<label>Hoeveel is 2+2? (Anti-spam)<span class="star">*</span></label>
<input name="human" placeholder="Uw antwoord">
</div>
<div id="form-right">
<label>Uw bericht:<span class="star">*</span></label>
<textarea name="message" placeholder="Uw bericht"></textarea>
</div>
<input id="submit" name="submit" type="submit" value="Verzenden">
</form>
SEND.PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
?>
<?php
if ($_POST['submit']) {
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
?>
Can somebody please help me to get a full working form? Thank you
You should see isset method from php.
<?php
if(isset($_POST["name"]))
{
//code here
}
?>
The problem is that when you reload the page, the server side code looks for the index name and it does not find it because name does not have any value yet. The value is received once you submit the page. So that's why the error is there.
update
The error is that when you submit the form, there are some fields that may be send empty, so once that the post values goes to send.php there are $_POST that will be expecting values. if some of them do not recive does values, it will tell you: "Hey, i do not have values, so i im null". That why you have to check that the fields are all filled or make a validation on server side.
<?php
if(isset($_POST["name"],$_POST["last_name"],etc,etc))
{
//if everything ok, ill go on.
}
else{
//if there are empty field, go back
}
?>
UPDATE 2
if (isset($_POST['name'],$_POST['email'],$_POST['message'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: ' . $_POST['email'];
$to = 'myemail#mail.com' . ', ';
$to .= $_POST['email'];
$subject = 'Uw vraag op www.aledvertising.be';
$human = $_POST['human'];
$body = "From: $name\n E-Mail: $email\nMessage:\n $message";
if ($name != '' && $email != '' && $message != '') {
if ($human == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p class="green">Uw bericht is succesvol verzonden.</p>';
} else {
echo '<p>Er iets misgelopen, probeer opnieuw aub.</p>';
}
} else if ($_POST['submit'] && $human != '4') {
echo '<p>2+2 is niet gelijk aan het getal dat u hebt ingevoerd.</p>';
}
} else {
echo '<p>Alle velden met een * zijn verplicht in te vullen.</p>';
}
}
?>
setting the isset() function and then declaring the variables worked for me, tested it in my server, not giving any errors and form is submitted successfully...
please check it once...

Categories