Header error when sending email with PHP

Asked

Viewed 640 times

0

I’m trying to send an email via PHP:

$headers = "From: [email protected]" . "\r\n";
$mgm = "código" . $_GET['codigo'];

$enviaremail = mail($_GET['email'], 'Cadastro BUSCAFREE', $mgm, $headers);
if($enviaremail){
  echo "ok";
} else {
  echo "ERRO AO ENVIAR E-MAIL!";
}

I get nothing from forms. If I remove the headers works well. What’s wrong with it?

When I include the header, the code does not give error, returns 'ok', but the email does not arrive.

2 answers

3


Many emails often fall into SPAM or do not even reach the recipient due to filters on the servers, understand one thing, when you use the function mail() or the program sendmail generally does the sending is the server and not a user authenticated via SMTP, so mail servers for spam security will block things like unauthenticated emails.

It’s not putting half a dozen headers that will solve, like:

$headers  = "From: testsite < [email protected] >\n";
$headers .= "Cc: testsite < [email protected] >\n"; 
$headers .= "X-Sender: testsite < [email protected] >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n";
$headers .= "Return-Path: [email protected]\n";

And if it is receiving without the headers it is probably because the message arrives in PLAIN format (text) instead of HTML, and the server that receives the message allows this because PLAIN messages do not have much interaction which makes them safer than HTML, probably.

This has already been much discussed here on the site, it is something that is a lot talking about SPAM, and the practical and most guaranteed solution in PHP without a shadow of doubt is to use authenticated SMTP, there are projects that already solve this for you, as:

Note that authenticated emails have a limit of mails per hour or day, usually a limit of 100 messages per day, precisely to prevent people from using emails to make SPAM attacks.

How to install Phpmailer

If you are using Composer run the command in the project folder:

composer require phpmailer/phpmailer

And at the beginning of your file add this:

use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

If you don’t get down from https://github.com/PHPMailer/PHPMailer/releases the version compatible with your PHP and add this to the file header:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';

And then just below do something like this, of course the settings should follow the same as they would in an email client like Outlook or Thunderbird:

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient
    $mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Information');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    //Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
  • 1

    That’s basically it, I’ve been using Phpmailer forever.

2

Speak, Italo! Beauty?

Man, at Stackoverflow you have a lot of questions about that.

Take a look at the link below: https://stackoverflow.com/questions/566182/complete-mail-header

Apparently this is your answer.

$headers  = "From: testsite < [email protected] >\n";
$headers .= "Cc: testsite < [email protected] >\n"; 
$headers .= "X-Sender: testsite < [email protected] >\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "X-Priority: 1\n"; // Urgent message!
$headers .= "Return-Path: [email protected]\n"; // Return path for errors
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";

If this still doesn’t resolve, edit the question by placing the error that is returned when you include the header, ok?

Thanks, I hope I helped. :)

  • Oops, do I need to include this whole header or just whatever I want? I only included the first line (From) which is what I’m wanting, yet it didn’t work. I used the same question code (I just swapped the header line for the first line of the code Voce posted). it does not give error, printa 'ok' on the screen, but the email does not arrive

  • I tried to send the email with the full header exactly like the one Voce posted. returns ok, but will not

  • 2

    Dude, if the e-mail doesn’t arrive, it’s very likely the reason is a spam lock on the server. To avoid this headache, follow the tip of Guilherme Nascimento and use Phpmailer, no doubt is the best option. Another very interesting option would be to use Sendgrid. https://github.com/sendgrid/smtpapi-php

Browser other questions tagged

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