Textarea retains formatting

Stupid question I’m sure, but…I have a signature in Mac mail app, consisting of three lines of text (name, email, url) with a horizontal line above and below made from underscores. I wanted to add this into a textarea in a .php file for use in a mail() routine so it would always be there whenever I sent mail. So I copied and pasted as is between the textarea tags in BBEdit (so on the BBEdit page it looks the same as on emails in the mail app). And when I uploaded to the server and access that page, it’s still formatted exactly the same! If I send an email it retains its formatting. The .php file doesn’t strip out any of the formatting in the textarea!

Can anyone explain why? If I add
tags to be sure, they just show up as text! I’m not complaining, just surprised and not sure I can trust it. I can’t see it being machine-specific.

Are you using \n in the PHP for new lines?

Not on the textarea.

Will you show us the full page of code please?

Have you got this working as wanted?

Here’s the code. It works as I want it to, but not sure why the formatting is retained.

I have another question: why does the subject get mangled but the message body shows Japanese correctly? How can I get the subject to be properly formatted?

<html>
<head>
<title>Send email</title>
<meta http-equiv="Content-Language" content="ja">
<meta http-equiv="Content-Type" content="text/javascript; UTF-8 ">
<link rel="shortcut icon" href="../../favicon.ico">
<link rel="icon" href="../../favicon.png" type="image/png">
<link rel="stylesheet" type="text/css" href="styles.css">

<style type="text/css">
td {
font-size: 14px;
padding:1;
}

@-webkit-keyframes blink { 
   50% { border-color: #ff0000; } 
}
@keyframes blink { 
   50% { border-color: #ff0000; } 
}
div#test {
    padding: 1px;
    border: 10px solid transparent;
}

#test:hover {
    animation: blink .5s step-end infinite alternate;
    -webkit-animation: blink .5s step-end infinite alternate;
}
</style>

<title>Selecting or deselecting all CheckBoxes</title>  
        <script type="text/javascript">  
            function selects(){  
                var ele=document.getElementsByClassName('checkbx');  
                for(var i=0; i<ele.length; i++){  
                    if(ele[i].type=='checkbox')  
                        ele[i].checked=true;  
                }  
            }  
            function deSelect(){  
                var ele=document.getElementsByClassName('checkbx');  
                for(var i=0; i<ele.length; i++){  
                    if(ele[i].type=='checkbox')  
                        ele[i].checked=false;  
                      
                }  
            }             
        </script>  
</head>

<!-- BODY --> 

<body topmargin="0" marginheight="0" leftmargin="0" marginwidth="0" style="font-size:13px;">

<span style="z-index:100; position:absolute; left:10px; top:8px"><font style='color: orange; font-size:14px'>
<script language='javascript'>
document.write('<b>SEND EMAIL</b>');
</script>
</font></span>

<table width="100%" cellspacing="0" cellpadding="3" style="border:0;height:31px"><tr><td bgcolor="black" align=center style="border:0">
<input type="button" value="Return to main menu" onClick="window.location='../index.php';" class="button" />
</td></tr></table>&nbsp;

<!-- send message -->

<?php
if (isset($_POST['submit']) && $_POST['submit']=="SEND MESSAGE") { //b

$subject=$_POST['subject'];
$message=$_POST['message'];

$message=wordwrap($message,70);
$listofadds="";
foreach ($_POST['emailto'] as $send) { //d
include ('/home/usn/private/dblon.php');
$to=$send;
$headers = "From: info@company.com";

mb_language("ja");
$subject = mb_convert_encoding($subject, "ISO-2022-JP","AUTO");
$subject = mb_encode_mimeheader($subject);
$message = mb_convert_encoding($message, "ISO-2022-JP","AUTO");

$mail_sent = mail($to, $subject, $message, $headers );

echo ($mail_sent ? "<p><span style='color:#006400; font-weight:bold; font-size:16px;'>&nbsp;&nbsp;&nbsp;&nbsp;An email has been sent to ".$send. ".</span></p>" : "<p><span style='color:#AC0000; font-weight:bold;'>Mail could not be sent to ".$send.". Please try again later.</span></p>");

$listofadds="$listofadds"." $send\n";

}  //d

$adminmessage="The message below has been sent to\n\n"."$listofadds"."\n\nSubject:\n\n$subject\n\nMessage:\n\n$message";

$headers = "From: info@company.com";

$mail_sent = @mail("info@company.com", "Mail sent to", $adminmessage, $headers );

echo ($mail_sent ? "<p><span style='color:#8B008B; font-weight:bold; font-size:16px;'><br>&nbsp;&nbsp;&nbsp;&nbsp;A copy has also been posted to admin.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Return to the <a href='../index.php' class='navylink'>Main Menu</a>.</span></p>" : "<p><span style='color:#AC0000; font-weight:bold;'>Mail could not be sent to admin.</span></p>");

exit();

}  //b

