尝试使用PHP脚本发送电子邮件

时间:2011-12-25 21:40:10

标签: php xampp

我正在使用XAMPP运行Web服务器。我使用Web表单使用PHP脚本发送电子邮件。我想知道,有人可以告诉我为什么我会收到这个错误吗?

警告:mail()[function.mail]:“sendmail_from”未在php.ini中设置或自定义“From:”标头在第24行的C:\ xampp \ htdocs \ format.php中丢失

这是我的php.ini代码,提供了相关部分:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = postmaster@localhost

这也是我的PHP脚本,只是为了向您展示我编码的内容(顺便说一句,我看不出第24行有什么问题)。

<?php

$to = 'lnation@gmail.com';
$subject = 'This is from your company';

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$message = <<<EMAIL

Hi! My name is $email

$message

From $name
Oh ya, my email is $email

EMAIL;

$header = '$email';

if($_POST){
mail($to,  $subject, $message, $header);
$feedback = 'Thanks for the email';
}

?>

任何建议都将受到赞赏。

4 个答案:

答案 0 :(得分:3)

取消注释ini文件中的sendmail_from设置:

sendmail_from = postmaster @ localhost

(从头开始删除分号)。您也可以将postmaster @ localhost更改为您实际希望人们将其视为发件人的内容

答案 1 :(得分:2)

查看邮件上的PHP手册:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

您需要FROM标头。 http://php.net/manual/en/function.mail.php

答案 2 :(得分:2)

始终在脚本中设置,不要依赖于php设置......

开始使用swiftmailer,您要后悔...

答案 3 :(得分:1)

你需要有一个smtp服务器来发送来自php邮件功能的电子邮件。尝试使用gmail smtp和脚本发送邮件。