I got my PHP mail form working great and just have one last touch I wanted to add. Basically I am pulling the info from the form like this:
$fields{"Comments"} = "Additional Comments by Fitter: ";
The HTML for this field is:
<textarea name="Comments" id="styled" onfocus="this.value=''" onblur="setbg('white')">Enter your comment here...</textarea>
The email would then displays:
Additional Comments by Fitter: Enter your comment here...
I would like the info being pulled to be a different color. I have tried:
$fields{"<span style='color:red;'>Comments</span>"} = "Additional Comments by Fitter: ";
but then no comments are displayed (I assume I am not wrapping it right). I have:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
At the top of the file as well. Any help would be great. Thanks.
You need to wrap the value, not the key.
Related
I have a questionnaire form on the web. After filling in this form, I send it to me by email, using the PHP function mail(). The form body and the data it contains, including the private message are displayed correctly on gmail.com. The problem, however, occurs in the header of the email itself. Some characters are displayed incorrectly.
Here is a sample header:
$headers = "Content-Type:text/html; charset=utf-8\r\n";
$headers .= "From:" .$email . "\r\n";
$headers .= "Reply:" . $email . "\r\n";
$headers .= "X-Mailer: PHP/". phpversion() . "\r\n" ;
Required display of email subject:
Nový dotaz -- námět, od Fořt Petr <p.fort1990#gmail.com>
Simultaneous displaying of the subject:
Nový dotaz -- námÄ☒t od: FoÅ☒t Petr <p.fort1990#gmail.com>
The squared times symbol is more like a rectangle.
Is anything wrong? Or where should I look for a mistake?
I'm not sure \r\n works on all platforms
see : Which line break in php mail header, \r\n or \n?
instead
("xxx\r\n\yyy");
use
Header('xxx');
Header('yyy');
or use PHP_EOL, not "\r\n"
Problem solved. My hosting provider uses different character encoding for the headers - I can't explain why, but the following php function will do it all.
function recode_to_utf8 ($text, $encoding = "utf-8")
{
return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}
And now all you have to do is send an email using the mail () method in combination with the method defined above recode_to_utf8(). Like this:
mail(recode_to_utf8($mail_to), recode_to_utf8($subject), recode_to_utf8($message), recode_to_utf8($headers));
I hope it helps others if they have the same problem as me.
I'm trying to figure out with email confirmation function via email, received by user.
If I have <input type='submit' value='Accept' id='btn1'>, (which is correct way shown by ADyson, instead double quotes: <input type="submit" value="Accept" id="btn1">), then how do I use PHP $_POST['acceptval'] for confirmation from this HTML, which must record value to MySQL database from received message form by included ID, then to pass this condition from the website.
So, if I will include require 'connect.php'; to code below, and will get data for to, message, subject, I will receive message to my userreceiveremail#gmail.com, then I want allow user press button, and record value to his record field, but I can't figure out, how it works:
<?php
$to = "userreceiveremail#gmail.com";
$subject = "Confirmation notification";
$message = "
<html>
<head>
<title>Accept registration</title>
</head>
<body>
<p>Hello, dear User! Please, accept your registration</p>
<input type='submit' value='Accept' id='btn1'>
</body>
</html>
";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <senderemail#gmail.com>' . "\r\n";
$headers .= 'Cc: Some text' . "\r\n";
mail($to,$subject,$message,$headers);
echo "Mail sent successfully";
?>
Maybe my scenario does not correspond to the classic or correct approach to solving this particular task, because I'm just trying to figure out, how to get desired result, anyway yet I can't find any close example for this.
Any advice, guide or example would be very helpful
So my form is creating Dynamically forms based on the Total Person.
It the Total Person is: 3
Then 3 Forms will appear with each different "valuenames" like:
<input name="lastname[1]">
<input name="lastname[2]">
<input name="lastname[3]">
But how can I call the results of each input in $POST PHP?
See this picture what I mean:
I need to show, get, call that values in php.
So it is the print, I need to post each values of this arrays to send the results as a mail:
**[lastname] => Array ( [1] => MARVIN [2] => JIM [3] => ZOO**
Example:
Lastname 1: MARVIN
Lastname 2: JIM
Lastname 3: ZOO
The following code will postfix a variable with a number to evaluate, and the for loop will loop over 0 to 4 (which is 5 fields in total) and then echo out the result.
You will have to adapt the code to match your other form variables.
<?php
for($i=0;$i<5;$i++){
$var = 'lastname'.$i;
if(isset($_POST[$var])){
echo $_POST[$var];
}
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="<? $_SERVER['PHP_SELF'];?>" method="post">
<input type="text" name="lastname1" />
<input type="text" name="lastname2" />
<input type="text" name="lastname3" />
<input type="text" name="lastname4" />
<input type="text" name="lastname5" />
<input type="submit" value="test">
</form>
</body>
</html>
I see that you do get the values on your array. but your php code is beginners, cut, paste, adaptation and "happy thinking", We all started one day, so I will review it in some detail.
You initiate your loop with
$message .=
should be:
$message =
As you are initiating a mesagge, not concatenating to it...
Then you follow it up with:
$message =
when now, you are concatenating to $message, it should be
$message .=
read: languaje operators
Then kind of really basic stuff,
Then you follow messsage = with "White space", so the php processor goes reading until a statement closure ";" is found. The parser wont find it because it keeps reading into yor message string until "From" is found.
That is because the parser either finds a literal string between "" or '' or a concatenation operator ".", for which the parser will add the variable value between concatenators.
As you can check, those two conditions are met until the "From" is encountered at which point the parse will throw an error because it is either expecting an statment closure ";" or a new concatenation operator, but finds a literal.
If you did not catch it probably you need to turn on error reporting:
Put this at the begining of the page: (Do take those out in production!!)
error_reporting(E_ALL);
ini_set("display_errors", 0);
Happy coding!
Nevertheless, althoug some times work, it is absolutely recommended no to span comands over several lines.
There are two choices, concaternate for every line, like you have in the $headers secction...
Or use the heredoc syntax although that takes some practice to master.
Heredoc sintax will take anythin between the "<<
Additionally the parser will greedily look for variables or unidimensional array variables and replace them with their values, more complex values should be enclosed by curly braces, just for helping you start, here is the revised php code with the $meesage block under heredoc syntax an, but do read this basic page if you want to make some progress:
php strings manual
here is the revised php code:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 0);
foreach($_POST['name'] as $index => $name)
{ // Just for clarity sake,Obtain this person information. similarly could have been done inside the $message build,
// Left bornday to show how is that done......
$Passenger = ($index);
$Name = $name;
$LastName = $_POST['lastname'][$index];
$BornDay = $_POST['bornday'][$index];
$Trip = $_POST['destination'][$index];
$Persons = $_POST['vol'][$index];
$Kids = $_POST['kinderen'][$index];
$Babys = $_POST['Babys'][$index];
$message .= <<<EOD
Trip: $Trip<br><br>
Persons: $Persons<br>
Kids: $Kids<br>
Babys: $Babys<br><br>
Personsinformation: $Name $LastName {$_POST['bornday'][$index] <br><br>
EOD;
// DBG // Show the message
echo "The $index message is: $message<br>";
// Sending mail dpends a bit on you enviroment, supposig this applies:
$headers = 'From:' . $_POST['email'] . "\r\n";
$headers .= 'Reply-To:' . $_POST['email'] . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; ";
$headers .= "boundary=$num\r\n";
$headers .= "--$num\r\n";
// Define the message section
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Content-Transfer-Encoding:8bit\r\n\n";
$headers .= "$message\r\n";
$headers .= "--$num\r\n";
// Send email
mail ($to, $subject, '', $headers);
print_r($_POST);
}
?>
A user fills out a form which is sent to my email via the variables their data is read into.
I want to have headers above each users entry, and have these appear bold in the email.
The email should read:
OCCUPATION:
Data the user entered.
I have tried
$Occupationheader = "<strong>"."OCCUPATION"."</strong>"."\n\n" ;
and
$Occupationheader = "<strong>OCCUPATION:<strong>\n\n";
The data is sent to the email, as follows: (this works, but want to format the headings in bold).
mail( "myemailaddress", "subjectmatter",
$Occupationheader.$Occupation);
Any ideas?
Thanks guys.
$mailContent = "<html>
<body>
<strong>HEADER</strong>
<br/>message
</body>
</html>";
NOTE: This is provided as an additional answer.
Without seeing full source code, am providing my (additional) answer below.
The following, need to be present in your code, in order to send out Emails in HTML format:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
As per the PHP manual on the subject: http://php.net/manual/en/function.mail.php
The following PHP code works perfectly, but it is not doing line breaks for some reason.
PHP:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=utf-8\n";
$headers .= "From: '".$title."' <".$store_email."> \n";
$subject = "New Payment Received";
//MESSAGE
$message = "New payment was successfully recieved through paypal payment terminal:";
$message .= "\r\n\nFrom ".$paypal->pp_data['payer_email'];
$message .= "\r\nPaid: ".$paypal->pp_data['payment_gross']." ".$paypal->pp_data['mc_currency'];
$message .= "\r\nDate: ".date('d/m/Y');
$message .= "\r\nTime: ".date('g:i A');
mail($admin_email,$subject,$message,$headers);
Any wonder what's wrong? Thanks in advance.
You're sending HTML e-mail. Line breaks have no meaning in HTML, you'll need <br /> tags.
The direct answer ceejayoz gives is correct and to the point in that the html element <br> is needed because it is a html email.
The bigger issue is that not all email is readable in html (example: user doesn't allow html emails). Anyone sending email should send it in 2 parts. One being a html formatted message and the other "alternative" in plain text. In that way the recipient will be able to read the email regardless of email reader.
The \r\n line break works in plain text alternative part and in html<br> or other elements as needed to format.
Doing this will avoid the next question. Recipients are complaining my emails are blank.