?>
<!-- addresses submitted -->

<?php

if (isset($_POST['submit']) && $_POST['submit']=="COMPOSE MESSAGE" && isset($_POST['email'])) {  //r

?>
<form name="compose" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<table cellpadding="5" cellspacing="0" style="margin-top:20px;" align="center"><tr><td colspan="4" style="background-color:#DDA0DD; font-size:15px;">You chose the following email addresses to email.<br> Compose the message below and click 'SEND MESSAGE'. A copy will be sent to admin.<br>If the address list is incorrect click 'Cancel' to start again.</td></tr>

<tr style="color:navy; font-size:15px; font-weight:bold; background-color:#FFE4E1;"><td align="center">Stu #</td><td>Class</td><td>Name</td><td width="400">Send to</td></tr>
<?php

$emailto=array();
[$i]="";
foreach ($_POST['email'] as $sendto) {  //h

echo("<input type='hidden' name='emailto[$i]' value='".$sendto."'>");

if($_POST['how']=="alpha") {
include ('/home/usn/private/dblon.php');
$result=mysqli_query($dbcnx, "SELECT stuno,fname,lname,emailhome,emailmobile,emailwork,day,starth,startm FROM Details WHERE emailhome='$sendto' OR emailmobile='$sendto' OR emailwork='$sendto'"); 
}

if($_POST['how']=="class") {
include ('/home/usn/private/dblon.php');
$result=mysqli_query($dbcnx, "SELECT stuno,fname,lname,emailhome,emailmobile,emailwork,day,starth,startm FROM Details WHERE (emailhome='$sendto' OR emailmobile='$sendto' OR emailwork='$sendto')"); 
}

while ($row=mysqli_fetch_array($result)) {  //e
$stuno=$row['stuno'];
$fname=$row['fname'];
$lname=$row['lname'];
$emailhome=$row['emailhome'];
$emailmobile=$row['emailmobile'];
$emailwork=$row['emailwork'];
$day=$row['day'];
$starth=$row['starth'];
$startm=$row['startm'];

echo ("<tr><td style='text-align:center'>".$stuno."</td><td>".$day." (".$starth.":".$startm.")</td><td>".$fname." ".$lname."</td><td>");

if ($sendto==$emailhome) {echo("(Home) ");}
if ($sendto==$emailmobile) {echo("(Mobile) ");}
if ($sendto==$emailwork) {echo("(Work) ");}

echo($sendto."</td></tr>");

}  //e
}  //h 


?>
<tr><td style="background-color:#898d8f; padding:8px;" colspan="4" align="center">
<input type="button" value="Cancel" onClick="window.location='sendemail.php';" >
&nbsp;&nbsp;

<input type="button" value="Return to main menu" onClick="window.location='../index.php';">
</td></tr></table>

<table cellspacing="0" style="margin-top:20px; border:0" align="center">
<tr><td style="border:0;"> &nbsp;&nbsp;Subject <span style="font-size:13px;color:#994444;font-weight:bold">(in English):</span></td></tr>
<tr><td style="border:0;"><input type="text" name="subject" size="67"></td></tr>
<tr><td style="border:0;"> &nbsp;&nbsp;Message:</td></tr>
<tr><td style="border:0;">
<textarea name="message" rows="30" WRAP="hard" cols="65">
**このメールは送信専用です。返信いただく場合はinfo@company.comへお願いします**








_____________________________
Company
info@company.com
www.company.com
_____________________________
</textarea>

</td></tr>
<tr><td  align="center" style="background-color:#898d8f;padding:8px;border:0"><input type="submit" name="submit" value="SEND MESSAGE" class="button">
&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Clear Form"></td>

</tr>
</table>

</form>
<?php
exit();
}  //r
?>

<!-- get names -->

<?php

if (isset($_POST['submit']) && $_POST['submit']=="COMPOSE MESSAGE" && !isset($_POST['email'])) { //k

$tryagain="1";
echo("&nbsp;&nbsp;&nbsp;<span style='color:#AC0000; font-weight:bold; font-size:16px;'>No email addresses were chosen.</span>");
}  //k

