我需要使用PHP的邮件功能测试一些脚本,希望最终能在本地进行测试。我正在使用MAMP。有没有不安装任何第三方软件就可以实现这一点的方法?
我已经搜索了一些资料,但还没有找到令人满意的解决方案。
谢谢!
您是想测试邮件发送,还是测试代码的其他部分?
如果是前者,您需要进行以下配置:
SMTP = smtp.example.com
smtp_port = 25
sendmail_from = me@example.com
<?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.
C.
<?php
require_once 'Mail.php';
function send_mail($recipient,$subject,$body){
$host = "yourmailserver.net";
$username = "you@yourmailserver.net";
$password = "password";
$port = 25;
$headers = array ('From' => "Your agent <noreply@yoursite.net>",
'To' => $recipient,
'Subject' => $subject
);
$smtp = Mail::factory(
'smtp',
array ('host' => $host,
'auth' => true,
'port' => $port,
'username' => $username,
'password' => $password)
);
$smtp->send($recipient, $headers, $body);
}
?>
我认为最好的解决方案是将所有消息写入文件。因此,您只需要制作自己的sendmail。
在httpd.conf文件中添加以下字符串:
php_admin_value sendmail_path
"/Applications/MAMP/somefolder/mysendmail.sh"
在mysendmail.sh文件中添加以下内容:
while read line do echo "$line" >> ../mail_log.txt done
echo "------------- 下一封邮件 ----------------" >> ../mail_log.txt
exit 0
不要忘记设置权限:chmod 755 mysendmail.sh
您可以使用您的Gmail帐户,并通过Gmail的SMTP服务器发送测试电子邮件。
您可以使用phpmailer类(http://phpmailer.worxware.com/)来实现此功能。当您下载此类时,在examples/文件夹中有一个基本的Gmail示例。