0

I am using PHP mail function to send a mail with shared hosting of GoDaddy, previously email is getting in the inbox but now I am getting email in the spam box instead of inbox.

Below is my code:

 <?php
   if(isset($_POST['submit']))
   {
   
   $name=$_POST['name'];
   $company=$_POST['company'];
   $email=$_POST['email'];
    
           $to = $email;
           $from = $email;
           $CC = 'myname@gmail.com';
           $subject ="Testing";
           $htmlContent = '
                
   Dear '.$name.' from '.$company.',
   
   \nThank you for your interest in our impact initiatives.
   
   \nPlease reach out to us, we are happy to learn more from you!
   
   \nAll the best,
   Dev Team.
   ';
          
                         
              $headers = "From: ".$name." <".$from.">\r\n";
              $headers .= "Cc: $CC";
            
       
   //            if($extension ==""){ 
                   $headers .= "\nMIME-Version: 1.0" . "\r\n";
               $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
   //            }
              $semi_rand = md5(time());
              
              $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
   //           if(!$extension ==""){ 
   //           $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
   //        
   //        $htmlContent = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n";
   //           
   //        $htmlContent .= "--{$mime_boundary}\n";
   //           }
    
           if(mail($to, $subject, $htmlContent, $headers)) {
               header("Location:index.php");
   //            echo "<p>mail sent to $to and $headers!</p>";
           } else {
               echo "<p>mail could not be sent!</p>";
           }
     
   
   }    
   ?>

I have used all headers but still I am facing same issue. Kindly let me know if anything needs to change and please give me some suggestions to resolve this issue.

Thank you in advance.

  • Clearly, your e-mail provider thinks this message looks like spam. There isn't a magic header you can include that says "this is not spam" - if there was, spammers would include it, and spam filters would be useless. You may be able to look into _why_ the provider (GMail?) classed it as spam, but that's not really a programming question. – IMSoP Nov 25 '21 at 11:04
  • 3
    When you add the address you get from the form as the "from" address, you're basically spoofing that address (you're pretending to be them). Many mail servers do check if the server that sent it has permission to send from that address (which you most likely don't have) by checking things like SPF and DKIM records (DNS settings). You should send the email from _your_ address and then send it using the email providers SMTP. Then I would also recommend you to use a mail library like PHPMailer or SwiftMailer instead of using the low level `mail()` function (easier to use SMTP). – M. Eriksson Nov 25 '21 at 11:06
  • 2
    I’m voting to close this question because there probably isn't a _programming_ problem here, just the common problem of persuading automated filters that your message is not spam. – IMSoP Nov 25 '21 at 11:06
  • https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail – DarkBee Nov 25 '21 at 11:10
  • You can't just enter any value in the `from` anymore. There are a lot of systems in place nowdays to prevent e-mail spoofing (SPF, DKIM, DMARC, ...). You should send the e-mail from an e-mail address associated with the domain you are sending the domain from and use reply to – DarkBee Nov 25 '21 at 11:13

1 Answers1

0

Its is very likely that phpmail function does not fit the anti-spam methords most providers use as the server the mail is being sent from is not recognised as allowed. DKIM, SPF, RIP need to be matched. To find out what gmail does not like about your email would be best to review the headers of the sent message to make sure it fits your domains sending criteria. However if you want to get this resolved quickly using an existing email account or creating a new one for your domain such as website@ and using STMP auth should prevent your emails getting flagged as the sending server should already match the needed criteria. Here is a link to a thread that should help: Sending email with PHP from an SMTP server

BobMorley
  • 36
  • 5