if ((isset($_POST['submit']) && $_POST['submit']=="GET LIST" && isset($_POST['howlist'])) OR isset($tryagain)) { //p
?>
<form name="form2" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<table cellpadding="5" cellspacing="0" style="margin-top:20px;" align="center"><tr><td colspan="4" style="background-color:#DDA0DD; font-size:15px;">&nbsp;Select ALL the student(s) and email address(es) you wish to send to <span style="color:#ac0000;font-weight:bold">(or Select All at the bottom):</span></td></tr>

<tr style="color:navy; font-size:15px; font-weight:bold; background-color:#FFE4E1;"><td align="center">&nbsp;Stu #&nbsp;</td><td>&nbsp;Class&nbsp;</td><td>&nbsp;Name</td><td width="400">&nbsp;Send to&nbsp;</td></tr>
<?php
include ('/home/usn/private/dblon.php');

if ($_POST['howlist']=="allabc") {
$result=mysqli_query($dbcnx, "SELECT stuno,fname,lname,emailhome,emailmobile,emailwork,day,starth,startm FROM Details ORDER BY fname"); 
echo("<input type='hidden' name='how' value='alpha'>");
}

if ($_POST['howlist']=="allclass") {
$result=mysqli_query($dbcnx, "SELECT stuno,fname,lname,emailhome,emailmobile,emailwork,day,starth,startm FROM Details ORDER BY day,starth,startm,fname"); 
echo("<input type='hidden' name='how' value='class'>");
}

$email=array();
$count=0;

while ($row=mysqli_fetch_array($result)) {  //j
$stuno=$row['stuno'];
$fname=$row['fname'];
$lname=$row['lname'];
$emailhome=$row['emailhome'];
$emailmobile=$row['emailmobile'];
$emailwork=$row['emailwork'];
$day=$row['day'];
$starth=$row['starth'];
$startm=$row['startm'];

echo ("<tr><td style='text-align:center'>".$stuno."</td><td>".$day." (".$starth.":".$startm.")</td><td>".$fname." ".$lname."</td><td>");

if (!empty($emailhome)) {
echo("<input class='checkbx' type='checkbox' name='email[$count]' value='".$emailhome."'>");
echo("<span style='color:#4B0082;'>Home (".$emailhome.")</span><br>");
$count++;
} else {
echo("");
}

if (!empty($emailmobile)) {
echo("<input class='checkbx' type='checkbox' name='email[$count]' value='".$emailmobile."'>");
echo("<span style='color:#006400;'>Mobile (".$emailmobile.")</span><br>");
$count++;
} else {
echo("");
}

if (!empty($emailwork)) {
echo("<input class='checkbx' type='checkbox' name='email[$count]' value='".$emailwork."'>");
echo("<span style='color:#FF4500;'>Work (".$emailwork.")</span>&nbsp;");
$count++;
} else {
echo("");
}

echo("</td></tr>");

}  //j

$howlist=$_POST['howlist'];
echo("<input type='hidden' name='howlist' value=".$howlist.">");

?>
<tr><td colspan="2" align="center" style='color:red;border:solid 2px red'>
<div id="test">
<input type="button" onclick='selects()' value="Select All"/>&nbsp; 
<input type="button" onclick='deSelect()' value="Deselect All"/></td>
</div>
<td style="background-color:#898d8f; padding:8px;" colspan="2" align="center"><input type="submit" name="submit" value="COMPOSE MESSAGE" class="button" >
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="Clear Form" class="button">
&nbsp;&nbsp;
<input type="button"  class="button" value="Return to main menu" onClick="window.location='../index.php';">
	
</td></tr></table>
</form>

<?php

exit();
}  //p
?>

<!-- front page -->

<form name="mainform" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table align="center" style="border:0; font-size: 20px; margin-top:100px;"><tr><td style="border:0; color:#ee2222; padding:8px">
How would you like the list of students presented?</td></tr>
<tr><td style="border:0; padding:8px"><input type="checkbox" name="howlist" value="allabc"> All names in alphabetical order</td></tr>
<tr><td style="border:0; padding:8px"><input type="checkbox" name="howlist" value="allclass"> All names grouped by class</td></tr>

<tr><td style="background-color:#898d8f; padding:8px;" align="center"><input type="submit" name="submit" value="GET LIST" class="button" style='width:90px'>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="Clear Form" class="button">
&nbsp;&nbsp;

