为什么这个PHP代码不发送电子邮件?

时间:2011-08-02 04:55:19

标签: php

我在命令行上的unix共享主机帐户上执行它,但它不发送任何电子邮件。这有什么问题?我从PHP: How to send email with attachment using smtp settings?获得了代码,但它仍无效。

<?php
include('Mail.php');
include('Mail/mime.php');


// include_once('Mail/mime.php');|

// The code below composes and sends the email|

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './a.php';
$crlf = "\r\n";
$hdrs = array("From"=>'contactus@site.com', "Subject"=>"hello" ,"Reply-To"=>"contactus@site.com");

$mime = new Mail_mime($crlf);

$mime->setTXTBody($text);
$mime->setHTMLBody($html);

$mime->addAttachment($file,'application/octet-stream');

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail', $params);
$mail->send('rag.7raggupta@gmail.com', $hdrs, $body);

2 个答案:

答案 0 :(得分:4)

你试过mail($to, $subj, $body)吗?它可能是您的服务器设置的问题,而不一定是Pear包或PHP本身。

答案 1 :(得分:0)

首先,您是否安装了Pear和Mime_Mail软件包?如果不这样做,那么这将不起作用,因为它是Pear特定代码。

接下来,假设安装了pear,我将发布上面的代码版本,我认为它将按原样运行。

<?php

include('Mail.php');
include('Mail/mime.php');

$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = './a.php';
$crlf = "\n";
$hdrs = array(
              'From'    => 'contactus@site.com',
              'Subject' => 'Hello',
              'Reply-To' => 'contactus@site.com'
              );

$mime = new Mail_mime(array('eol' => $crlf));

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file, 'application/x-httpd-php');

// do not ever try to call these lines in reverse order
// when using versions older than 1.6.0
$body = $mime->get();
// or in 1.6.0 and newer
// $body = $mime->getMessageBody();

$hdrs = $mime->txtHeaders($hdrs);

$mail =& Mail::factory('mail');
$mail->send('rag.7raggupta@gmail.com', $hdrs, $body);

?>

我不确定你想把php附件作为八位字节流发送,因为我不认为这是一个适用于php脚本的标识。我修改它是php的正确mime类型。

如需进一步参考,请查看this link to the Mail_Mime manual

相关问题