Forum Moderators: coopster
I ask for help with a mailform I have come across, which I have modified to suite my needs but having an issue regarding ascii encoding in the mail header.
Since Mozilla Thunderbird must have an appropriate encoding to read special characters like åäö, in my case these are swedish letters, I must have the encoding correctly.
I only need this for subject field and recipient field. As of now all special characters are replaced with ? (question mark)
This code is what I believe is for the subject and recipient header:
$sent_email = mail("$receiver", "Förfrågan: $content->title".$messageproper, "From: $name <$email>");
the "content->title" is the dynamic title taken from the page where the form is at.
I do not quite understand what the $messageproper is for exactly...
Here below is the full code. If anyone finds something that could be improven then please patch me up.
<?phpforeach($_POST as $key => $value){
$_SESSION['form'][$key]=$value;
}// ----------------------------------------------------------------------------- //
// ------------------ Configuration -------------------------------------------- //
// -------- YOU CAN AND SHOULD TAKE 5 MINUTES TO CUSTOMIZE THIS ---------------- //$receiver = "mail@home.net"; // Your email.
$v_w = "er2009"; // Write a word for the user to write, in order to unable bots to send emails.
// Examples: 2 + 2 = ? then the v.w. shall be 4... or what color is the sky ?
// answer shall be blue, ect... :)
$yoursitename = "My Site"; // Write your site name.$url_2_contactf = sefRelToAbs( 'index.php?option=com_content&view=article&id='.$menu->id.'&catid='.$content->catid.':events&Itemid=13'); // URL to reload the same page
$field_size = 30; // Chose the size you want the fields to be. Default 30.
$textarea_cols = 40; // Chose how many columns you want the text area to have. Default 30.
$textarea_row = 7; // Chose how many rows you want the text area to have. Default 10.// ----- Custom Error Messages
$er_name = 'Fyll i namnet på kontaktpersonen.<br>'; // Enter your name
$er_email_miss = 'E-post fältet är tomt. <br>Hur ska vi annars kunna svara?'; // Email field is empty
$er_email = 'E-post adressen ser ut att vara felaktig.<br>'; // Email is invalid or empty
$er_adress = 'Var god fyll i adressen.<br>'; // There is No Subject
$er_tel = 'Vi kan behöva ha Erat telefonnummer, <br>ifall vi måste kontakta er.'; // Enter a Message
$er_vw = 'Var god fyll i verifieringskoden.<br>'; // Enter Verification Word
$er_wvw = 'Felaktig verifieringskod.<br>'; // Wrong Verification Word$acfv = '3.8'; // Version. Don't edit
// ----------------------------------------------------------------------------- //
// ------------------ Notes ---------------------------------------------------- //
// ----------------------------------------------------------------------------- ///* This is a free script. From time to time I update it. It is your
responsability to check for updates once in a while.
*/// ----------------------------------------------------------------------------- //
// ------------------ DO NOT EDIT BELLOW THIS ---------------------------------- //
// ----------------------------------------------------------------------------- //// Lägg till varje ny rad som finns med i Mail Form, annars kommer den ej visas i mailet
if(isset($_POST['send']))
{
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$adress = htmlspecialchars($_POST['adress']);
$tel = htmlspecialchars($_POST['tel']);
$pax = htmlspecialchars($_POST['pax']);
$datefr = htmlspecialchars($_POST['datefr']);
$dateto = htmlspecialchars($_POST['dateto']);
$msg = htmlspecialchars($_POST['msg']);
$vword = htmlspecialchars($_POST['vword']);
$ip = $_SERVER['REMOTE_ADDR'];
// CHECK IF EMAIL IS VALID ////////////////////////////////////////////////////////////////////
function CheckMail($email)
{
if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\.[a-z]{2,4}$", $email))
{
return true;
}
else
{
return false;
}
}
// CHECK IF THE FORM HAS BEEN FILLED //////////////////////////////////////////////////////////
if(empty($email))
{ mosRedirect( $url_2_contactf,
$tag_b4_message."".$tag_bitwin_er_msg_b4."".$er_email_miss."".$tag_bitwin_er_msg_af);
}
// CHECK IF EMAIL IS VALID ////////////////////////////////////////////////////////////////////
if(CheckMail($email) != true)
{ mosRedirect( $url_2_contactf,
$tag_b4_message." ".$tag_bitwin_er_msg_b4." ".$er_email." ".$tag_bitwin_er_msg_af);
} /////////////////////////////////////////////////////////////////////////////////////////////
if(empty($name))
{ mosRedirect( $url_2_contactf,
$tag_b4_message." ".$tag_bitwin_er_msg_b4." ".$er_name." ".$tag_bitwin_er_msg_af);
}
if(empty($adress))
{ mosRedirect( $url_2_contactf,
$tag_b4_message." ".$tag_bitwin_er_msg_b4." ".$er_adress." ".$tag_bitwin_er_msg_af);
}
if(empty($tel))
{ mosRedirect( $url_2_contactf,
$tag_b4_message." ".$tag_bitwin_er_msg_b4." ".$er_tel." ".$tag_bitwin_er_msg_af);
}
if(empty($vword))
{ mosRedirect( $url_2_contactf,
$tag_b4_message." ".$tag_bitwin_er_msg_b4." ".$er_vw." ".$tag_bitwin_er_msg_af);
}
if($vword != $v_w)
{ mosRedirect( $url_2_contactf,
$tag_b4_message." ".$tag_bitwin_er_msg_b4." ".$er_wvw." ".$tag_bitwin_er_msg_af);
}
////////////////////////////////////////////////////////////////////
// IF THEY ARE NOT EMPTY
if(!empty($name) && !empty($adress) && !empty($tel) && !empty($vword) && CheckMail($email) == true)
{
if($vword == $v_w)
{
$headers .= "From: ".$email."";
// WE STORE THE MESSAGE IN A VARIABLE
$messageproper =
sefRelToAbs( $url_2_contactf ) ."\n".
"Från: $name - $email \n\n".
"------------------------- $yoursitename -------------------------\n\n" .
"Namn: $name\n" .
"Adress: $adress\n" .
"Tel: $tel\n\n" .
"Pax: $pax\n".
"Datum från: $datefr\n".
"Datum till: $dateto\n" .
"Meddelande: $msg\n" .
"\n---------------------------------------------------------------\n".
"Skickat från IP numret: $ip" ;
$sent_email = mail("$receiver", "Förfrågan: $content->title".$subject, $messageproper, "From: $name <$email>");
if($sent_email)
{ mosRedirect("$url_2_contactf","Tack!<br>Din förfrågan är skickad.<br> Vi kommer svara Er så snart som möjligt");
}
}
}
}
else
{?>
<!-- EDIT BELLOW -->
<hr />
<p>Skicka en bokningsförfrågan genom formuläret nedan.</p>
<form name="mailform" class="form" method="post" action="<?php $url_2_contactf ?>"><br />
<fieldset>
<legend> Kontaktuppgifter </legend>
<label for="name">Namn: *</label><br />
<input type="text" name="name" id="name" size="<?php echo $field_size; ?>"
value="<?php echo @$_SESSION['form']['name']; ?>" /><br />
<label for="adress">Adress: * <span>(gata,postnr, ort)</span></label><br />
<input type="text" name="adress" id="adress" size="<?php echo $field_size; ?>"
value="<?php echo @$_SESSION['form']['adress']; ?>"/><br />
<label for="tel">Telefon: *</label><br />
<input type="text" name="tel" id="tel"
value="<?php echo @$_SESSION['form']['tel']; ?>" size="<?php echo $field_size; ?>" /><br />
<label for="email">E-post: *</label><br />
<input type="text" name="email" id="email" size="<?php echo $field_size; ?>"
value="<?php echo @$_SESSION['form']['email']; ?>" /><br />
</fieldset><fieldset>
<?php include 'veri_text.txt';?>
</fieldset><fieldset>
<legend> Reseuppgifter </legend><label for="pax">Antal personer:</label>
<select name="pax" id="pax">
<option value="1">1</option> <option selected value="2">2</option>
<option value="3">3</option> <option value="4">4</option><option value="5">5</option> <option value="6">6</option>
<option value="7">7</option> <option value="8">8</option>
<option value="9">9</option> <option value="10">10</option>
</select><br /><br />
<label for="datefr">Datum från:</label>
<label class="dateto" for="dateto">Datum till:</label><br />
<input type="text" readonly="readonly" maxlength="100" size="8" name="datefr" class="text_area" id="datefr" onclick="return showSobiCalendar( 'datefr', 'datefr_calendarButton');"
value="<?php echo @$_SESSION['form']['datefr']; ?>" />
<input class="datebut" type="reset" value=" ... " onclick="return showSobiCalendar( 'datefr', 'datefr_calendarButton');" class="button" id="datefr_calendarButton" name="reset"/>
<input type="text" readonly="readonly" maxlength="100" size="8" name="dateto" class="text_area" id="dateto" onclick="return showSobiCalendar( 'dateto', 'dateto_calendarButton');"
value="<?php echo @$_SESSION['form']['dateto']; ?>"/>
<input class="datebut" type="reset" value=" ... " onclick="return showSobiCalendar( 'dateto', 'dateto_calendarButton');" class="button" id="dateto_calendarButton" name="reset"/><br /><label for="msg">Meddelande: </label><br />
<textarea name="msg" id="msg" rows="<?php echo $textarea_row; ?>" cols="<?php echo $textarea_cols; ?>" ><?php echo @$_SESSION['form']['msg'];?></textarea><br />
</fieldset>
<fieldset>
<input class="button" type="submit" name="send" value="Skicka förfrågan" /> <br />
</fieldset>
</form><!-- EDIT ABOVE -->
<?php
// Cleanses all fields after revisit
unset($_SESSION['form']);
} ?><?php include 'jsdate.txt'; ?>
/Thanks
I have been going through that page for a few days and tested quite a few alternatives to make it fit to my form... without any success of course. Results are the same. The mailbody displays all fine, just not in the mail header, as in Subject and Recepient
If you or anyone could try giving me alternatives to test, then I would gladly try.
Thanks
=UTF-8?B?[Base64 encoded Subject]?=
The B stands for Base64 and is simpler I think.
why encoding?
because you are not allowed to use 8-bit ASCII in mail headers only 7-bit ASCII is allowed and pure utf-8 uses 8-bit ASCII characters.
And I doubt if MAIL function supports special characters in the "recipient" field !
And [in.php.net ] has helluva info about mail bits and pieces...
Cheers :)
Now I compared your code with my code and finally I saw something I could modify.
This was my original,
$sent_email = mail("$receiver", "Förfrågan: $content->title".$subject, $messageproper, "From: $name <$email>");
And with the help from your code I merged it like this
$sent_email = mail("$receiver", "=?UTF-8?B?".base64_encode("Förfrågan: $content->title".$subject )."?=", $messageproper, "From: $name <$email>");
Subject: =?UTF-8?B?RsO2cmZyw6VnYW46IEJ1c3NyZXNh?=
Now all å ä and ö displays fine in Thunderbird´s subject. It is not so vicious that the same cannot be applied to the recipients fild. Just as long the subject is solved I am happy :)
Thanks for your help!