HTML elements in PHP - php

I am very new to PHP, so be gentle.
It seems I cant run HTML in PHP.
For example when I try to
echo "<script> success(); </script>";
Result will be:
<script> success(); </script>
Another example:
$msg2 = "<b>Dear</b> ".$name ."\n\n" ."Thank you for your registration, we will be contacting you within 48h."."\n\n" ."Yours ....";
Result will be:
<b>Dear</b> (name of the client)
Thank you for your registration, we will be contacting you within 48h.
Yours ....
I checked the meta its:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I will give You guys the code ( some of it is in Estonian )
<?php
if (isset($_REQUEST['email'], $_REQUEST['nimi'], $_REQUEST['koolitus'], $_REQUEST['telf'], $_REQUEST['kuupaev'], $_REQUEST['tingimused'])) {
$admin_email = "ville.madison#gmail.com";
$email = $_REQUEST['email'];
$koolitus = $_REQUEST['koolitus'];
$nimi = $_REQUEST['nimi'];
$kuupaev = $_REQUEST['kuupaev'];
$telf = $_REQUEST['telf'];
$marked = $_REQUEST['marked'];
$kodulehemail = "koolitused#registreerumine.ee";
$koolitaja = $_REQUEST['koolitaja'];
$beauty = "info#beautyfactory.ee";
$msg = "Koolitusele registreerumine: " . $koolitus . "\n" . "Soovitud koolitaja: " . $koolitaja . "\n\n" . " Nimi: " . $nimi . "\n" . " Email: " . $email . "\n" . " Telefon: " . $telf . "\n" . " Soovitud kuupäev (aaaa-kk-pp): " . $kuupaev . "\n\n" . "Märked: " . $marked;
$msg2 = '<b>Hea</b> ' . $nimi . "\n\n" . "Täname Teid koolitusele broneerimast, teatame Teile registreeringu kinnitamisest 48h jooksul." . "\n\n" . "Teie Beauty Factory";
mail($admin_email, "Koolitusele registreerumine", $msg, "From:" . $kodulehemail);
mail($email, "Koolitusele registreerumine", $msg2, "From:" . $beauty);
}
?>
At the moment there is only one word that I am trying to make in bold for the sake of testing. But when I get it working I will start editing it more and I want to echo a javascript function in the php if statement.
I have tried using single quotes, double quotes and the:
nl2br(htmlentities($msg2));
None of it works

The message you send is not interpreted as HTML. The default
header of the mail function is text/plain.
If you want your customers to see the bold text, you must specify the content-type as HTML in the message header.
<?php
// Set the content type of the email to HTML (and UTF-8)
$header = 'Content-type: text/html; charset=utf-8' . "\r\n";
// Add the from field
$header .= 'From: ' . $beauty . "\r\n";
// Send email
mail ($email, "Koolitusele registreerumine", $msg2, $header);
?>
However you will not be able to use Javascript as it is intepreted client side. PHP does not interpret Javascript, and most of the mail clients will not execute Javascript for obvious security reasons.

Here is a sample example of how to use html tags in php:
<?php
var $name = "xyz";
var $email = "xyz#gmail.com";
echo '<table border="1" style="width:20%">';
echo '<tr><th>UserName</th><th>Email</th></tr><tr><td>';
echo $name."<br>";
echo '</td><td>';
echo $email."<br>";
echo '</td></tr></table>';
?>
This is basic php and html table format example,you can follow this for write html tags.

The problem was that the function mail() had a default content of text/plain, just needed the $header to make text/html. Thanks guys for your quick answers !

Related

Email form is messing up UTF-8 text [duplicate]

