MAMP 上的 php 邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1226299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php mail on MAMP
提问by Ori
I need to test some script using PHP's mail. I'd like to be able to finally get this working locally. I am using MAMP. Is there a way to do this without installing any third party software?
我需要使用 PHP 的邮件测试一些脚本。我希望能够最终在本地工作。我正在使用 MAMP。有没有办法在不安装任何第三方软件的情况下做到这一点?
I've done some searching on this but haven't found anything appealing.
我已经对此进行了一些搜索,但没有发现任何吸引人的地方。
Thanks
谢谢
回答by symcbean
Are you specifically trying to test the sending of mail, or are you testing the rest of the code?
您是专门尝试测试邮件的发送,还是测试其余的代码?
In the case of the former, you need to configure:
对于前者,您需要配置:
SMTP = smtp.example.com
smtp_port = 25
sendmail_from = [email protected]
in your php.ini file (check where it is with phpinfo()), substituting in appropriate values.
在您的 php.ini 文件中(使用 phpinfo() 检查它的位置),替换为适当的值。
To test the code other than the process of sending mail, then I'd recommend creating 2 include files:
要测试发送邮件过程以外的代码,那么我建议创建 2 个包含文件:
<?php
// for live usage/mail send testing
function ori_mail()
{
return call_user_func_array('mail',func_get_args());
}
and for testing other code
和测试其他代码
function ori_mail()
{
file_put_contents('debug_mail_scripts.txt'
,date('r') . ':' . var_export(func_get_args(), true)
, FILE_APPEND);
}
And include the relevant one to your testing.
并将相关的一项纳入您的测试。
Note that testing integration with the SMTP server, and testing deliverbility of your code is rather complex but should be done independently of testing your PHP.
请注意,测试与 SMTP 服务器的集成以及测试代码的可交付性相当复杂,但应该独立于测试 PHP 完成。
C.
C。
回答by Gordon Potter
You might want to consider the Swift Mailer library
您可能需要考虑 Swift Mailer 库
It makes doing email from PHP code much more reliable. You could even point your mailer script to a real SMTP service. This can eliminate a a lot of issues you would run into when moving from local to to production environments.
它使从 PHP 代码发送电子邮件更加可靠。您甚至可以将邮件程序脚本指向真正的 SMTP 服务。这可以消除您在从本地环境转移到生产环境时会遇到的许多问题。
Using swift mailer is as simple as using a single include at the top of your PHP script and writing a code block to send a simple message. And it is fully object oriented.
使用 swift mailer 就像在 PHP 脚本顶部使用单个包含并编写代码块来发送简单消息一样简单。而且它是完全面向对象的。
回答by Andrea Fiore
Few months ago I had a similar problem whilst developing on my local machine an application which involved sending automating email notifications. I have lost quite some time installing Sendmail on OSX and eventually I could not get it working right..
几个月前,我在本地机器上开发一个涉及发送自动化电子邮件通知的应用程序时遇到了类似的问题。我在 OSX 上安装 Sendmail 已经浪费了很多时间,最终我无法让它正常工作..
My approach was to use the PEAR Mailas a temporary replacement for php's native mail function. Basically you can define a function called send-mail(see code below) and, once you deploy your app on a server, you can possibly replace the calls to that function with calls to mail().
我的方法是使用 PEAR Mail作为 php 本地邮件功能的临时替代品。基本上,您可以定义一个名为send-mail的函数(请参阅下面的代码),并且一旦您在服务器上部署您的应用程序,您就可以将对该函数的调用替换为对 mail() 的调用。
<?php
require_once 'Mail.php';
function send_mail($recipient,$subject,$body){
$host = "yourmailserver.net";
$username = "[email protected]";
$password = "password";
$port = 25;
$headers = array ('From' => "Your agent <[email protected]>",
'To' => $recipient,
'Subject' => $subject
);
$smtp = Mail::factory(
'smtp',
array ('host' => $host,
'auth' => true,
'port' => $port,
'username' => $username,
'password' => $password)
);
$smtp->send($recipient, $headers, $body);
}
?>
回答by pixeline
what i do is i use the phpmailer class(warning: horrible website !) and specify a real smtp server on which i have an account. So i don't use mail() but use smtp. In this way, it does not matter whether i'm on my local server or on the real server. But you do need a working smtp access to that smtp mail server. Best would be to actually use the production mail server (the one that will be used by your application when it goes live). In this manner, you won't have last minute surprises when you discover that the mailserver messes up the reply-to field and little things like that.
我所做的是使用phpmailer 类(警告:可怕的网站!)并指定一个真正的 smtp 服务器,我在其上有一个帐户。所以我不使用mail(),而是使用smtp。这样,我是在本地服务器上还是在真实服务器上都没有关系。但是您确实需要对该 smtp 邮件服务器进行有效的 smtp 访问。最好是实际使用生产邮件服务器(您的应用程序上线时将使用的服务器)。以这种方式,当您发现邮件服务器弄乱了回复字段和诸如此类的小事时,您将不会在最后一刻感到惊讶。
回答by Julien
You could use your gmail account and send your test emails via gmail's SMTP server.
您可以使用您的 gmail 帐户并通过 gmail 的 SMTP 服务器发送您的测试电子邮件。
You can use the phpmailer class (http://phpmailer.worxware.com/) to do this. There is a basic gmail example in the examples/ folder when you download this class.
您可以使用 phpmailer 类 ( http://phpmailer.worxware.com/) 来执行此操作。下载此类时,examples/ 文件夹中有一个基本的 gmail 示例。
回答by Jane
I think the best solution is write all messages to file. So you just need to make own sendmail.
我认为最好的解决方案是将所有消息写入文件。所以你只需要制作自己的sendmail。
add to httpd.conf file this strings:
php_admin_value sendmail_path
"/Applications/MAMP/somefolder/mysendmail.sh"In the file mysendmail.sh add following:
#!/bin/bash
while read line do echo "$line" >> ../mail_log.txt done
echo "------------- next mail ----------------" >> ../mail_log.txt
exit 0
Do not forget to set privilegies: chmod 755 mysendmail.sh
将此字符串添加到 httpd.conf 文件中:
php_admin_value sendmail_path
"/Applications/MAMP/somefolder/mysendmail.sh"在文件 mysendmail.sh 中添加以下内容:
#!/bin/bash
而读行做 echo "$line" >> ../mail_log.txt done
回声“------------下一封邮件----”>> ../mail_log.txt
退出 0
不要忘记设置权限:chmod 755 mysendmail.sh

