2012-08-16 167 views
0

我正在尝试使用PHP发送电子邮件。我在这里发现了很多帖子,我感到迷茫。我试图使用很多代码,但没有一个能够工作。以PHP发送电子邮件

最后,我读了关于PEAR,并且我有以下代码,我正在使用PHP 5.3.6的MAMP在Mac OSX 10.7.3上工作。

我需要做些什么来使代码工作,它显示一个错误加载页面!?有没有配置?

如果有人有配置说明的工作示例,请分享。

<?php 
require_once "Mail.php"; 

$recipients = '[email protected]'; 

$headers['From'] = '[email protected]'; 
$headers['To']  = '[email protected]'; 
$headers['Subject'] = 'Test message'; 

$body = 'Test message'; 

$params['sendmail_path'] = '/usr/lib/sendmail'; 

// Create the mail object using the Mail::factory method 
$mail_object =& Mail::factory('sendmail', $params); 

$mail_object->send($recipients, $headers, $body); 

if (PEAR::isError($mail)) { 
    echo("<p>" . $mail->getMessage() . "</p>"); 
} else { 
    echo("<p>Message successfully sent!</p>"); 
} 
?> 

回答

4

使用PHP发送邮件简单得不能再简单:

mail('[email protected]', 'Subject', 'Here comes the message'); 

就是这样。请记住,您的服务器需要配置为发送电子邮件。

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

+0

我需要什么样的配置和在那里??? – user1483799 2012-08-16 16:46:16

+0

老实说,我从来没有这样做过(我租用的每台服务器都经过正确配置),但阅读[本指南](http://email.about.com/od/emailprogrammingtips/qt/Configure_PHP_to_Use_a_Local_Mail_Server_for_Sending_Mail.htm )似乎你所需要做的就是在php.ini中设置sendmail_path变量来指向你的sendmail函数。与你现在的代码完全一样(将其设置为/ usr/lib/sendmail)(尽管文章建议使用/ usr/bin/sendmail或/ usr/sbin/sendmail)。 – powerbuoy 2012-08-16 16:52:11