DNA
DNA

Reputation: 137

php form mail function

I am using a mail function to mail it to my email adress, but i want it to send me a message with variables in it. (I have tried to concatenate it with concatenation point, but it doesn't seem to work, or I did not use it right)

The code:

<?php

$myEmail = "[email protected]";
$emailOnderwerp = "Contact form";
$naam = $_POST['naam'];
$email = $_POST['email'];
$onderwerp = $_POST['onderwerp'];
$bericht = $_POST['bericht'];
$mail1 = "<h1>";
$mail2 = "</h1><h2>Afzender:</h2><p>";
$mail3 = " (";
$mail4 = ")</p><h2>Bericht:</h2><p>";
$mail5 = "</p>";

mail("$myEmail","$emailOnderwerp",MESSAGE,"Content-type:text/html; charset=iso-8859-1"."\r\n"."From:[email protected]");
?>

I know MESSAGE is not supposed to be there, it is just to make it easy to see that the message needs to be placed there.

So I want the message to be $mail1 + $onderwerp + $mail2 + $naam + $mail3 + ...

How can I achieve this?

Upvotes: 0

Views: 56

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

As I mentioned in comments, you're overthinking this and there are a few simpler ways to go about this.

Either by changing your whole block to: (no need for all those variables)

$mail = "

<h1>
</h1><h2>Afzender:</h2><p>
 (
)</p><h2>Bericht:</h2><p>
</p>

";

then

mail("$myEmail","$emailOnderwerp",$mail,...

But, if you wish to continue using what you have now:

You can concatenate in a few ways, such as:

$mail = $mail1 . "" . $mail2 . "" . $mail3 . "" . $mail4 . "" . $mail5;

or

$mail = "$mail1 $mail2 $mail3 $mail4 $mail5";

Sidenote:

You're missing a closing semi-colon for:

mail($myEmail,"$emailOnderwerp",$message,...")

which would throw a parse error, if that is your actual code.

Upvotes: 2

giessebi
giessebi

Reputation: 168

When you use double quotes you can add the variables straight to your message string (although it does not read very well)

<?php

$myEmail = "[email protected]";
$emailOnderwerp = "Contact form";
$naam = $_POST['naam'];
$email = $_POST['email'];
$onderwerp = $_POST['onderwerp'];
$bericht = $_POST['bericht'];
$message = "<h1>$onderwerp</h1><h2>Afzender:</h2><p>($naam)</p><h2>Bericht</h2><p>$bericht</p>";

mail($myEmail,"$emailOnderwerp",$message,"Content-type:text/html; charset=iso-8859-1"."\r\n"."From:[email protected]")
?>

(you may want to check the subject though.... but technically it should work)

Another option would be something like

$message.= "text and tags";
$message.= $variable

Or you could use HEREDOC syntax

$message=<<<ENDOFMESSAGE
<tags> and $variables
...
ENDOFMESSAGE;

Also you need to add double quotes before "Content-type"...

Hope it helps.

Upvotes: 0

Related Questions