title: python smtp receipt confirmation DSN
body: <p>I am dealing with sporadic non-delivery of emails sent using python2.7.6 from a linux cluster to internal email recipients whose addresses are known to be valid. Since the originating code used the deprecated syntax of <code>cat &lt;file&gt; | mailx -s &lt;subject&gt; &lt;recipient&gt;</code> to generate a cmd for submission as a <code>subprocess(cmd, shell=True)</code>, I have some working amended code-in-progress using smtp as below.</p>
  
  <pre><code>import smtplib
  from email.mime.text import MIMEText
  def emailer(from_addr, to_addr, subj, email_body):
      if not os.path.isfile(email_body):
          msg = MIMEText(email_body)  
      else:
          with open(email_body, 'rb') as FHrb:
              msg = MIMEText(FHrb.read())
      msg['Subject']=subj
      s = smtplib.SMTP('localhost'); s.set_debuglevel(True)
      try:
          s.sendmail(from_addr,
                 ','.join(to_addr) if isinstance(to_addr, list) else to_addr.split(','),
                 msg.as_string())
      except Exception as e:
          print e
  
      s.quit()
  </code></pre>
  
  <p>When called this code returns to console the verbosity below (specific details masked by  to protect the innocent &amp; reduce post size)</p>
  
  <pre><code>send: 'ehlo &lt;localhost&gt;.&lt;localdomain&gt;\r\n'
  reply: '250-&lt;localhost&gt;.localdomain\r\n'
  reply: '250-PIPELINING\r\n'
  reply: '250-SIZE 10240000\r\n'
  reply: '250-VRFY\r\n'
  reply: '250-ETRN\r\n'
  reply: '250-ENHANCEDSTATUSCODES\r\n'
  reply: '250-8BITMIME\r\n'
  reply: '250 DSN\r\n'
  reply: retcode (250); Msg: &lt;localhost&gt;.localdomain
  PIPELINING
  SIZE 10240000
  VRFY
  ETRN
  ENHANCEDSTATUSCODES
  8BITMIME
  DSN 
  send: 'mail FROM:&lt;SENDER&gt; size=4178\r\n'
  reply: '250 2.1.0 Ok\r\n'
  reply: retcode (250); Msg: 2.1.0 Ok
  send: 'rcpt TO:&lt;me@my_email_address&gt;\r\n'
  reply: '250 2.1.5 Ok\r\n'
  reply: retcode (250); Msg: 2.1.5 Ok
  send: 'data\r\n'
  reply: '354 End data with &lt;CR&gt;&lt;LF&gt;.&lt;CR&gt;&lt;LF&gt;\r\n'
  reply: retcode (354); Msg: End data with &lt;CR&gt;&lt;LF&gt;.&lt;CR&gt;&lt;LF&gt;
  data: (354, 'End data with &lt;CR&gt;&lt;LF&gt;.&lt;CR&gt;&lt;LF&gt;')
  send: 'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\nSubject: subject supplied\r\n\r\n\r\n&lt;CONTENT REMOVED&gt;.\r\n'
  reply: '250 2.0.0 Ok: queued as 7548F12004F\r\n'
  reply: retcode (250); Msg: 2.0.0 Ok: queued as 7548F12004F
  data: (250, '2.0.0 Ok: queued as 7548F12004F')
  {}
  </code></pre>
  
  <p>The trailing empty {} indicates success. Which is confirmed by the maillogs:</p>
  
  <pre><code>Feb  1 11:02:06 &lt;localhost&gt; postfix/smtpd[19642]: 7548F12004F: client=localhost[127.0.0.1]
  Feb  1 11:02:06 &lt;localhost&gt; postfix/cleanup[18681]: 7548F12004F: message-id=&lt;20170201110206.7548F12004F@&lt;localhost&gt;.localdomain&gt;
  Feb  1 11:02:06 &lt;localhost&gt; postfix/qmgr[2243]: 7548F12004F: from=&lt;ngsservice@&lt;localhost&gt;.localdomain&gt;, size=4685, nrcpt=1 (queue active)
  Feb  1 11:02:06 &lt;localhost&gt; postfix/smtp[19867]: 7548F12004F: to=me@my_email_address, relay=&lt;our ip address&gt;[&lt;our ip address&gt;]:25, delay=0.5, delays=0/0.01/0/0.48, dsn=2.6.0, status=sent (250 2.6.0 &lt;20170201110206.7548F12004F@&lt;localhost&gt;.localdomain&gt; [InternalId=32804943] Queued mail for delivery)
  Feb  1 11:02:06 &lt;localhost&gt; postfix/qmgr[2243]: 7548F12004F: removed
  </code></pre>
  
  <p>However I get the exact same return if I pass an entirely invalid email address (one which when tested from e.g. Outlook returns a non-delivery receipt). </p>
  
  <p>Console output indicates the system is capable of DSN. Running the debug command <code>sendmail -bv &lt;me@my_email_address&gt;</code> returns the console message <code>Mail Delivery Status Report will be mailed to &lt;me&gt;</code>, and the maillog has DSN entries:</p>
  
  <pre><code>Jan 26 08:34:38 &lt;localdomain&gt; postfix/pickup[16208]: 83BC11200A5: uid=&lt;my_uid&gt; from=&lt;me&gt;
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/cleanup[8341]: 83BC11200A5: message-id=&lt;20170126083438.83BC11200A5@&lt;localdomain&gt;.localdomain&gt;
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/qmgr[2243]: 83BC11200A5: from=&lt;me@&lt;localdomain&gt;.localdomain&gt;, size=316, nrcpt=1 (queue active)
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/smtp[8343]: 83BC11200A5: to=&lt;me@my_email_address&gt;, relay=&lt;our ip address&gt;[&lt;our ip address&gt;]:25, delay=0.06, delays=0.03/0.01/0.01/0.02, dsn=2.1.5, status=deliverable (250 2.1.5 Recipient OK)
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/cleanup[8341]: 8F0591200A7: message-id=&lt;20170126083438.8F0591200A7@&lt;localdomain&gt;.localdomain&gt;
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/bounce[8344]: 83BC11200A5: sender delivery status notification: 8F0591200A7
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/qmgr[2243]: 83BC11200A5: removed             
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/qmgr[2243]: 8F0591200A7: from=&lt;&gt;, size=1988, nrcpt=1 (queue active)
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/local[8345]: 8F0591200A7: to=&lt;me@&lt;localdomain&gt;.localdomain&gt;, relay=local, delay=0.02, delays=0/0.01/0/0.01, dsn=2.0.0, status=sent (delivered to mailbox)
  Jan 26 08:34:38 &lt;localdomain&gt; postfix/qmgr[2243]: 8F0591200A7: removed
  </code></pre>
  
  <p>So the system appears to be capable of DSN Delivery Status Notification which would appear a good way fwd but I need to help on how to implement this. This is a new area for me so explicit details would be great. Many thanks</p>