1

I have a few questions regarding sending email in PHP. I've been on google for the last few days and I'm still having trouble getting this to fully work.

My first question is how do I change the "From" section of my email? I have "To: support@mydomain.com" in my "from" section:

I'd like to have just the proper name of my domain (eg: "testingstuff.com" -> "Testing Stuff"). How could I achieve this?

Once I actually open the email everything in it is fine and correct, including the From email address being "support@mydomain.com".

Also my mail won't send to gmail addresses. It shows up in my mail queue and my logs say it is sent but it never is received on my gmail. Do I have to take extra steps for Google to accept my email? If so what are those? Do other major mail provides require the same steps, or are they different steps?

This is my code so far:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set("sendmail_from", "support@mydomain.com"); 
class email {
    public static function send($to, $subject, $message) {
        $headers = "From: Testing Stuff <support@mydomaincom>\r\n";
        $headers .= "Reply-To: support@mydomain.com\r\n";
        $headers .= "Content-type: text/html\r\n";
        mail($to, $subject, $message, $headers);
    }
}
?>

Usage:

require_once("../mail.php");
email::send("support@mydomaincom", "testing email subject", "testing email body");

Am I doing anything wrong in my code?

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • have you checked the spamfolder in the gmail inbox? Also `$headers .= 'From: My Domain ' . "\r\n";` should work – Evochrome Jan 20 '17 at 21:48
  • Possible duplicate of [PHP mail() form sending to GMAIL spam](http://stackoverflow.com/questions/12188334/php-mail-form-sending-to-gmail-spam) http://stackoverflow.com/questions/712392/send-email-using-the-gmail-smtp-server-from-a-php-page – zod Jan 20 '17 at 21:49
  • 4
    I would recommend using an existing email library, like PHPMailer, Swift Mailer or similar, and send the email through your email providers SMTP-server. That would probably solve both issues out of the box. – M. Eriksson Jan 20 '17 at 21:51

1 Answers1

0

You need to check if the email is sent properly checking the mail() result, in this way:

$result = mail($to, $subject, $message, $headers);
if(!$result) {   
    echo "Error";   
} else {
    echo "Success";
}

this is inside your static function, Also check your spam folder if the mail function return "success".

Marcello Perri
  • 572
  • 7
  • 22
  • This won't say anything about if the email got sent or not, just if it successfully was placed in the systems email queue. It can still fail when the system actually is trying to send it. – M. Eriksson Jan 20 '17 at 22:16