PHP mail() function not sending email

前端 未结 3 1871
忘了有多久
忘了有多久 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);
    

提交回复
热议问题