Gmail displaying source host on recipient when using PHP mail()

Asked

Viewed 355 times

0

Hi, I need to send automated emails as requested by the user. I have the function that makes this sending but in the server the sender stays this way in gmail:

contact site.com for br232.hostgator.com.br

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

CONTACT OF THE WEBSITE ([email protected])

Including with the sender’s name and not only the email, so:

Remetente (From) no Gmail

Explanation of gmail: http://support.google.com/mail/answer/1311182?hl=pt-BR

  • 1

    no, gmail even provided an explanation for this: https://support.google.com/mail/answer/1311182?hl=pt-BR

  • I put an image

  • use the gmail email!

  • Okay, I get the problem, I edited my answer, please try it. I edited the title of the answer to make it a little more intuitive for other people who have the same problem to find your question ;)

  • 2

    I think you’re missing an important piece of information in the answers given so far: the additional parameter -f in case PHP is using sendmail, to precisely set the sending host.

  • @Bacco did not know that this was necessary with SMTP, I will do a test only with mail(); and Gmail, your comment seems an embryo of a response (as the bfavaretto would say)

  • @Guillhermenascimento ended up posting a "minireply" for appearing a duplicate with the same problem.

Show 2 more comments

3 answers

1


A possible solution to remove this would be to use Phpmailer to upload.

First download via Poser in your project folder (your use Composer):

composer require phpmailer/phpmailer

Or download the latest Release on: https://github.com/PHPMailer/PHPMailer/releases

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

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

email.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 to use Gmail as "sender" you will have to unlock in Gmail settings, the settings to use your Gmail account to send emails is:

//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 problems see:

  • 1

    Okay! It worked! Vlw!

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, the -f:

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

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

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

Important: not always the sendmail allows this override. Some extra configuration may be required directly on the system.

0

Include the desired information in the header, 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 function mail in documentation.

  • Actually the line break in \n will suit Unix-like and Windows, it would be better to exchange both for constant PHP_EOL. Understand as a constructive criticism.

  • Updated, thanks @Guilhermenascimento

  • I edited the answer because from: foo@foo and from: foo <foo@foo> would be the same thing and the PHP_EOL already solved, in fact I think in windows no longer use r n, use n even. Only the Notepad.exe text editor uses r n. If you disagree do rollback =)

  • appears the name of my host next to the email!!!

  • face, but in gmail is like this the sender "Contact [email protected] by br232.hostgator.com.br

  • @APS is this the title of the email? Have you changed anything in Subject? You have registered as contact at any time the email?

  • not registered as contact the email, the header is as in this post:

  • $headers = "MIME-Version: 1.1". PHP_EOL; $headers .= " Content-type:text/html; charset=UTF-8". PHP_EOL; $headers .= " From: Kloubbe <[email protected]>". PHP_EOL; // sender on LINUX $headers .= " Return-Path: [email protected]". PHP_EOL; // Return-path

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.