2010-08-11 74 views
13

我試圖發送一個電子郵件與ZendMail(這個簡單的腳本概括起來)使用Zend Mail發送郵件時出現問題?

<?php 
require_once 'Zend/Mail.php'; 

$mail = new Zend_Mail(); 
$mail->setBodyText('My Nice Test Text'); 
$mail->setBodyHtml('My Nice Test Text'); 
$mail->setFrom('[email protected]', 'Mr Example'); 
$mail->addTo('[email protected]', 'Mr Test'); 
$mail->setSubject('TestSubject'); 
$mail->send(); 
?> 

但是我得到這個堆棧跟蹤:

Fatal error: Uncaught exception 'Zend_Mail_Transport_Exception' with message 'Unable to send mail. ' in /usr/share/php/libzend-framework-php/Zend/Mail/Transport/Sendmail.php:137 Stack trace: #0 /usr/share/php/libzend-framework-php/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Sendmail->_sendMail() #1 /usr/share/php/libzend-framework-php/Zend/Mail.php(1178): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail)) #2 /var/www/hexreaction/mail/index2.php(11): Zend_Mail->send() #3 {main} thrown in /usr/share/php/libzend-framework-php/Zend/Mail/Transport/Sendmail.php on line 137 

編輯:

我我並沒有試圖使用SMTP發送電子郵件,而且我遇到了一個不太可怕的問題,但仍然是一個問題。

<?php 
require_once 'Zend/Mail.php'; 
$config = array('auth' => 'login', 
       'username' => '[email protected]', 
       'password' => 'secretpass'); 

$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config); 

$mail = new Zend_Mail(); 
$mail->setBodyText('This is the text of the mail.'); 
$mail->setFrom('[email protected]', 'Some Sender'); 
$mail->addTo('[email protected]', 'Some Recipient'); 
$mail->setSubject('TestSubject'); 
$mail->send($transport); 
?> 

該拋的這個錯誤,我真的不知道爲什麼:

Fatal error: Class 'Zend_Mail_Transport_Smtp' not found in /var/www/hexreaction/mail/index3.php on line 7 

編輯2:

這是我最後的工作代碼

require_once('Zend/Mail/Transport/Smtp.php'); 
require_once 'Zend/Mail.php'; 
$config = array('auth' => 'login', 
       'username' => '[email protected]', 
       'password' => 'somepass', 
       'ssl' => 'tls'); 

$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config); 

$mail = new Zend_Mail(); 
$mail->setBodyText('This is the text of the mail.'); 
$mail->setFrom('[email protected]', 'Some Sender'); 
$mail->addTo('[email protected]', 'Some Recipient'); 
$mail->setSubject('TestSubject'); 
$mail->send($transport); 
+0

更新Goles答案需要添加**「SSL」 =>「TLS」,**在頂部,以避免錯誤 看到我的回答 – 2016-07-07 16:30:11

+0

這是不可思議的一看;感謝更新;我將在未來爲我的問題做這件事。 – Ahdee 2017-09-24 14:52:45

回答

15

,你可以請參閱堆棧跟蹤Zend_Mail使用Zend_Mail_Transport_Sendmail作爲傳輸適配器。
因此,請確保系統上正在運行與sendmail兼容的MTA(例如Postfix)。

作爲替代,你可以使用Zend_Mail_Transport_Smtp傳輸適配器和使用外部SMTP,服務器,像這樣

$tr = new Zend_Mail_Transport_Smtp('mail.example.com', array(
    'auth'  => 'login', 
    'username' => $username, 
    'password' => $password, 
    'port'  => $port, 
)); 
Zend_Mail::setDefaultTransport($tr); 

編輯: 爲了您的第二個問題:一

require_once('Zend/Mail/Transport/Smtp.php');

應該幫助。

+0

我正在嘗試使用SMTP,(編輯我的文章),我得到一個不太可怕的錯誤,但仍然是一個錯誤。 – Goles 2010-08-11 15:10:28

+0

Thanks !,也GMAIL期待SSL,所以我添加了我的最終工作代碼:-) – Goles 2010-08-11 17:49:44

2

上的Zend_Mail另一個偉大的事情是這是可鏈接的,所以你可以這樣做:

$mail = new Zend_Mail(); 
$mail->setBodyText('My Nice Test Text') 
    ->setBodyHtml('My Nice Test Text') 
    ->setFrom('[email protected]', 'Mr Example') 
    ->addTo('[email protected]', 'Mr Test') 
    ->setSubject('TestSubject') 
    ->send(); 

不知道是否「可鏈接」是正確的話,但我希望你得到了點。這只是一個免費提示。給出的答案是(右)由本傑明

+1

請參閱MethodChaining/Fluent界面以獲取更多信息 http://martinfowler.com/dslwip/MethodChaining.html – 2010-08-11 09:57:28

0

更新Goles答案需要添加「SSL」 =>「TLS」,在頂部以避免錯誤

require_once('Zend/Mail/Transport/Smtp.php'); 
require_once 'Zend/Mail.php'; 
$config = array(
       'ssl' => 'tls', 
       'auth' => 'login', 
       'username' => '[email protected]', 
       'password' => 'somepass' 
       ); 

$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config); 

$mail = new Zend_Mail(); 
$mail->setBodyText('This is the text of the mail.'); 
$mail->setFrom('[email protected]', 'Some Sender'); 
$mail->addTo('[email protected]', 'Some Recipient'); 
$mail->setSubject('TestSubject'); 
$mail->send($transport); 
0

此外,如果你想seand郵件Magento的帶有附件對下面的代碼片段

$config = array(
       'ssl' => 'tls', 
       'auth' => 'login', 
       'username' => '[email protected]', 
       'password' => 'yourPassword' 
       ); 

     $transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config); 


     $bodytext = "Please see attachment for customers detail."; 
     $mail = new Zend_Mail(); 
     $mail->setFrom('[email protected]','Hassan'); 
     $mail->addTo('[email protected]'); 
     $mail->setSubject('Customers info'); 
     $mail->setBodyText($bodytext); 

     $file = $mail->createAttachment(file_get_contents($path.$fileName)); 
     $file ->type  = 'text/csv'; 
     $file ->disposition = Zend_Mime::DISPOSITION_INLINE; 
     $file ->encoding = Zend_Mime::ENCODING_BASE64; 
     $file ->filename = $fileName; 

     if(!$mail->send($transport)) { 
      echo 'Message could not be sent.'; 
      echo 'Mailer Error: ' . $mail->ErrorInfo; 
     } else { 
      echo 'Message has been sent'; 
     } 
     echo "File Completed";exit; 
    }