Unclear on argument passing to "sendmail'

John Draper lists at webcrunchers.com
Tue Aug 22 13:23:28 EDT 2006


Tim Williams wrote:

> On 21/08/06, John Draper <lists at webcrunchers.com> wrote:
>
>> In "smtplib" module, the "sendmail" method of function is to be  passed
>
> > host, but it is the Domain name
>
>> for the SMTP Server as gotten from the "dig" command?   IE:   dig -tMX
>> would give me the SMTP
>> server.   In my code I have:
>
> >
>
>> try:
>>     print "Sending message to host: %s" % mailHost
>>     server=smtplib.SMTP(mailHost)
>>     server.sendmail(reply_email,email,body)
>>     server.quit()
>> except:
>>     print "Uunable to send"
>>
>> Is mailHost like "mail.t-mobile.com" which I get from the MX record for
>> a given domain?
>> But also,  is the "email" just the mail account,  ie:  the username?
>> without the @<domain>?
>>
>> I need to be able to query the mail server?   Also,  I want to be able
>> to handle
>> the "SMTPRecipientsRefused" exception.   What is the proper syntax for
>> handling
>> this?
>>
>> Do I do it like this?
>>
>> try:
>>     print "Sending message to host: %s" % mailHost
>>     server=smtplib.SMTP(mailHost)
>>     server.sendmail(reply_email,email,body)
>>     server.quit()
>> except SMTPRecipientsRefused:
>>     print "Recipient refused"
>>
>> Is that the right syntax?   I have severe problems with not enough
>> example code.
>>
>
> mailHost is the name of the mail server server you are sending the
> email to/via,  for internet mail this will be a server from the
> recipient's domain's mx records (or your ISP server).

Ok,  that's what I thought,  I wasn't sure if I were to put the MAIN host
name or the MX host name.

>
> reply_email is the full email address of the sender, or the email
> address you wish to appear as the sender. It does not have to be the
> same address as used in the body headers

OK

>
> email is a bad choice for a variable name,  try something like
> to_email,  in your case it should contain the full email address of
> the recipeint or a list of recipients.  

Ok,   so If I already have a MX hostname of "mail.myhost.com",  then
I would put into my "to_email"...    <myusername>@myhost.com for
my user name,  is that right?   And not just <myusername> without the
@myhost.com,   right?

> The address(es)  do not have
> to be the same address as used in the body headers

OK

>
> server.sendmail returns a list of failed recipient email addresses if
> only some failed,   if all the recipients failed you get an exception.

Hmmm - the problem I have is if I knowingly put in a bad recipient,  and
try to send to a unknown user,  I get all appearances that the mail went 
through.
I never got any kind of failed recipient (Which for me,  is a total 
bummer), because
now I cannot know if the mail was sucessfully sent.  Is this normal 
behaviour for
an SMTP server?   I know in SOME instances,  in the case the SMTP server has
to "relay" the mail to another server,  I wouldn't get this exception 
and it would
appear the mail went through,  but will the "sendmail" method also return
the failed recipient's Email address in this case as well?

By the way,  I'm sending this mail to a "sms" gateway to a cellular 
provider,
so the username is their phone number.   But (sigh),  these providers don't
appear to tell me if I put in a bogus phone number.

>
> Apologies for the bad formatting below,  its untested but should show
> you the structure for managing an SMTP msg send.  You could tidy it up
> without too much effort
>
> import sys
>
> for mailHost in MX_records:
> try:
>    print "Sending message to host: %s" % mailHost
>    server=smtplib.SMTP(mailHost)
>    failed = server.sendmail(reply_email,email,body)
>    server.quit()
>    break
> except smtplib.SMTPRecipientsRefused, x :  #all recips failed
>    for recip in x.recipients:
>        print recip
>        server.quit()
>        break
> except smtplib.SMTPDataError, x: # an error at the end of the
>             # message body =  MSG Failed
> # all recips failed
>    print x[0], x[1]
>    server.quit()
>    break
> except smtplib.SMTPSenderRefused, x : # the sender was refused = #MSG 
> failed
> # all recips failed
>    print x[0], x[1]
>    server.quit()
>    break
> except: #can't connect so continue to next MX server - don't fail !!!
>    e_error = str(sys.exc_info()[0])
>    print e_error
>    server.quit()
>    continue
>
> for recip in failed:  # some failed, some didn't
>    print recip

Hmmm - so THAT's how I use the exceptions.  What is "x"?  Is that the 
error code,
or is that the returned dictionary.    I thought I had to do it this way....

   d = server.sendmail(reply_email,email,body)

Where "d" is the dictionary.   But in your above code,  you return a 
"failed",  but
you are not using it in any other part of the code.  Is this because "x" 
when used
in the "except" clause is what I'm to use?   Is "x" and "failed" the 
same dictionary?

Ok,  I got one more question...  in the docs on the smtplib class,  a 
"sendmail"
method also has other arguments,   such as mail_options,  and rcpt_options.
What are they,  and could you give me an example of how to use these 
arguments?
I looked in the docs for further explanation,  but couldn't find any 
references in
any obvious place.

What data type are they?   Are they arrays,  dictionaries, or other objects?
Can I somehow use these to tell the SMTP server to let me know if the
username is invalid and unknown by the SMTP server?   In MY case,  it's
the cell phone provider.

I know this question may be outside the subject of this posting,  but 
the whole
reason why I'm even using SMTP to send "sms" messages is because the
protocol is secret?   Known only by the provider?   I don't suppose 
there is any
standard protocol for sending "sms" messages,  is there?   Thought I would
ask anyway.

John



More information about the Python-list mailing list