PHP form do not show ÆØÅ - javascript

So I have made a contact form on my website. Everything works like a charm. However, the result beeing delivered to the email fail to display the ØÆÅ letters and instead only shows as weird symbols.. I have tried everything and feel really lost.
<?php
if (isset($_POST['submit']))
$sword = $_POST['swordy'];
$check = $_POST['check'];
$mailFrom = $_POST['mail'];
$message = $_POST['message'];
$ccname = $_POST['ccname'];
$droppy1 = $_POST['droppy1'];
$tel = $_POST['tel'];
$subject = 'Bryllupsinvitasjon ';
$mailTo = "your#email.com";
$headers = "From: ".$mailFrom;
$txt = " Du har motatt svar på bryllupsinvitasjon fra: ".$ccname."\n\n Svar: $check \n\n Telefonnummer: $tel\n\n Allergier: $droppy1\n\n Andre allergier/intolleranser/spørsmål/henvendelser: $message";
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html?mailsendt");
?>

Try inserting the corresponding Unicode or HTML code for your specific character. I believe all three of your indicated chars are on this page: https://www.rapidtables.com/code/text/unicode-characters.html

Your email headers here aren't specifying UTF-8 encoding. You'll need to add that into the headers, specifically as part of the Content-Type header, like so:
Content-Type: text/html; charset="UTF-8"
With that header, the email should display the characters properly.

Related

Inserting variable into PHP header Location URL

