2010-08-11 10 views
13

Sto cercando di inviare una e-mail con ZendMail (questo semplice script riassume)Problemi con l'invio di posta con 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(); 
?> 

Tuttavia ottengo questo stack trace:

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 

EDIT:

I Non sto cercando di usare SMTP per inviare la mia e-mail e sto avendo un problema meno orribile, ma ancora un problema.

<?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); 
?> 

di questo lancio questo errore, non ho davvero capisco perché:

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

EDIT 2:

Questo è il mio codice di lavoro finale

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

aggiornato risposta Goles bisogno di aggiungere ** 'ssl' => 'TLS', ** in alto per evitare errori vedere la mia risposta –

+0

questo è incredibile; grazie per l'aggiornamento; Lo farò anche per le mie domande in futuro. – Ahdee

risposta

15

come si può vedere nella traccia Stack Zend_Mail utilizza Zend_Mail_Transport_Sendmail come adattatore di trasporto.
Quindi assicurati che un MTA compatibile con sendmail (ad esempio Postfix) sia in esecuzione sul tuo sistema.

In alternativa è possibile utilizzare l'adattatore di trasporto Zend_Mail_Transport_Smtp e utilizzare un server SMTP esterno in questo modo

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

Edit: Per il vostro secondo problema: un

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

dovrebbe aiutare .

+0

Sto provando a usare anche SMTP, (modificato il mio post), e sto ricevendo un errore meno orribile, ma ancora un errore. – Goles

+0

Grazie! Anche GMAIL si aspetta SSL, quindi ho aggiunto il mio codice di lavoro finale :-) – Goles

2

Un'altra grande cosa su Zend_Mail è che è chainable, in modo da poter fare questo:

$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(); 

non so per certo se 'chainable' è la parola giusta, ma spero che hai il punto. Questo è solo un consiglio gratuito. La risposta è data (a destra) di Benjamin

+1

Vedere MethodChaining/Fluent Interface per ulteriori informazioni http://martinfowler.com/dslwip/MethodChaining.html –

0

Aggiornato risposta Goles bisogno di aggiungere 'ssl' => 'TLS', in alto per evitare errori

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

Inoltre, se si desidera seand la posta in Magento con attacco a dare un'occhiata al seguente frammento

$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; 
    }