Problems sending php email

Asked

Viewed 663 times

1

I am trying to send emails to specific lists registered in the database, with the php mail function but the email is not sent. What can be the mistake?

Below the script for sending emails:

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    //realiza a conexão com o banco de dados
    require_once("includes/conexao.php");
    //monta o corpo da mensagem
    $corpo                  = file_get_contents("$_POST[mensagem]");
    //lista de e-mails que será enviada
    $lista                  = $_POST["lista"];
    //remetente (e-mail de quem envia)
    $remetente              = $_POST["remetente"];
    //pega o domínio do remetente
    $dominio                = substr(strrchr($remetente, "@"), 1);
    //assunto da mensagem
    $assunto                = $_POST["assunto"];

    //realiza o envio dos emails
    $sql                    = "SELECT DISTINCT email FROM dados WHERE lista = '".$lista."'";
    $query                  = mysqli_query($conexao, $sql);
    while($exibir           = mysqli_fetch_array($query)){
        //headers
        $destinatario       = $exibir["email"];
        $headers            = "MIME-Version:1.1"."\n";
        $headers           .= "Content-type: text/html; charset=utf-8"."\n";
        $headers           .= "To: <$destinatario>"."\n";
        $headers           .= "From: Teste de envio"."\n";
        $headers           .= "Reply-To: <no-reply@$dominio>"."\n";
        $headers           .= "Return-Path: <$remetente>"."\n";
        $envio              = mail($destinatario, $assunto, $corpo, $headers);
    }

Note: No error is returned.

  • Where you are testing this upload, localhost or on the server?

  • I’m testing on the server, Kinghost

  • 2

    Usually shared host does not allow bulk uploading or long script runs. Some even allow bulk email sending but with limitations (200, 300 per day), for example. Consult your provider and check the signed plan conditions

  • 1

    smtp ta configured for?

  • @Rafaelferreira Yes, SMTP is configuring correctly

  • Thanks @Danielomine Very important information. I will check, maybe this is it.

  • 1

    the error may be the missing parameter -f also: mail( $destinatario, $assunto, $corpo, $headers, '-f'.$remetente ); in systems based on sendmail.

Show 2 more comments

1 answer

1


First try to force the errors to show:

<?php

    ini_set('display_errors','On');
    error_reporting(E_ALL);

It may also be that the function mail(), is disabled in the kinghost, for several reasons, one of them is spam.

I advise to use Phpmailer, for sending via SMTP, it is safer and your email will not run the risk of falling into spam also.

  • 4

    It is erroneous to say that the email will not fall into the SPAM box, since, it depends on many factors involving the configuration of the machine from which the email is being sent, the history of the IP emails, the content of the email, its title, reverse IP and many other things.

  • Thank you @Ívini, but I use the mail() on the kinghost for other applications without problems. I’ll do a search on Phpmailer

Browser other questions tagged

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