I'm very new to PHP.
I have an HTML membership form that when a user clicks the submit button it goes to a PHP page that compiles the fields passed, sends an email and lastly forwards the user to a "Thank you" Confirmation" html page.
I would like to include two of the form fields into the URL string so that I can then call up in the confirmation page to congratulate the name of the person for joining our club.
Two Questions
How do I concatenate the redirect url to include first name and last name for example:
new_member_confirmation.html?firstname=$firstname&lastname=$lastname
how do I then 'GET' the names in the URL string to display on the confirmation html page. For example "Thank you Bill Smith for becoming a member". I dont have an example of this as I do not know how to call or GET the values from the url string into the HML page
Here is the revised code which does not include every form field variable but gives you the gist of the PHP script.
<?php
// Multiple recipients
$to = 'info#abs.com'; // note the comma
// Form http_post_fields
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$subject = 'New Membership';
$cdateTime = date("l jS \of F Y h:i:s A");
// Message
$message = "
<html>
<head>
<title>New Membership</title>
</head>
<strong>From:</strong> \n $firstname \n $lastname <br>
<strong>Email:</strong> \n $email <br>
<strong>Date/Time Submitted:</strong>\n $cdateTime
</font></p>
<body>
</body>
</html>
";
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: Club Name <info#domain.com>'; // include a comma for more than one recipiant
$headers[] = 'From: Club Name <info#domain.com>';
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers)) or die("Email was not sent!");
// Here is where the user gets sent to the page below. Id like to append the $firstname $lastname variables into the URL string
header('Location: membership_confirmation.html');
// I thought I could create a URL string variable above like this --
// $urString = "Location: membership_confirmation.html?membername=$firstname \n $lastname";
// and then add it to the last portion of the script like this
// header($urString);
// Unfortunately, that doesn't work after I tried testing it.
?>
You you can concatenate to the url in your header statement, and use urlcode for your variables:
header('Location: membership_confirmation.html?firstname='.urlencode($firstname).'&lastname='.urlencode($lastname));
The header needs to reference a PHP file instead of an html file:
header('Location: membership_confirmation.php?firstname...
In the membership_confirmation.php file, get the variables via $GET similarly to how you are getting them from $POST in your current file, e.g.:
$firstname = $_GET['firstname'];
Ok here is what I came up with.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var url_string = "https://eaa309.club/membership_confirmation.html?firstname=Kelly&lastname=Brady";
var url = new URL(url_string);
var firstname = url.searchParams.get("firstname");
var lastname = url.searchParams.get("lastname");
//console.log(c);
//Write it out
document.getElementById("demo").innerHTML =
"Page path is: " + firstname + " " + lastname;
</script>
</body>
</html>

Send email to multiple recipient

i am using CI as framework and PhpMailer as email sender.
Building a project for notification, in which i am generating mail List which at least consists of (600 emails). But here i am restricted to send email to multiple recipient at one go by the google.
could anyone please suggest optimal way of doing it or any third party tool in php, javascript or jquery for doing it.
If you store emails in the database, you can try the solutions below. Here I assume the email is stored in a table called members and an querying database using PDO.
The code below can be modified to adapt to your needs
<?php
$db = new PDO (
'mysql:host=localhost;dbname=yourdb_name-goes-here;charset=utf8',
'root', // username
'' // password
);
// get emails from database via PDO
$result = $db->prepare('
SELECT email FROM members');
$result->execute(array());
while ($row = $result->fetch()){
$id= $row['id'];
$email = $row['email'];
$j=0;
for ($i=1; $i<=count($id); $i++) {
// send email here in multiple
$to=$email;
$subject = "Hello you have Message from Nancy";
$message = 'my message goes here';
$from = "yourdomain#eample.com";
$headers = "From:" . $from;
$sent=mail($to,$subject,$message,$headers);
if($sent) {
print "<br><font color=green><b>Your mail was sent Successfully</b></font>";
} else {
print "<br><font color=orange><b>We encountered an error sending your mail</b></font>";
}
$j++;
}
}
?>

I'm not sure how to send form over email [duplicate]

This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 7 years ago.
At the bottom of my site there is a "send message" button. I want it to take the message and contact information(email, name) and send it to my email address. How could I possibly do this? I'm new at this website stuff by the way.
You can use PHP mail function.
<?php
if (!empty($_POST)) {
$name= $_POST['name'];
$mail_id= $_POST['email_id'];
$email= '';//email address on which you want to receive website details
$message_field = $_POST['message'];
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Return-Path: \r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header= "From: TriaaHousing";
$message="Name:".$name."\r\n";
$message .=" ";
$message .= "EmailId:".$mail_id."\r\n";
$message .=" ";
$message .= "Message:".$message_field;
if(mail($email, "Subject", $message, $header)){
echo 1;
}else{
echo 0;
}
}
?>
Use can use default PHP's mail() functionality OR you can use PHPMailer (Mail send helper). Both are safe and correct. but if you need some other stuff then use PHPMailer.
1. Using PHP's mail() function it's possible. Remember mail function will not work in Local server.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: from#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
NOTE: You need to config SMTP on your local server if you are using SMTP. Take a look at this similar post.
2. You could also use PHPMailer class at https://github.com/PHPMailer/PHPMailer .
It allows you to use the mail function or use an smtp server transparently. It also handles HTML based emails and attachments so you don't have to write your own implementation.
Here is an example from the page above:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('webmaster#example.com', 'Webmaster User'); // Add a recipient
$mail->addAddress('webmaster#example.com'); // Name is optional example
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Use addReplyTo, addCC, addBCC if require.
Hope this help you well!

Form submission opens php page

I have a form on html page. it has action as mailer.php. All code works properly, but on submission, page goes to mailer.php and browser shows an empty page on screen
contactus.html form tag:
<form id="form_20" action="mailer.php" method="post" target="_self"
enctype="multipart/form-data">
//input fields here
</form>
mailer.php file:
<?php
$to = '---------------'; //Removed for privacy
$subject = 'New Enquiry';
$name = $_POST['Name'];
$tel = $_POST['Number'];
$email = $_POST['Email'];
$comments = $_POST['Message'];
$body = "From: $name \nNumber: $tel \nEmail id: $email \nComments: $comments";
$headers = 'FROM: ---------------------------'.PHP_EOL; //Removed for privacy
mail($to, $subject, $body, $headers);
echo '<script>alert("Your data has been submitted.");</script>';
?>
The code works fine, I get the mail too.
The only problem I have is that the page redirects to mailer.php
P.S. : If anyone finds this a duplicate question, please comment the link to the question it duplicates so I get my answer. Thanks
to send the user back to the previous page:
header('Location: http://www.example.com/foo.php?back=yes'); //add your url
after the mail call, and remove the script
on foo.php
if ($_GET['back']=='yes'){
echo '<script>alert("Your data has been submitted.");</script>';
}

How to make PHP script redirect and display a popup box simultaneously when something is true/successful?

In my PHP code, I have tried to make it so that after the user successfully sends an email, it redirects back to the contact page and then displays a popup box informing the user that the email has been successfully sent: however, the program only ever runs the top line, and does not run the popup box code. How do I fix this?
<? php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: #';
$to = '#';
$subject = 'Customer Message - Online Form';
$body = "From: ".$name.
"\r\n E-Mail: ".$email.
"\r\n Message: \r\n".$message;
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
header('Location: http://www.google.com.au');
echo "<script>alert('Message successfully sent.');</script>";
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
You are redirecting, by setting your header, before you print your alert. Try redirecting in the JS.
<? php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: #';
$to = '#';
$subject = 'Customer Message - Online Form';
$body = "From: ".$name.
"\r\n E-Mail: ".$email.
"\r\n Message: \r\n".$message;
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
echo "<script>alert('Message successfully sent.'); window.location='http://www.google.com.au'</script>";
//header('Location: http://www.google.com.au');
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
Note
This will cause a JS alert before switching pages. If you want to alter on the page you land on you will want to send either a $_POST or $_GET variable and listen for it on the page you land on
You can't redirect the user away from the page and expect the page to still display something to the user.
If you want to display a message on a different page than store it in session and then, on the other page, check if something is stored in session and display that.
you are redirecting using your headers, if you try to echo first and then use header to redirect you will get an error, saying the headers are allready set.
You could add the redirect to your frontend, by returning something else like
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
echo '<script>
alert("Message successfully sent.");
location.href="http://www.google.com.au";
</script>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
the location redirect should wait for the alert to be dismissed, before executing

Categories