I'd like to send an email from a python3 process on a wmcs VPS to report errors.
I looked at https://wikitech.wikimedia.org/wiki/Help:Email_in_Cloud_VPS but could use some help.
sudo echo "Subject: sendmail test2" | /usr/sbin/sendmail -v <myemail> works.
When I try to send the equivalent from python smtplib I get a 221 error message.
Thanks,
Tim
On Mon, May 15, 2023 at 2:57 PM Tim Moody tim@timmoody.com wrote:
I'd like to send an email from a python3 process on a wmcs VPS to report errors.
I looked at https://wikitech.wikimedia.org/wiki/Help:Email_in_Cloud_VPS but could use some help.
sudo echo "Subject: sendmail test2" | /usr/sbin/sendmail -v <myemail> works.
The `sudo` here does nothing useful. It is bound to the `echo` invocation and not the `sendmail` one.
When I try to send the equivalent from python smtplib I get a 221 error message.
221 is the SMTP status code for closing connection/goodbye. This isn't specifically an error, instead it means that the SMTP server has decided to end the session.
* Are there other status codes you see from your attempted python code prior to the 221? * What SMTP server are you connecting to? * Is the python code available somewhere for review?
Here is a quick example of sending email using Python 3.9 and smtplib from inside Toolforge:
$ become bd808-test -- webservice python3.9 shell -- python3.9
import smtplib import ssl context = ssl.create_default_context() server = smtplib.SMTP("mail.tools.wmcloud.org", 587) server.starttls(context=context)
(220, b'TLS go ahead')
server.sendmail("bd808-test.maintainers@toolforge.org",
"bd808@wikimedia.org", """Subject: smtplib example from Toolforge ... ... Hello world. ... --- ... Bryan""") {}
server.quit()
(221, b'mail.tools.wmcloud.org closing connection')
Things would typically look similar from a Cloud VPS project. The major change would be to use mx-out03.wmcloud.org or mx-out04.wmcloud.org as your outbound SMTP service. There is a bit more complication in using TLS as well due to the x509 certificate being a bit of a mess (bad subject and expired):
$ ssh devportal-demo01.devportal.eqiad1.wikimedia.cloud $ python3.9
import smtplib import ssl context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE server = smtplib.SMTP("mx-out03.wmcloud.org", 25) server.starttls(context=context)
(220, b'TLS go ahead')
server.sendmail("bd808@wikimedia.org", "bd808@wikimedia.org",
"""Subject: smtplib example from Cloud VPS ... ... Hello world. ... --- ... Bryan""") {}
server.close()
I hope that helps a bit.
Bryan
Thanks. That got me closer. I was not using ssl. When I took your code and tried to connect to localhost I got
STARTTLS extension not supported by server
when I connected to mx-out03.wmcloud.org I got
(220, b'TLS go ahead')
But when I then do
server.sendmail("noreply@wikimedia.org", "tim@tim.com", ... """Subject: smtplib example from Cloud VPS ... ... ... ... Hello world. ... ... --- ... ... Bryan""")
I get SSL: WRONG_VERSION_NUMBER
In the console /usr/sbin/sendmail -v tim@tim.com <email.txt
works just fine, so maybe I should just use subproc.
On Mon, May 15, 2023 at 7:44 PM Bryan Davis bd808@wikimedia.org wrote:
On Mon, May 15, 2023 at 2:57 PM Tim Moody tim@timmoody.com wrote:
I'd like to send an email from a python3 process on a wmcs VPS to report
errors.
I looked at https://wikitech.wikimedia.org/wiki/Help:Email_in_Cloud_VPS
but could use some help.
sudo echo "Subject: sendmail test2" | /usr/sbin/sendmail -v <myemail>
works.
The `sudo` here does nothing useful. It is bound to the `echo` invocation and not the `sendmail` one.
When I try to send the equivalent from python smtplib I get a 221 error
message.
221 is the SMTP status code for closing connection/goodbye. This isn't specifically an error, instead it means that the SMTP server has decided to end the session.
- Are there other status codes you see from your attempted python code
prior to the 221?
- What SMTP server are you connecting to?
- Is the python code available somewhere for review?
Here is a quick example of sending email using Python 3.9 and smtplib from inside Toolforge:
$ become bd808-test -- webservice python3.9 shell -- python3.9
import smtplib import ssl context = ssl.create_default_context() server = smtplib.SMTP("mail.tools.wmcloud.org", 587) server.starttls(context=context)
(220, b'TLS go ahead')
server.sendmail("bd808-test.maintainers@toolforge.org",
"bd808@wikimedia.org", """Subject: smtplib example from Toolforge ... ... Hello world. ... --- ... Bryan""") {}
server.quit()
(221, b'mail.tools.wmcloud.org closing connection')
Things would typically look similar from a Cloud VPS project. The major change would be to use mx-out03.wmcloud.org or mx-out04.wmcloud.org as your outbound SMTP service. There is a bit more complication in using TLS as well due to the x509 certificate being a bit of a mess (bad subject and expired):
$ ssh devportal-demo01.devportal.eqiad1.wikimedia.cloud $ python3.9
import smtplib import ssl context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE server = smtplib.SMTP("mx-out03.wmcloud.org", 25) server.starttls(context=context)
(220, b'TLS go ahead')
server.sendmail("bd808@wikimedia.org", "bd808@wikimedia.org",
"""Subject: smtplib example from Cloud VPS ... ... Hello world. ... --- ... Bryan""") {}
server.close()
I hope that helps a bit.
Bryan
Bryan Davis Technical Engagement Wikimedia Foundation Principal Software Engineer Boise, ID USA [[m:User:BDavis_(WMF)]] irc: bd808 _______________________________________________ Wikitech-l mailing list -- wikitech-l@lists.wikimedia.org To unsubscribe send an email to wikitech-l-leave@lists.wikimedia.org https://lists.wikimedia.org/postorius/lists/wikitech-l.lists.wikimedia.org/
On Tue, May 16, 2023 at 7:07 AM Tim Moody tim@timmoody.com wrote:
Thanks. That got me closer. I was not using ssl. When I took your code and tried to connect to localhost I got
STARTTLS extension not supported by server
*nod* The local relay is bound to localhost, so I guess we never worried about adding transport security. I would hope that things would work by just leaving out the starttls() call and related ssl setup. I have not tested that myself at this point however.
when I connected to mx-out03.wmcloud.org I got
(220, b'TLS go ahead')
But when I then do
server.sendmail("noreply@wikimedia.org", "tim@tim.com", ... """Subject: smtplib example from Cloud VPS ... ... ... ... Hello world. ... ... --- ... ... Bryan""")
I get SSL: WRONG_VERSION_NUMBER
I saw that in one of my own tests as well. I don't have a good explanation of what triggered it.
In the console /usr/sbin/sendmail -v tim@tim.com <email.txt
works just fine, so maybe I should just use subproc.
If shelling out to the sendmail client works for your use case, go for it! Simpler is nicer in most things. :)
Bryan
wikitech-l@lists.wikimedia.org