2012-03-19 95 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

相關問題