使用PHP发送电子邮件

时间:2014-04-23 09:09:07

标签: php email

我有联系表单,我想发送电子邮件到emailField的电子邮件。

$subject = $_POST['demail2'];
$message = $_POST['textarea'];
$mail_from = $_POST['demail'];
$name = $_POST['dname'];
$header = "from: $name";
$to = "some-email@some-provider.com";

$send_contact = mail($to, $subject, $message, $header);

if ($send_contact)
{
    echo "We´ve recieved your information";
}
else
{
    echo "Error";
}

我试试这个但不行......

3 个答案:

答案 0 :(得分:0)

试试这个,

$subject = $_POST['demail2'];
$message = $_POST['textarea'];
$mail_from = $_POST['demail'];
$name = $_POST['dname'];

$to = "some-email@some-provider.com";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

$headers .= 'From: '.$name.' <'.$mail_from.'>' . "\r\n";    
$send_contact = mail($to, $subject, $message, $headers);

答案 1 :(得分:0)

1 - 打开您的php.ini
2 - 查找并取消注释sendmail_path = "sendmail -t -i"
3 - 重新启动apache

注意:
您应该放置 FULL PATH (即/usr/sbin/sendmail -t -i
sendmail_path = "/usr/sbin/sendmail -t -i"

如果仍然失败,您应该启用并显示错误以便正确调试,为此,在脚本顶部添加以下代码:

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

答案 2 :(得分:0)

当mail()返回false时,您可以使用error_get_last()。

使用print_r(error_get_last())可以得到正确的错误