简体   繁体   中英

PHP mail function not sending email to Gmail account?

I am working on a WordPress plugin in which I send email using the PHP mail function, but there is a problem with the mail function. It sends emails to my non-Gmail account, but it's doesn't send emails to my Gmail account. I am using following code:

function send_mail()
        {
            global $wpdb;
            $to = 'mymail@gmail.com';
            $subject = 'Hello';
            $name='my name';
            $from="name@mydomain.com";

            $message = "
            <html>
            <head>
            <title>my title</title>
            </head>
            <body>
            <div>
                <tt> ".Hii How Are you."</tt>
            </div>
            </body>
            </html>";

            $header  = "MIME-Version: 1.0\r\n";
            $header .= "Content-type: text/html; charset=iso-8859-1\r\n";
            $header .= "From: ".$name."<".$from.">\r\n";

            mail($to, $subject, $message, $header);
}

Is there something wrong with my code, or is there some issue with the mail function? If any alternate method is available to send email, please give me the link.

Check if adding fifth variable works for you... here is my code for sending emails.

    if( mail( $recipient, $subject, $message, $headers, "-f noreply@mydomain.com"))
        return "success";

Check the spam folder, It might be there. Its a server issue, it hapened to me also many times. Gmail blocks mails or sends to spam from some servers due to some reasons. Ask your server provider to check why mails are not going to gmail inbox.

use the code sample as below, email address in <> and that last paramter worked for me.

$headers = 'From: <test@test.com>' . "\r\n" .
'Reply-To: <test@test.com>';

mail('<myEmail@gmail.com>', 'the subject', 'the message', $headers,
  '-fwebmaster@example.com');
?>

It is the answer provided in PHP mail() function will not send to gmail but will send to my non-gmail account by ARH3, which i have tried and tested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM