> ## Documentation Index
> Fetch the complete documentation index at: https://docs.saninternet.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Criar formulário de envio de e-mail

> Tutorial de como criar um formulário html para envio de e-mail utilizando a biblioteca PHPMailer do PHP.

**Na parte html**, você deve ter o formulário aonde o usuário informa os dados. Abaixo está um exemplo:

```
<form action="file.php" method="post">
   <input type="text" name="name" placeholder="Seu nome">
   <input type="email" name="from" placeholder="Seu email">
   <input type="text" name="to" placeholder="Destinatário">
   <input type="text" name="subject" placeholder="Assunto">
   <textarea name="content" placeholder="Mensagem"></textarea>
   <button type="submit">Enviar</button>
</form>
```

**Na parte php**, esse formulário vai ser enviado ao arquivo "file.php", e este tem o código abaixo:

```
<?php
$host = "seudominio.com.br";
$port = "587";
$username = "email@exemplo.com";
$password = "123";

$from_email = $_POST['from'];
$from_name = $_POST['name'];
$subject = $_POST['subject'];
$content = $_POST['content'];
$to = $_POST['to'];

require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = $host;
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->Port = $port;
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($from_email, $from_name);
$mail->Subject = $subject;
$mail->MsgHTML($content);
$mail->AddAddress($to);

if ($mail->Send()) {
   echo 'Enviado com sucesso';
} else {
   echo 'Erro: '.$mail->ErrorInfo;
}
```

Além de você ter que trocar as variáveis $host, $port, $username e $password, esse php utiliza uma outra classe pra enviar emails, que pode ser feito o download através do endereço [https://github.com/PHPMailer/PHPMailer](https://github.com/PHPMailer/PHPMailer). Na página de download há mais detalhes das funcionalidades desta classe.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.