Too many problems with PHPMailer

0

Yesterday I asked a question in which I sent an email with the PHPMailer function but I sent it blank. Today I try to send it and it arrives in my inbox but the problem is that it arrives as if I sent it to me, it is not taking into account the FROM data. Help please the code is as follows:

    <?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>
        </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.";
        }

        }
 ?>
    
asked by Guillermo Ricardo Spindola Bri 05.08.2016 в 17:52
source

2 answers

0

Finally I achieved it and was adding the following line:

$correo->SMTPOptions = array('ssl' => array('verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true));

and also adding the class:

require_once('class.smtp.php');
    
answered by 05.08.2016 / 20:16
source
1

Use this function:

function enviar_correo($destinatarios, $mail_asunto, $mail_contendio, $from, $from_name, $archivos_adjuntos_ruta,$archivos_adjuntos_temp){
$mail= new PHPMailer(); // defaults to using php "mail()"
$mail->CharSet = 'UTF-8';
$body= $mail_contendio;
$mail->IsSMTP(); // telling the protocol to use SMTP
$mail->Host = "smtp.farmaciassanasana.com.mx"; // SMTP server
$mail->From = $from;
$mail->FromName = $from_name;
$mail->Subject = $mail_asunto;
$mail->MsgHTML($body);
$destinatarios=explode(",", $destinatarios);
if(!empty($destinatarios)){
foreach($destinatarios as $un_destinatario){
$mail->AddAddress($un_destinatario); //destinatarios
}
}else{
return false;
}
if(!empty($archivos_adjuntos_ruta)){
foreach($archivos_adjuntos_ruta as $archivo){
$mail->AddAttachment($archivo); // attachment
}
}
if(!empty($archivos_adjuntos_temp)){
foreach($archivos_adjuntos_temp as $nombrearchivo=>$contenidoArchivo){
$mail->AddStringAttachment($contenidoArchivo,$nombrearch ivo,'base64');
}
}
$mail->Timeout = 20;
if($mail->Send()) {
return array(true);
}else {
return array(false,"Mailer Error: ".$mail->ErrorInfo);
}
}

and here are some examples to invoke:

$archivos_adjuntos_ruta=array($path1,path2);
$archivos_adjuntos_temp=array(utf8_decode($strfile PDF)=>$strContenidoPdf,utf8_decode($strNomArch)=>$ strContenidoXml);
enviar_correo($Email_destinatarios_string_separado_por_comas,$email_asunto, $mail_contendio_en_html,$correo_remitente,$nombre_remitente,array(),archivos_adjuntos_temp);//los archivos estan en variables temporales
enviar_correo($Email_destinatarios_string_separado_por_comas,$email_asunto, $mail_contendio_en_html,$correo_remitente,$nombre_remitente,$archivos_adjuntos_ruta,array()) ;//los archivos estan en rutas en disco
enviar_correo($Email_destinatarios_string_separado_por_comas,$email_asunto, $mail_contendio_en_html,$correo_remitente,$nombre_remitente,$archivos_adjuntos_ruta,archivos _adjuntos_temp);//ambas opciones al mismo tiempo

If you still have problems, it is most likely that it is a bad configuration in your SMTP server, or your badly armed html. And another thing, the function does not use login, but if you use it you will have to add these two lines:

$mail->Username   = "[email protected]";
$mail->Password   = "mipassword"
    
answered by 05.08.2016 в 18:23