在php中使用mail()发送邮件 [英] send mail using mail() in php

查看:106
本文介绍了在php中使用mail()发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在学习php时才知道我尝试过此代码的mail()函数

Hello I am learning php where i came to know mail() function i have tried this code

function sendMail()
{
    $to = 'Sohil Desai<sohildesaixxxx@gmail.com>';
    $from = 'Sohil Desai<sohildesaixxxx@hotmail.com>';
    $subject = 'Test Mail';

    $headers  = 'MIME-Version: 1.0'. "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: '.$from . "\r\n";
    $headers .= 'X-Mailer: PHP/'.phpversion();

    $message = 'This mail is sent for testing.';

    $mail = mail($to, $subject, $message, $headers);

    if (!$mail) {
        return 'Error occured sending mail.';
    }

    return 'Mail successfully sent.';
}

echo sendmail();

我只测试了gmail,ymail和hotmail。

I have tested only for gmail, ymail and hotmail.

此功能将垃圾邮件发送给gmail&

This function sends mail in a spam for gmail & hotmail and it won't send mail to ymail.

为什么会发生?

我正在使用Ubuntu 12.04& php版本5.3.10。

I am using Ubuntu 12.04 & php version 5.3.10.

有人可以帮助我吗?

推荐答案

尝试将 to添加为标题:

Try adding "to" as a header:

$headers  = 'MIME-Version: 1.0'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: ' . $to . "\r\n";
$headers .= 'From: '.$from . "\r\n";
$headers .= 'X-Mailer: PHP/'.phpversion();

更多详细信息可以在 http://www.php.net/manual/zh-CN/function.mail.php (示例4)。

More details can be found at http://www.php.net/manual/en/function.mail.php (example #4).

这篇关于在php中使用mail()发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