【问题标题】:Can't get php mail() to work无法让 php mail() 工作
【发布时间】:2012-05-06 11:30:03
【问题描述】:

我在我的 Mac 上运行了 Mamp,并试图让 mail() 工作。

这是我必须处理的。

$to = 'mymail@gmail.com';
$subject = 'The subject!';
$message = 'Hi there!';


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


// Mail it
if(mail($to, $subject, $message, $headers))
{ print 'success!'; }
else
{ print 'fail!'; }

?>

它只是不断返回错误。知道我做错了什么吗? 我需要检查一些 php/apache 的设置吗?

【问题讨论】:

  • 它甚至没有进入邮件功能?还是返回“失败”?
  • 你必须设置 SMTP 服务器
  • ... 或者只使用 SwiftMailer 或类似的,这样您就可以利用您可能需要的功能,例如 HTML 电子邮件,而无需手动破解 MIME 标头的麻烦。然后,与外部 SMTP 服务器的连接也很容易获得,
  • 当然 php mail() 函数需要在服务器上才能工作。在本地主机上,您总是失败!此外还有在本地主机上发送邮件的设置,但我认为现在你正在寻找你的代码可以工作!
  • 如果您得到“真”,则意味着您的邮件功能仅在接收权问题上起作用,请检查您的终端是否没有电子邮件过滤器,并发现此链接有用:formtoemail.com/support/not_receiving_email.php

标签: php apache email mamp


【解决方案1】:

如果你在 localhost 上使用你的 sn-p,放在服务器上然后尝试。 php mail() 函数需要在服务器上才能工作。在本地主机上,您总是失败!

【讨论】:

  • 除非你安装了 PHP 客户端
【解决方案2】:

试试这个:

<?php
$Name = "Da Duder"; //senders name
$email = "email@adress.com"; //senders e-mail adress
$recipient = "PersonWhoGetsIt@emailadress.com"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields

ini_set('sendmail_from', 'me@domain.com');

mail($recipient, $subject, $mail_body, $header);
?>

http://be.php.net/manual/en/function.mail.php

每行文字不得超过 70 个字符,需要用 LF (\n) 截断

编辑:正如@brad 建议的那样:SwiftMailer 真的很棒!

【讨论】:

  • 我现在正在研究 Swift。
猜你喜欢
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多