PHP中的邮件函数不发送标头


mail function in php not sending headers

我带来了一个共享服务器,并获得了一点存储空间。当我运行我的网站时,邮件功能工作得很好。带有主题和信息的邮件已发送给所有收件人。但是发送的邮件提到了"发件人"。我所包含的头不工作。我想发送我的头例如:表单:mymail@mysite.com。我该怎么做呢?

<?php
$output="";
if(isset($_POST['submit']) && !empty($_POST['message']))
{
    $sub=$_POST['subject'];
    $msg=$_POST['message'];
    $sql = "select email from login where status='client'";
    $query = mysqli_query($con,$sql);
    $to=array();
    while($row = mysqli_fetch_array($query))
    {
        $to[] = $row['email'];
        $parts = implode(',',$to);
        $headers = 'From: mymail@mysiet.com;';
        $headers .= 'MIME-Version: 1.0' . "'r'n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
        $subject = $sub;
        $content = $msg;    
    }
    if(mail($parts,$subject,$content,$headers))
    {
        $output="mail sent";
    }
    else
    {
        $output="Error in connection, Mail could not be sent. Please try again";
    }
}
?>

你需要手动指定头,这是我使用的函数,它提供给几乎每个提供者漂亮。你想要的标题是From:Reply-To:,我也建议X-Mailer遵循这里所看到的,我几年前通过SO也发现了它。

<?php
    $to      = "user@anotherdomain.com";                                
    $headers = 'From: My Name <email@domain.com.au>' . "'r'n" .
               'Reply-To: My Name <email2@domain.com.au>' . "'r'n" .
               'X-Mailer: PHP/' . phpversion() . "'r'n" .
               'Content-type: text/html; charset=iso-8859-1';
    $subject = "My subject line here";
    $message = 'Message HTML and Content Here.';
    if(mail($to, $subject, $message, $headers)){
        // Success Here
    }else{
        // Failed Here
    }
?>

我不能100%肯定它会为你工作,因为我在一个专用的机器上,已经说过-我确实在多个域中使用它,并且从未与我的Apache或php配置有关的邮件设置搞乱。

阅读更多PHP邮件函数:http://php.net/manual/en/function.mail.php

你可以试试下面的代码,只是从php.net中摘取的

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "'r'n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "'r'n";
$headers .= 'Cc: birthdayarchive@example.com' . "'r'n";
$headers .= 'Bcc: birthdaycheck@example.com' . "'r'n";

离题了,但是PHPMailer很好用。