PHP mail() function not sending email

前端 未结 3 1867
忘了有多久
忘了有多久 2021-01-07 10:11

I am attempting to send an email using the mail() PHP function. I had it working until I attempted to give it a subject of \"User registration\", t

相关标签:
3条回答
  • 2021-01-07 10:24

    I'm using this code on most of my projects:

    $subject = 'subject';
    $message = 'message';
    $to = 'user@gmail.com';
    $type = 'plain'; // or HTML
    $charset = 'utf-8';
    
    $mail     = 'no-reply@'.str_replace('www.', '', $_SERVER['SERVER_NAME']);
    $uniqid   = md5(uniqid(time()));
    $headers  = 'From: '.$mail."\n";
    $headers .= 'Reply-to: '.$mail."\n";
    $headers .= 'Return-Path: '.$mail."\n";
    $headers .= 'Message-ID: <'.$uniqid.'@'.$_SERVER['SERVER_NAME'].">\n";
    $headers .= 'MIME-Version: 1.0'."\n";
    $headers .= 'Date: '.gmdate('D, d M Y H:i:s', time())."\n";
    $headers .= 'X-Priority: 3'."\n";
    $headers .= 'X-MSMail-Priority: Normal'."\n";
    $headers .= 'Content-Type: multipart/mixed;boundary="----------'.$uniqid.'"'."\n";
    $headers .= '------------'.$uniqid."\n";
    $headers .= 'Content-type: text/'.$type.';charset='.$charset.''."\n";
    $headers .= 'Content-transfer-encoding: 7bit';
    
    mail($to, $subject, $message, $headers);
    
    0 讨论(0)
  • 2021-01-07 10:25

    I would suggest to use PHP_EOL instead of \r\n or \n as the line break would then be determined by your environment...

    $headers  = 'MIME-Version: 1.0' . PHP_EOL;
    

    etc.. hoping this might finally solve your problem!

    0 讨论(0)
  • 2021-01-07 10:32

    On your 4th line you're using ' that handles everything inside of it as a string so change

    $headers .= 'Content-type: text/html; chareset=iso-8859-1\r\n';
    

    To:

    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    

    and as mentioned in the comments change chareset to charset

    Edit:

    if your sending a txt/html mail you have according to documentation to set mime in the headers too so try this

        $to = $this->post_data['register-email'];
        $message = 'Hello etc';
    
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
        $headers .= 'From: Website <admin@example.com>' . "\r\n";
        mail($to, 'User Registration', $message, $headers);
    

    If it still doesn't work you could try to debugg your code, simply add

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

    on top of the page, and take it from there, and if you still can't solve it by yourself post it here and I'll do my best to help ya out.

    0 讨论(0)
提交回复
热议问题