2012-03-19 90 views
1

这是我的代码,我将mime.php,mail.phpand Mail.php等所有文件都包含在我的项目文件夹中,但邮件功能仍然不起作用。如何使用ubuntu操作系统在PHP中发送邮件?

<?php 
require "Mail.php"; 
require_once 'Mail/mime.php'; 
require_once 'Mail/mail.php'; 

$to = "[email protected]"; 
$from = "[email protected]"; 
$subject = utf8_decode("mail"); 
$message = utf8_decode("hi"); 
$attachment_data = utf8_decode("testingmail1-filecontent"); 
$attachment_filename = utf8_decode("testingmail1.php"); 
send_mail_utf8_with_attachment($to, $from, $subject, $message ,$attachment_data, $attachment_filename); 

function send_mail_utf8_with_attachment($to, $from, $subject, $message = "",$attachment_data="", $attachment_filename="") 
{ 
    $params = array(); 
    $params['head_charset'] = 'utf-8'; 
    $params['text_charset'] = 'utf-8'; 
    $params['html_charset'] = 'utf-8'; 
    $params['eol'] = "\n"; 
    $hdrs = array('From' => $from ,'Subject' => $subject); 
    $mime = new Mail_mime($params); 
    $mime->setTXTBody($message); 

    if (strlen($attachment_data) && strlen($attachment_filename)) 
    { 
     $mimeType = "application/octet-stream"; 
     $mime->addAttachment($attachment_data, $mimeType, $attachment_filename, false); 
    } 

    $body = $mime->get(); 
    $hdrs = $mime->headers($hdrs); 
    $mail =& Mail::factory('mail'); 
    return $mail->send($to, $hdrs, $body); 
} 
?> 

我已经实现了所有的安装目录:

sudo apt-get install php-pear 
sudo pear install mail 
sudo pear install Net_SMTP 
sudo pear install Auth_SASL 
sudo pear install mail_mime 

但仍是不能正常工作。

+2

的sendmail @ gmail的@ gmail.com是错误的。 – 2012-03-19 10:26:11

+1

你有什么错误?你有没有在你的php.ini中设置你的邮件发送服务器/细节? – ManseUK 2012-03-19 10:26:22

+0

这在Windows中运行良好,但在Ubuntu中并不这样做。我安装了所有的梨包,但仍然没有成果 – chandan 2012-03-19 13:03:37

回答

1

也许你的php.ini设置发送邮件是不正确的。尝试类似mail('[email protected]', 'My Subject', $message);。如果这是工作,你的邮件设置在php.ini中是正确的。

+0

我也做过,但它仍然无法正常工作。我想在这里可能会出现一些文件配置问题。如何更改php.ini文件中的设置? – chandan 2012-03-20 04:42:00

+1

你可以从shell发送邮件吗? 'mail your @ email.de' + subject + CTRL + D – tonymarschall 2012-03-20 08:48:41

相关问题