Sending email with dynamic inputs

Asked

Viewed 32 times

0

I’m using a code like this: https://codepen.io/leoamrl/pen/Yayzoj to add fields to my form.

How php would send forms like this?

Follows current php code:

<?php header("Content-type: text/html; charset=utf-8"); ?>

<?PHP
if ( isset( $_POST['nome'] ) && !empty( $_POST['nome'] ) &&
 isset( $_POST['email'] ) && !empty( $_POST['email'] ) &&
 isset( $_POST['telefone'] ) && !empty( $_POST['telefone'] ) ) {
  $nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_MAGIC_QUOTES);
  $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
  $telefone = filter_input(INPUT_POST, 'telefone', FILTER_SANITIZE_MAGIC_QUOTES);

$nome_amigo = $_POST['nome_amigo'];
$telefone_amigo = $_POST['telefone_amigo'];
$data = date('d-m-Y H:i:s');

$to = '[email protected]';
$subject = $nome.' - [MEU FUTURO AQUI]';
$body = '
<strong>Novo Lead da Landing Page MEU FUTURO AQUI ['.$data.']</strong><br>
---<br>
<strong>Dados Pessoais</strong><br>
NOME: '.$nome.'<br>
EMAIL '.$email.'<br>
TELEFONE: '.$telefone.'<br>
NOME AMIGO: '.$nome_amigo.'<br>
TELEFONE AMIGO: '.$telefone_amigo.'<br>
<br>
<small><em>Jogo.Digital</em></small>
';

$csv = $nome.';'.$email.';'.$telefone.';'.$nome_amigo.';'.$telefone_amigo.';'.$data.';BREAK'.PHP_EOL;

$fp = fopen("lista2.csv", "a");
$escreve = fwrite($fp, $csv);
fclose($fp);

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=UTF-8";
$headers[] = 'From: Jogo Digital <[email protected]>';
$headers[] = 'Reply-To: '.$nome.'<'.$email.'>';
$headers[] = 'Cc: Wesley <[email protected]>';
$headers[] = 'Cc: Renata <[email protected]>';
$headers[] = 'Cc: Roberto <[email protected]>';
$headers[] = 'Cc: Leda <[email protected]>';
$headers[] = 'Cc: Leo <[email protected]>';
$headers[] = "X-Mailer: PHP/".phpversion();

mail($to, $subject, $body, implode("\r\n", $headers)) or die("Error!");

echo '<script type="text/javascript">';
echo 'alert ("Parabéns!!!!! Você deu um passo para o novo. Agradecemos o interesse e em breve entraremos em contato.");';
echo 'window.location.replace("http://meufuturoaqui.com.br")';
echo '</script>';

exit;

}
?>
  • where your html is ?

  • I updated the codepen link

1 answer

1


From a foreach in your example fields

foreach($_POST['nome-amigo'] as $key => $val){
    $nome_amigo = $_POST['nome-amigo'][$key];
    $telefone_amigo = $_POST['telefone-amigo'][$key];
    $data = date('d-m-Y H:i:s');

    $to = '[email protected]';
    $subject = $nome.' - [MEU FUTURO AQUI]';
    $body = '
    <strong>Novo Lead da Landing Page MEU FUTURO AQUI ['.$data.']</strong><br>
---<br>
    <strong>Dados Pessoais</strong><br>
    NOME: '.$nome.'<br>
    EMAIL '.$email.'<br>
    TELEFONE: '.$telefone.'<br>
    NOME AMIGO: '.$nome_amigo.'<br>
    TELEFONE AMIGO: '.$telefone_amigo.'<br>
    <br>
    <small><em>Jogo.Digital</em></small>
    ';

    $csv =$nome.';'.$email.';'.$telefone.';'.$nome_amigo.';'.$telefone_amigo.';'.$data.';BREAK'.PHP_EOL;

    $fp = fopen("lista2.csv", "a");
    $escreve = fwrite($fp, $csv);
    fclose($fp);

}

Because you are receiving an array of $_POST['name-friend'] & $_POST['phone-friend'] so with a foreach you can read the data of all of them, note that your field in the script is changing from name_friend to name-friend and so on.

  • 1

    It worked. Thank you very much.

Browser other questions tagged

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