2012-11-06 7 views
15

में Smtp कनेक्शन को कैसे बंद करें मैं एक गियरमैन कार्यकर्ता प्रक्रिया से ईमेल भेजने के लिए स्विफ्टमेलर का उपयोग करता हूं। मैं ईमेल भेजने के लिए Swift_SmtpTransport कक्षा का उपयोग कर रहा हूं।स्विफ्टमेलर

समस्या यह है कि यदि यह कार्यकर्ता प्रक्रिया कुछ समय के लिए निष्क्रिय रहता है, तो स्विफ्टमेलर कनेक्शन समय समाप्त हो जाता है। अब जब अगली नौकरी आती है, तो स्विफ्टमेलर ईमेल भेजने में विफल रहता है क्योंकि कनेक्शन का समय समाप्त हो गया है।

आदर्श रूप से, मैं प्रत्येक नौकरी के बाद एसएमटीपी कनेक्शन बंद करना चाहता हूं। मैं कक्षा में एपीआई का पता लगाने में असमर्थ हूं जो यह विशेष रूप से करता है। न तो unset() ऑब्जेक्ट काम करता है क्योंकि यह एक स्थिर वर्ग है।

+2

शायद है: $ transport-> स्टॉप(), $ transport-> शुरू() –

+0

@Dragon Omg Ty इतना! मेरे पास अनंत लूप में पृष्ठभूमि कार्यकर्ता है और यह मेरे लिए हल हो गया है। –

उत्तर

0

मैं Swiftmailer और एडब्ल्यूएस एसईएस का उपयोग कर मैं त्रुटि हो रही थी अनंत लूप में एक कार्यकर्ता चल रहा हूँ: मेरी स्क्रिप्ट के लिए

Expected response code 250 but got code "421", with message "421 Timeout waiting for data from client. 

समाधान:

$love = true; 
while($love) { 
    $message = Message::to($record->to) 
     ->from(array('[email protected]' => $user->name())) 
     ->reply(array($user->email => $user->name())) 
     ->subject($record->subject) 
     ->body($body->value) 
     ->html(true) 
     ->send(); 

    if (! $message->was_sent()) 
     throw new Swift_TransportException($errstr . ': ' . $errno); 
} 
+2

यह उत्तर एक स्पष्टीकरण का उपयोग कर सकता है। क्या आपका मतलब है कि ऐसी त्रुटि 'स्विफ्ट_TransportException' फेंक नहीं रही थी, लेकिन क्या यह स्पष्ट रूप से इस मुद्दे को हल करता है? –

+3

क्या यह कोई उत्तर या प्रश्न है? – caponica

1

मैं के साथ एक ही अपवाद मिला स्विफ्टमेलर और एडब्ल्यूएस एसईएस का उपयोग करके बहुत सारे ईमेल भेजते समय symfony2 कमांड लाइन।

मैं हर बार परिवहन परत को शुरू और बंद करके अपनी समस्या को ठीक कर सकता हूं। अधिक जानकारी के लिए मेरे ब्लॉग पोस्ट को देखें: http://www.prowebdev.us/2013/06/swiftmailersymfony2-expected-response.html

10

एक कठोर विकल्प है: परिवहन को स्पष्ट रूप से रोकें। विधि मेल के बाद की कॉल पर, स्विफ्टमेलर जांच करेगा कि परिवहन ऊपर है (अब नहीं है) और इसे फिर से शुरू करें। IMNSHO, SwiftMailer एसएमटीपी टाइमआउट रोकना और automatically.But फिर से कनेक्ट करना चाहिए, अभी यह समाधान नहीं है:

function sendMail($your_args) { 
    try{ 
     $mailer = Swift_Mailer::newInstance($transport); 
     $message = Swift_Message::newInstance('Wonderful Subject') 
     ->setFrom(array('[email protected]' => 'John Doe')) 
     ->setTo(array('[email protected]', '[email protected]' => 'A name')) 
     ->setBody('Here is the message itself'); 

     $result = $mailer->send($message); 
     $mailer->getTransport()->stop(); 

    } catch (Swift_TransportException $e) { 
     //this should be caught to understand if the issue is on transport 
    } catch (Exception $e) { 
     //something else happened 
    } 

} 
6

मैं एक पाश में मेल भेजने और मैं Swift_TransportException पकड़ने गया था और Swift_Mailer का एक नया उदाहरण बनाने लेकिन यह सही फिक्स नहीं था: समस्या परिवहन है, मेलर नहीं है। समाधान Swift_SmtpTransport::stop() को सुस्पष्ट रूप से कॉल जारी करने के लिए है:

foreach($recipients as $to => $body){ 
    try{ 
     $message->setTo($to); 
     $message->setBody(body); 
     $mailer->send($message); 
    }catch(Swift_TransportException $e){ 
     $mailer->getTransport()->stop(); 
     sleep(10); // Just in case ;-) 
    } 
} 

इस तरह, स्विफ्ट का पता लगाता है मेलर बंद कर दिया जाता है और यह स्वचालित रूप से शुरू होता है, इसलिए यह सही संचार त्रुटियों से ठीक।

+1

लेकिन क्या आपको पुनः भेजने की कोशिश नहीं करनी चाहिए? – tishma

+0

@tishma प्रश्न पूछता है कि मेलर को सही तरीके से कैसे रीसेट करना है और यही वह है जिसे मैं जवाब देने का प्रयास करता हूं। –

+0

जब पाइप टूटा हुआ है '$ मेलर-> getTransport() -> रोकें() ' – Jekis

1

जब पाइप टूटा हुआ है तो मेलर-> getTransport() -> stop() भी असफल हो जाएगा। और इस त्रुटि के कारण परिवहन को रोका नहीं जा सकता है। वैकल्पिक हल

// Let's try to send an email. 
$tries = 3; 
while ($tries--) { 
    try { 
     $sent = $this->mailer->send($message); 
     break; 
    } catch (\Exception $e) { 
     // Connection problems 
     // @see https://github.com/swiftmailer/swiftmailer/issues/490 
     try { 
      // Try to stop 
      $this->mailer->getTransport()->stop(); 
     } catch (\Exception $e) { 
      // Got Exception while stopping transport. 
      // We have to set _started to 'false' manually, because due to an exception it is 'true' now. 
      $t = $this->mailer->getTransport(); 
      $reflection = new \ReflectionClass($t); 
      $prop = $reflection->getProperty('_started'); 
      $prop->setAccessible(true); 
      $prop->setValue($t, false); 
      $prop->setAccessible(false); 
     } 
    } 
} 
संबंधित मुद्दे