I have the following code to send an email to a gmail account, it works but not quite right since I would like the name of the person that passed through the POST method and the email that enters it to appear when the email arrives. It appears as if I sent it myself.
<?php
require_once('class.phpmailer.php');
require_once('class.smtp.php');
require_once('PHPMailerAutoload.php');
require('vistas/mail.view.php');
define("destino", "[email protected]");
define("farmacia", "Guillermo");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$nombre=filter_var($_POST['nombre'], FILTER_SANITIZE_STRING);
$de=filter_var($_POST['de'], FILTER_SANITIZE_EMAIL);
$telefono=filter_var($_POST['telefono'], FILTER_SANITIZE_STRING);
$asunto=filter_var($_POST['asunto'], FILTER_SANITIZE_STRING);
$mensaje='
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>Cuerpo del mensaje</h1>
<table border="1">
<tr>
<td>Uno</td>
<td>Dos</td>
</tr>
</table>
</body>
</html>
';
$mensaje.=filter_var($_POST['mensaje'], FILTER_SANITIZE_STRING);
$correo = new PHPMailer(); //Creamos una instancia en lugar usar mail()
$correo->IsSMTP();
$correo->SMTPOptions = array('ssl' => array('verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true));
// optional
// used only when SMTP requires authentication
$correo->SMTPAuth = true;
$correo->SMTPSecure = 'tls';
$correo->Host = 'smtp.gmail.com';
$correo->Port = 587;
$correo->Username = '[email protected]';
$correo->Password = 'password';
//Usamos el SetFrom para decirle al script quien envia el correo
$correo->SetFrom($de, $nombre);
//Usamos el AddReplyTo para decirle al script a quien tiene que responder el correo
$correo->AddReplyTo($de, $nombre);
//Usamos el AddAddress para agregar un destinatario
$correo->AddAddress(destino, farmacia);
//Ponemos el asunto del mensaje
$correo->Subject = $asunto;
/*
* Si deseamos enviar un correo con formato HTML utilizaremos MsgHTML:
* $correo->MsgHTML("<strong>Mi Mensaje en HTML</strong>");
* Si deseamos enviarlo en texto plano, haremos lo siguiente:
* $correo->IsHTML(false);
* $correo->Body = "Mi mensaje en Texto Plano";
*/
$correo->MsgHTML($mensaje);
//Si deseamos agregar un archivo adjunto utilizamos AddAttachment
//$correo->AddAttachment("images/phpmailer.gif");
$correo->CharSet = "UTF8";
//$correo->Encoding = "quotedprintable";
//Enviamos el correo
if(!$correo->Send()) {
echo "Hubo un error: " . $correo->ErrorInfo;
} else {
echo "Mensaje enviado con exito.";
}
}
?>
The following image shows how the email arrives:
I hope you can help me.