Gmail displaying source host on the recipient when I use PHP mail ()

0

Hi, I need to send automatic emails as the user requests. I have the function that does this sending but on the server the sender stays that way in gmail:

  

[email protected] by br232.hostgator.com.br

I need it as if it was sent by smtp, is it possible?

  

SITE CONTACT ([email protected])

Even with the name of the sender and not just the email, like this:

  

Explaingmail: link

    
asked by anonymous 15.03.2016 / 21:57

3 answers

1

One possible solution to remove this would be to use PHPMailer to upload.

First download via composer into your project folder (your using composer):

composer require phpmailer/phpmailer

Or download the latest Release at: link

If you downloaded extract the PHPMailer-5.2.14 folder in the folder that your script is, the folder structure should look something like:

./projeto
    |---- enviaremail.php
    |---- PHPMailer-5.2.14/
            |---- PHPMailerAutoload.php
            |---- ...

enviaremail.php :

<?php
require 'PHPMailer-5.2.14/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Pra depurar o código remova o // do começo

$mail->isSMTP();                                      // Define como SMTP
$mail->Host = 'smtp.exemplo.com';                     // Endereço do SMTP
$mail->Port = 25;                                     // Porta do SMTP
$mail->SMTPAuth = true;                               // Autenticação no SMTP
$mail->Username = '[email protected]';              // SMTP username
$mail->Password = 'senha';                            // SMTP password

//$mail->setFrom('[email protected]', 'Mailer');       //comentei esta linha pois o Gmail irá detectar se tentar alterar o "from", mas pode tentar

//Adiciona destinatários:

$mail->addAddress('[email protected]', 'Joe User');     // Adiciona destinatário
$mail->addAddress('[email protected]');               // Destinatário sem nome

$mail->addReplyTo('[email protected]', 'Information');

//Manda como cópia
$mail->addCC('[email protected]');

//Manda como cópia oculta
$mail->addBCC('[email protected]');

//Anexos se precisar    
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');


//Habilita HTML
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Assunto';
$mail->Body    = 'Mensagem <b>teste</b>';
$mail->AltBody = 'Mensagem em texto, alternativa ao HTML';

if(!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Mensagem enviada';
}

If you're going to use Gmail as a sender, you'll have to unlock it in Gmail settings, so the settings for using your Gmail account to send emails are:

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';

// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// Se a rede não suportar SMTP sobre IPv6

$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "yourpassword";

For other issues see:

16.03.2016 / 01:49
1

According to own PHP documentation , you can use additional parameters for the mailer of the system.

If the system is based on sendmail , there is a parameter that can help, -f :

<?php 
    mail($to, $subject, $message, $headers, "[email protected]"); 
?>

This causes sendmail to identify itself with the given name when making the SMTP connection.

By default, sendmail uses the OS configuration, and this is what generates the divergence of the addresses, and consequently causes Google to show the two addresses, "from", which is used by PHP, and the "path", which is removed from the protocol negotiation. -f does the override of this behavior.

Important: sendmail does not allow this override. Some extra configuration may be required directly in the system.

    
22.03.2016 / 17:21
0

Include the desired information in the header, for example:

$headers  = "MIME-Version: 1.1".PHP_EOL;
$headers .= "Content-type: text/plain; charset=iso-8859-1".PHP_EOL;
$headers .= "From: Meu Nome <[email protected]>".PHP_EOL; // remetente no LINUX
$headers .= "Return-Path: [email protected]".PHP_EOL; // return-path
$envio = mail("[email protected]", "Assunto", "Texto", $headers);

See more about the mail function in documentation .

    
15.03.2016 / 22:01