<input type="button" value="Return to main menu" onClick="window.location='../index.php';" class="button" />
</td></tr>
</table>
</form>
</body>
</html>

I think this may answer your question about the formatting:

It says that with the
wrap="hard"
attribute of
<textarea>
"the text in the textarea is wrapped (contains newlines) when submitted in a form. "

In what way does the subject get mangled?

I put

<?php
echo "<pre>";
print_r ($_POST['message']);
echo "<pre>";

echo "<pre>";
print_r ($_POST['subject']);
echo "<pre>";
 ?>

on the page to see what is being returned and it simply shows the subject text, not mangled. I only entered a single line of text into the subject input though.

Many thanks for your help, wake689.

If you type Japanese in the message body, it stays Japanese when sent. But Japanese in the subject gets mangled into Martian when sent.

I have taken the form part of your code and added a little so that I can put it online and it will send the emails to my address. I have removed my email address from the following code:

<?php
if (isset($_POST['submit'])){


$message=$_POST['message'];
$subject=$_POST['subject'];

echo "Before mb_convert, message = $message<br> and subject = $subject <br><br>";



 mb_language('ja');
 $subject = mb_convert_encoding($subject, 'ISO-2022-JP','AUTO');
 $subject = mb_encode_mimeheader($subject);
 $message = mb_convert_encoding($message, 'ISO-2022-JP','AUTO');

 $to = "*************************";
 $headers="From: me";

 $mail_sent = mail($to, $subject, $message, $headers );


echo "After mb_convert, message = $message<br> and subject = $subject <br><br>";
}
?>

<form name="compose" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<table cellpadding="5" cellspacing="0" style="margin-top:20px;" align="center"><tr><td colspan="4" style="background-color:#DDA0DD; font-size:15px;">You chose the following email addresses to email.<br> Compose the message below and click 'SEND MESSAGE'. A copy will be sent to admin.<br>If the address list is incorrect click 'Cancel' to start again.</td></tr>

<tr style="color:navy; font-size:15px; font-weight:bold; background-color:#FFE4E1;"><td align="center">Stu #</td><td>Class</td><td>Name</td><td width="400">Send to</td></tr>

<tr><td style="background-color:#898d8f; padding:8px;" colspan="4" align="center">
<input type="button" value="Cancel" onClick="window.location='sendemail.php';" >
&nbsp;&nbsp;

<input type="button" value="Return to main menu" onClick="window.location='../index.php';">
</td></tr></table>

<table cellspacing="0" style="margin-top:20px; border:0" align="center">
<tr><td style="border:0;"> &nbsp;&nbsp;Subject <span style="font-size:13px;color:#994444;font-weight:bold">(in English):</span></td></tr>
<tr><td style="border:0;"><input type="text" name="subject" size="67"></td></tr>
<tr><td style="border:0;"> &nbsp;&nbsp;Message:</td></tr>
<tr><td style="border:0;">
<textarea name="message" rows="30" WRAP="hard" cols="65">
**このメールは送信専用です。返信いただく場合はinfo@company.comへお願いします**








_____________________________
Company
info@company.com
www.company.com
_____________________________
</textarea>

</td></tr>
<tr><td  align="center" style="background-color:#898d8f;padding:8px;border:0"><input type="submit" name="submit" value="SEND MESSAGE" class="button">
&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="Clear Form"></td>

</tr>
</table>

</form>

Any Japanese in the subject and the message arrives intact in emails sent to me. If you would like I will put your email address into it on my server so you can see how the emails arrive to you.

I wonder if there is a difference in the way your server vs my server deal with languages?

I note a few lines in your code:

 mb_language('ja');
 $subject = mb_convert_encoding($subject, 'ISO-2022-JP','AUTO');
 $subject = mb_encode_mimeheader($subject);
 $message = mb_convert_encoding($message, 'ISO-2022-JP','AUTO');

This is to do with encoding the strings into a format that the mail() function uses. I wonder if the extra step in that the subject goes through is anything to do with why the subject gets garbled for you?

The above page on my server is at https://www.danieljeffery.co.uk/jstest/form.php and it shows what is returned in $_POST['subject] and $_POST[‘message’] both before and after the above coding.

I don’t know what your level of expertise in PHP is? I am no more than a lower end intermediate.

There is a lot of code here that accesses various contact information from a mySQL database. What is the intended use of the page ultimately?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.