PHPMailer sends mail with the recipient's mail

1

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 = "UTF­8";
        //$correo->Encoding = "quoted­printable";

        //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.

    
asked by Guillermo Ricardo Spindola Bri 09.08.2016 в 21:28
source

3 answers

1

This is how the sender is placed:

$correo->From = $from;//correo remitente
$correo->FromName = $from_name;//nombre remitente

and so the recipients:

$destinatarios=explode(",", $destinatarios);//$destinatarios="[email protected],[email protected]"
if(!empty($destinatarios)){
foreach($destinatarios as $un_destinatario){
$correo->AddAddress($un_destinatario); //destinatarios
}
    
answered by 09.08.2016 в 21:38
0

Using PhpMailer , you have to configure it in this way when you send the email and name of the sender:

//Crea instancia PHPMailer.
$correo = new PHPMailer;
...
...
$correo->From = "[email protected]";
$correo->FromName = "Nombre remitente";

see here a example (English).

    
answered by 09.08.2016 в 23:18
0

You are setting the constants that you have in $correo->AddAddress . That is, in the first part of the code you put this:

define("destino", "[email protected]");
define("farmacia", "Guillermo");

After that, you go to $correo->AddAddress(destino, farmacia); , and you put the constants that you are going to send by email to the person. I want to assume that the constant destination has your mail, so whenever it is sent to your inbox it appears as me, because you are sending it to yourself.

Another thing, $correo->CharSet = "UTF­8"; is not written like that, it's $correo->CharSet = "UTF­-8";

Greetings! I hope you serve, any questions comment.

EDIT: Add the following from who sent you the email:

$correo->From = $de;

And to know the name of the person would be:

$correo->FromName = $para;
    
answered by 10.08.2016 в 18:50