This emailing function is supposed to be triggered by some event in the markets and should send email with actionable info.
For sending emails we need to be using properly configured email account (because of chain of trust), othervise there is very high chance the email gets blocked by receiver (gmail, yahoo, ...).
In this example we have created dummy gmail account and allowed that account to be accessed by an application using only password.
- Settings -> enble access for less secure apps
- and immediatelly before logging to account with the script went here https://accounts.google.com/DisplayUnlockCaptcha so google doesnt block it straight away. Run your code within few minutes, should get recognized by google. Then it should work without restrictions.
Detailed instructions here: https://support.google.com/mail/thread/26987633?hl=en
$ cat send_email.py #!/home/coil/anaconda3/bin/python3 # sources: # https://humberto.io/blog/sending-and-receiving-emails-with-python/ # https://realpython.com/python-send-email/ import smtplib from email.mime.text import MIMEText smtp_ssl_host = 'smtp.gmail.com' smtp_ssl_port = 465 username = 'email@example.com' password = 'putpasswordherefordummyaccount' def send_email(): sender = 'firstname.lastname@example.org' receiver = 'email@example.com' # implicitly joined string msg_body = ("something is going on \n" "with some stock \n" "in the stock market") message = MIMEText(msg_body, "plain") # treat message as dictionary message['subject'] = 'stock event' message['from'] = sender message['to'] = receiver # contact gmail server and send mail via my gmail dummy account try: server = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port) server.login(username, password) server.sendmail(sender, receiver, message.as_string()) server.quit() print("Successfully sent email") except: print("Error: unable to send email") # --- send_email() $
Turns out that gmail is blocking mails sent via plain python smtplib.
But regular bash command line mail command works just fine and I can receive these messages in my gmail. But that works only from my VM on PC, did not work from Google Cloud VM.
'Internet host' setup while installing mail utils turns out to be working configuration on my PC VM.
$ sudo apt-get install mailutils $ mail -s 'subject' firstname.lastname@example.org <<< 'testing message'
Good article about all various mail sending options:
How to send email properly:
Needed to create smtp server in python, still doesnt work
^C(base) coil@free-tier-vm:~/scripts$ python3 -m smtpd -n -c DebuggingServer localhost:1025 ---------- MESSAGE FOLLOWS ---------- b'From: stock event <email@example.com>' b' To: tcoil <firstname.lastname@example.org>' b' Subject: actionable stock event' b'X-Peer: 127.0.0.1' b'' b' Something is going on with some stock.' b' ' ------------ END MESSAGE ------------ ---------- MESSAGE FOLLOWS ---------- b'From: stock event <email@example.com>' b' To: tcoil <firstname.lastname@example.org>' b' Subject: actionable stock event' b'X-Peer: 127.0.0.1' b'' b' Something is going on with some stock.' b' ' ------------ END MESSAGE ------------ ---------- MESSAGE FOLLOWS ---------- b'From: stock event <email@example.com>'