Sending email via php does not work

Asked

Viewed 388 times

-4

I’m using the same script I’ve used on other sites but it seems this site isn’t sending emails.

If I send an email through virtual control panel min email is received in outlook in spam but gmail does not even arrive.

What should I do?

       <?php


    $subject = "example";
    $from ='[email protected]';
    $to ='<[email protected]>';
    $bcc = null; // Esconder endereços de e-mails.
    $cc = null; // Qualquer destinatário pode ver os endereços de e-mail especificados nos campos To e Cc.
    $message = "<!DOCTYPE html>
    <html>
    <head>
    <style>
    table {
        width:100%;
    }
    table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
    }
    th, td {
        padding: 5px;
        text-align: left;
    }
    table#t01 tr:nth-child(even) {
        background-color: #eee;
    }
    table#t01 tr:nth-child(odd) {
       background-color:#fff;
    }
    table#t01 th    {
        background-color: black;
        color: white;
    }
    </style>
    </head>
    <body>


    <table id='t01'>
      <tr>
        <th>Lisguagens uteis</th>
        <th>Sites</th>
        <th>Prioridade</th> 
      </tr>
      <tr>
        <td>HTML</td>
        <td>http://www.w3schools.com/</td>
        <td>1</td>  
      </tr>
      <tr>
        <td>CSS</td>
        <td>http://www.w3schools.com/</td>
        <td>2</td>  
      </tr>
      <tr>
        <td>JAVASCRIPT</td>
        <td>https://codecademy.com/learn/javascript</td>
        <td>3</td>  
      </tr>
      <tr>
        <td>PHP</td>
        <td>https://external.codecademy.com/learn/php</td>
        <td>4</td>  
      </tr>
    </table>
    <table id='t01'>
      <tr>
      <th>Nota:</th>
      </tr>
      <tr>
      <th>text exemple</th>
      </tr>
    </body>
    </html>";

    $headers = sprintf( 'Date: %s%s', date( "D, d M Y H:i:s O" ), PHP_EOL );
    $headers .= sprintf( 'Return-Path: %s%s', $from, PHP_EOL );
    $headers .= sprintf( 'To: %s%s', $to, PHP_EOL );
    $headers .= sprintf( 'Cc: %s%s', $cc, PHP_EOL );
    $headers .= sprintf( 'Bcc: %s%s', $bcc, PHP_EOL );
    $headers .= sprintf( 'From: %s%s', $from, PHP_EOL );
    $headers .= sprintf( 'Reply-To: %s%s', $from, PHP_EOL );
    $headers .= sprintf( 'Message-ID: <%s@%s>%s', md5( uniqid( rand( ), true ) ), $_SERVER[ 'HTTP_HOST' ], PHP_EOL );
    $headers .= sprintf( 'X-Priority: %d%s', 3, PHP_EOL );
    $headers .= sprintf( 'X-Mailer: PHP/%s%s', phpversion( ), PHP_EOL );
    $headers .= sprintf( 'Disposition-Notification-To: %s%s', $from, PHP_EOL );
    $headers .= sprintf( 'MIME-Version: 1.0%s', PHP_EOL );
    $headers .= sprintf( 'Content-Transfer-Encoding: 8bit%s', PHP_EOL );
    $headers .= sprintf( 'Content-Type: text/html; charset="iso-8859-1"%s', PHP_EOL );

    mail(null, $subject, $message, $headers);







    echo("enviado");
?>
  • Add your script to the question.

  • 1

    The first parameter of the "mail" function is the recipient, you are passing null to it.

  • but I’m going through the headers, either way if I put there the variable that contains the email it keeps sending itself

2 answers

2


Hello, you should pass another parameter, the "sender", in this fifth parameter should be placed some email from your company or domain for example: [email protected] as in the PHP Handbook:

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

NOTE: The PHP manual explains why to use -f

"Currently my hosting service is on Godaddy. When attempting to use the mail Function without the Fifth Parameter containing "-f", my message headers would not work. "

In a free translation:

"My host is Godaddy. when I try to use the mail function without the fifth parameter with -f my emails don’t work"

http://php.net/manual/en/function.mail.php

Without this fifth domain, it will send with a "strange" email that google, for example, considers malicious.

Make sure you are testing outside the localhost.

0

Try:

<?php
$subject = "example"; //Assunto
$from ='[email protected]'; //Email de quem está enviando
$to ='[email protected]'; //Email de quem vai receber

$message = "<!DOCTYPE html>
    <html>
    <head>
    <style>
    table {
        width:100%;
    }
    table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
    }
    th, td {
        padding: 5px;
        text-align: left;
    }
    table#t01 tr:nth-child(even) {
        background-color: #eee;
    }
    table#t01 tr:nth-child(odd) {
       background-color:#fff;
    }
    table#t01 th    {
        background-color: black;
        color: white;
    }
    </style>
    </head>
    <body>


    <table id='t01'>
      <tr>
        <th>Lisguagens uteis</th>
        <th>Sites</th>
        <th>Prioridade</th> 
      </tr>
      <tr>
        <td>HTML</td>
        <td>http://www.w3schools.com/</td>
        <td>1</td>  
      </tr>
      <tr>
        <td>CSS</td>
        <td>http://www.w3schools.com/</td>
        <td>2</td>  
      </tr>
      <tr>
        <td>JAVASCRIPT</td>
        <td>https://codecademy.com/learn/javascript</td>
        <td>3</td>  
      </tr>
      <tr>
        <td>PHP</td>
        <td>https://external.codecademy.com/learn/php</td>
        <td>4</td>  
      </tr>
    </table>
    <table id='t01'>
      <tr>
      <th>Nota:</th>
      </tr>
      <tr>
      <th>text exemple</th>
      </tr>
    </body>
    </html>";

$headers = "MIME-Version: 1.1\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "From: ".$from."\r\n"; // remetente
$headers .= "Return-Path: $from\r\n"; // return-path

mail($to, $subject, $message, $headers, "-r".$from);
?>
  • I tested it now and it didn’t work

Browser other questions tagged

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