Adding Fields in the Body of a PHP Email

Asked

Viewed 71 times

1

The problem is that I have a contact form, working all right, but I need it to contain more fields in the body of the email message.

For example, I have some fields like company, phone, etc... I would need that, in the body of the email, have them too.

Let’s go to the current situation of CODE PHP

    <?php
    $name       = @trim(stripslashes($_POST['name'])); 
    $from = trim($_POST["email"]);
    $subject    = "Contato via Site"; 
    $message = trim($_POST["message"]);
    $to         = "[email protected]";

    $headers   = array();
    $headers[] = "MIME-Version: 1.0";
    $headers[] = "Content-type: text/plain; charset=iso-8859-1";
    $headers[] = "From: {$name} <{$from}>";
    $headers[] = "Reply-To: <{$from}>";
    $headers[] = "Subject: {$subject}";
    $headers[] = "X-Mailer: PHP/".phpversion();

    $header = implode("\r\n", $headers);
    if (mail($to, $subject, $message, $header)) {

    ?>
        <script language="JavaScript">
        alert("Mensagem enviada.");
        </script>
        <script language='javascript'>history.back()</script>;
        <?
    }

    ?>

Unfortunately, my knowledge in PHP is virtually null.

In short:

I have a contact form, in it is sent only one field in the body of the email, the message. I need to add 3 more fields in the body, Name, Company and Phone.

  • Create a formatted string and assign it to $message. If you want then you can build a more incremented template.

1 answer

2


Only concatene as Strings:

$message .= $nome . "\n";
$message .= $empresa . "\n";
$message .= $telefone; 
  • It worked, but in the email cup only appears the message and the name, company and phone no

  • already have these fields in the HTML? and if they are already checking whether the values in the PHP !

  • It worked out buddy, thanks again haha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.