This question already has answers here:
How to send UTF-8 email?
(3 answers)
Closed 3 years ago.
I have a PHP form on my website. It collects text strings from HTML input elements, then sends the text to my mailbox. However, when people type in text in other languages, the email appears garbled. The text appears to be okay in the Web browser. There's even some JavaScript code that gets/sets some of these form fields, and seems to do so correctly.
Maybe the PHP code is not treating the text as unicode? Or the text gets sent to my mailbox using the wrong encoding? I would appreciate some help, thanks.
I have already added
header("Content-Type: text/html; charset=utf8");
to the top of the PHP files.
Here is a part of the PHP form that gets the inputted values using POST.
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$titletxt = $_POST["titletxt"];
$titleurl = $_POST["titleurl"];
$legend = $_POST["legend"];
$command = $_POST["command"];
$binding = $_POST["binding"];
$layout = $_POST["layout"];
Here is a part of the PHP form that emails me the text:
mail
(
"blahblah#blah.com",
"VGKD Bindings Submission",
"NAME:\t\t" . $name . "\n" .
"EMAIL:\t\t" . $email . "\n" .
"MESSAGE:\t" . $message . "\n" .
"GAME TITLE:\t" . $titletxt . "\n" .
"GAME URL:\t" . $titleurl . "\n" .
"LAYOUT:\t\t" . $layout . "\n\n" .
"LEGENDS:\n" . $legend . "\n\n" .
"COMMANDS:\n" . $command . "\n\n" .
"BINDINGS:\n" . $binding . "\n\n"
);
Please, try to add utf-8 charset header to your mail function, for example
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <example#example.com>' . "\r\n";
$headers .= 'Cc: example#example.com' . "\r\n";
mail
(
"blahblah#blah.com",
"VGKD Bindings Submission",
"NAME:\t\t" . $name . "\n" .
"EMAIL:\t\t" . $email . "\n" .
"MESSAGE:\t" . $message . "\n" .
"GAME TITLE:\t" . $titletxt . "\n" .
"GAME URL:\t" . $titleurl . "\n" .
"LAYOUT:\t\t" . $layout . "\n\n" .
"LEGENDS:\n" . $legend . "\n\n" .
"COMMANDS:\n" . $command . "\n\n" .
"BINDINGS:\n" . $binding . "\n\n",
$headers
);
The header() function sends a raw HTTP header to a client browser, but mail function with the variable $headers sends headers to email.

PHP String concatenation and New Lines

I need a bit of help understanding some PHP behavior that is not making sense to me, thanks in advance for the help.
First off, is there a difference between the use of \r\n and \n\r? I would think logically no, but I am thinking there has to be a practical reason why they are listed in the PHP document separately. I am currently using \r\n but sometimes I get a line break, sometimes I don't, its befuddling.
Second, I am aware that if I was echoing the information inside the browser, I would use the nl2br() function but I am not showing the information to the browser, it is being gathered and concatenated into a string that is then being sent via mail() and that's where the visual discrepancy is showing up.
Here is the script, pretty straight forward, nothing mind boggling here:
<?php
$browser = $_SERVER['HTTP_USER_AGENT'];
$page = $_SERVER['PHP_SELF'];
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$from_page = $_SERVER['HTTP_REFERER'];
$time = date('l, F jS Y - g:i:s A', $_SERVER['REQUEST_TIME']);
$uri = $_SERVER['SCRIPT_URI'];
$to = "***";
$subject = "Visitor Log";
$message = "IP Address : " . $ip . " ( HOST: " . $host . " )\r\n" . "Browser : " . $browser . "\r\n" . "From : " . $from_page . "\r\n" . "Page : " . $page . "\r\n" . "Time : " . $time . "\r\n" . "Script : " . $uri;
$headers = "From: ***" . "\r\n" . "Reply-To: ***" . "\r\n" . "X-Mailer: PHP/" . phpversion();
mail($to,$subject,$message,$headers);
?>
But as you can see, I have linebreaks that are padded into the $message for obvious readability of the email that I receive. But sometimes the emails arrive and everything is on a separate line as should be and sometimes it doesn't. So I thought maybe the method of adding a line break is not the best and that I am doing something wrong.
So the question is, what should I use to get the proper line breaks, if what I am using is not correct, and if it is correct, why isn't it always working? Your assistance to add some clarity to this would be greatly appreciated.
If it matters or makes a difference, I am using PHP 5 (5.5.28) Linux server with Apache.
UPDATED ANSWER
Thank you for everyone's help but ultimately I ended up fixing it as follows and even added the content type for good measure (TY #TOM).
<?php
$browser = $_SERVER['HTTP_USER_AGENT'];
$page = $_SERVER['PHP_SELF'];
$ip = $_SERVER['REMOTE_ADDR'];
$host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$from_page = $_SERVER['HTTP_REFERER'];
$time = date('l, F jS Y - g:i:s A', $_SERVER['REQUEST_TIME']);
$uri = $_SERVER['SCRIPT_URI'];
$to = "***";
$subject = "Visitor Log";
$message = "IP Address : " . $ip . " ( HOST: " . $host . " )" . "\r\n";
$message .= "Browser : " . $browser . "\r\n";
$message .= "From : " . $from_page . "\r\n";
$message .= "Page : " . $page . "\r\n";
$message .= "Time : " . $time . "\r\n";
$message .= "Script : " . $uri;
$headers = "From: ***" . "\r\n";
$headers .= "Reply-To: ***" . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
$headers .= "Content-Type: text/html; charset=\"utf-8\"" . "\r\n";
mail($to,$subject,nl2br($message),nl2br($headers));
?>
NOTE: The new formatting is not part of the solution, its just for myself. The solution is giving up on plain and going with html and using nl2br function to force the breaks, not elegant, not what I wanted entirely but works.
my best guess is that you sometimes send as text/plain and sometimes as text/html.. (since you do not specify anything, maybe the mail() or mailserver or client takes a best guess? you could investigate..)
i would try to force it to send as text/html and use
<br/>
's with nl2br, or force it as text/plain and use \n
like...
$message = "IP Address : " . $ip . " ( HOST: " . $host . " )\r\n" . 'Content-Type: text/html; charset="utf-8"'."\r\n"."Browser : " . $browser . "\r\n" . "From : " . $from_page . "\r\n" . "Page : " . $page . "\r\n" . "Time : " . $time . "\r\n" . "Script : " . $uri;
..if that's how you force it to mail as html, im not sure about the header
The thing is that you are not passing the content-type in the email header, so the email client could interpret it like plain-text or text/html depending of the vendor. The best is to pass the content-type as parameter in the email header.
content-type:text/plain: \n as line break
content-type:text/html: </br> as line break

sendmail.php does not work on mobile

Strange issue - my sendmail.php is working perfectly on desktop and on mobile devices only when requesting desktop websites (in Chrome app), but when using mobile site he does not work at all.
Can someone help me figure this out?
here is the code:
<?php
if(isset($_POST['email'])) {
if (!check_email($_POST['email']))
{
echo 'Please enter a valid email address<br />';
}
else send_email();
}
exit;
function check_email($emailAddress) {
if (filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
return TRUE;
} else {
return FALSE;
}
}
function send_email() {
$message = "\nName: " . $_POST['name'] .
"\nEmail: " . $_POST['email'] ;
$message .= "\nMessage: " . $_POST['comment'] .
"\n\nBrowser Info: " . $_SERVER["HTTP_USER_AGENT"] .
"\nIP: " . $_SERVER["REMOTE_ADDR"] .
"\n\nDate: " . date("Y-m-d h:i:s");
$siteEmail = $_POST['receiver'];
$emailTitle = $_POST['subject'];
$thankYouMessage = "Thank you for contacting us, we'll get back to you shortly.";
if(!mail($siteEmail, $emailTitle, $message, 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>'))
{
echo 'error';
}
else
{
echo 'success';
}
}
?>
You must make sure the mobile form has these two elements:
It is being submitted using method="POST".
Your email input has the attribute and value name="email".
I demonstrate where these things are set in the following code. This is incomplete, of course, it's just designed to show you where the two required parts must be.
<form ... method="POST">
...
<input type="text" name="email" ... >
...
</form>
I need to mention one more thing ...
That being said, what you're doing here is EXTREMELY INSECURE. You are allowing someone to set an email's from and two address on a web form. A (not so) clever hacker can easily write a script to turn your server into an open relay for spam or other evil activities. At minimum, you should remove $_POST['receiver'] and replace with with a hard-coded email address or at least not something that can be altered by an end-user when they POST to your form.
So I drilled down and found that the mobile form is directing submissions through this PHP file which was 404.
The issue now is, even when this file is avaiable, emails are not sent...
' . "\r\n"; $headers .=
'From: ' . "\r\n"; $headers = 'MIME-Version: 1.0' .
"\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message .= "Name: " . $name . ""; $message .= "Subject: "
. $subject . ""; $message .= "Email: " . $email . ""; $message .= "Message: " . $message_text . "";
mail($to, $subject, $message, $headers, "-f noreplay#info.com");
echo 1; }
if(isset($_POST['sendmail']) && $_POST['sendmail'] == 1){
send_mail(); } ?>

getting warning Header may not contain more than a single header on wordpress

I'm learning WordPress and PHP and building a website at www.sunriselodging.com
When trying to enter data in a form, I get a PHP error warning saying 'header may not contain more than a single header' on line 6. I've tried solutions like checking the URL which are not related to PHP. I read a few solutions here that are PHP dependent and hence I cannot figure out what needs to be done here.
My code is below:
<?php
$reservation_message = $_POST['reservation_message'];
// Redirect back
Header('Location: '. $_POST['reservation-url'].'?message='.$reservation_message);
echo "<meta charset='utf-8'>";
// Loading variables from form
$blog_name = $_POST['blog_name'];
$blog_email = $_POST['blog_email'];
$reservation_email_subject = $_POST['reservation_email_subject'];
$reservation_email = $_POST['reservation_email'];
$reservation_email_switch = $_POST['reservation_email_switch'];
$room = $_POST['reservation-room'];
$checkin = $_POST['reservation-checkin'];
$checkout = $_POST['reservation-checkout'];
$people = $_POST['reservation-people'];
$name = $_POST['reservation-name'];
$email = $_POST['reservation-email'];
$phone = $_POST['reservation-phone'];
$message = $_POST['reservation-message'];
if($reservation_email_switch == 'on'){
// EMAIL TO CLIENT
// SET info to email
// From in format NAME <email>
$from = $blog_name.'<'.$blog_email.'>';
// Reply to in format NAME <email>
$reply = $blog_name.'<'.$blog_email.'>';
// Subject
$subject = $reservation_email_subject;
// Message
$message = $reservation_email;
//
$to = $email;
$headers = 'From: '. $from . "\r\n" .
'Reply-To: '. $reply . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send mail
mail($to,$subject,$message,$headers);
// END OF EMAIL TO CLIENT
// ---------------------
}
// EMAIL TO RESERVATION CREW
$message = $_POST['reservation-message'];
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n" .
'Reply-To: '. $name . '<' . $email . '>' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = 'Reservation for room #' . $room;
$message = 'Name: ' . $name . '
Room: #' . $room . '
Check in: ' . $checkin . '
Check out: ' . $checkout . '
Number of people: ' . $people . '
Email: ' . $email . '
Phone: ' . $phone . '
' . $message;
$to = $blog_email;
// Send mail
mail($to,$subject,$message,$headers);
// END OF EMAIL TO RESERVATION CREW
// ---------------------
exit();
?>
The URL for the form is here - http://www.sunriselodging.com/reservation/. Apparently the form hasn't been sending emails either. Can someone please look at this and advise what's wrong with the code here?
I'll also appreciate if someone can direct me towards a good online PHP course for beginners (great if free).
Thank You,
Aadi
The whole problem with this here snippit is that you have 1) no conditions 2) no mail() function.
If you are going to use a header, you should probably have a condition. You should also probably have a condition to execute the emailer. Finally, you need to run this code before everything on the page (likely before the get_header() if on the page). Your workflow should go something similar to this:
<?php
// I assume you want the form to first process the email then forward
// so you need to check that the form was submitted
// Then check that the customer email is valid
if(isset($_POST['reservation-email']) && filter_var($_POST['reservation-email'], FILTER_VALIDATE_EMAIL)) {
// Loading variables from form
$blog_name = $_POST['blog_name'];
$blog_email = $_POST['blog_email'];
$reservation_email_subject = $_POST['reservation_email_subject'];
$reservation_email = $_POST['reservation_email'];
$reservation_email_switch = $_POST['reservation_email_switch'];
$room = $_POST['reservation-room'];
$checkin = $_POST['reservation-checkin'];
$checkout = $_POST['reservation-checkout'];
$people = $_POST['reservation-people'];
$name = $_POST['reservation-name'];
$email = $_POST['reservation-email'];
$phone = $_POST['reservation-phone'];
$message = $_POST['reservation-message'];
// Company email
$master = 'my#email.address';
// Use a from address
$header = "From: $email"."\r\n";
// Try mailing off the reservation to the hotel
if(mail($master,'Reservation Request',$message,$header)) {
// If success, send another email to the customer
$header = "From: no-reply#hotel.com"."\r\n";
mail($email,'Reservation Confirmation',$message,$header);
// What is the difference between this "reservation_message" and "reservation-message"??
$reservation_message = $_POST['reservation_message'];
// Redirect back
header('Location: '.$_POST['reservation-url'].'?message='.$reservation_message);
exit;
}
else {
echo 'Sorry, your reservation failed. Please contact customer service for reservations.';
exit;
}
}
?>
I was able to solve this after some research on Stack Overflow.
I added urlencode to the last string on the line containing header. I made:
Header('Location: '. $_POST['reservation-url'].'?message='.$reservation_message);
to look like this:
Header('Location: '. $_POST['reservation-url'].'?message='.urlencode($reservation_message));
To enable the mail function to send email I changed line 54 from
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n" .
to this
$headers = 'From: Booking#hotel.com' . "\r\n" .
& line 70 from
$to = $blog_email;
to
$to = 'my#email.address';
Hope this will help anyone who's stuck in a situation similar to mine. A big THANK YOU to all the people who helped.

PHP mail() not received for Outlook 2007

I have a contact form that I'm writing using jQuery and PHP. Below is the PHP bit, which works perfectly fine when I change the $youremail variable to my gmail account, but the emails are never received in my office Outlook account.
Is there something I'm missing?
<?php
// Email Vars
$youremail = "my_email#on_outlook2007.com";
$headers = "From: $name <$email>\n";
$subject = "Subject Line Here";
$ip = $_SERVER['REMOTE_ADDR'];
$message_clean = html_entity_decode(stripslashes($message));
// Format Email
$email_format_cc = $message_clean . "\n\n" . "___________________________________________________________________________" . "\n\n" .
"Name: " . $name . "\n" .
"Company: " . $company . "\n" .
"IP Address: " . $ip . "\n" .
"Sent: " . $timestamp;
$email_format = $email_format_cc . "\n" .
"Page: " . $from_url;
mail($youremail, $subject, $email_format, $headers);
?>
Check your server's mail log to see if/why gmail is bouncing the mail. PHP's mail() function is extraordinarily stupid and claims success even as the universe explodes around it.

Categories