We have written a small Python program, where we want to send a mail to our mail address. We have some code, but when it's about to finish it stops and prints the error message. To be exact it stops at server.sendmail
. When we print config.EMAIL_ADDRESS and config.PASSWORD, we receive the right credentials, so thats not the issue (config is where our credentials are stored).
We're using python3 on an ubuntu server.
import config
import smtplib
def send_mail(subject, msg):
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
print (f'EMAIL = {config.EMAIL_ADDRESS} & PASSWORD = {config.PASSWORD}')
server.login(config.EMAIL_ADDRESS, config.PASSWORD)
message = 'Subject: {}\n\n{}'.format(subject, msg)
server.sendmail(config.EMAIL_ADDRESS, config.EMAIL_ADDRESS, message)
server.quit()
print ("Successfully sent email")
except SMTPException:
print ("Error: unable to send email")
subject = "ALERT!!!"
msg = "LPN SENDER IKKE DATA!!! GØR NOGET VED DET!!!"
send_mail(subject, msg)
We have opened port 25 and 587, that should not be a problem. We don't know what else to do. Maybe someone here can help us out. Also we need to establish a secure connection. Is this secure? If not, what can we do to secure the transport of our mail?
UPDATE
ERROR CODE
File "Alarm.py", line 21, in send_mail
server.sendmail(config.EMAIL_ADDRESS, config.EMAIL_ADDRESS, message)
File "/usr/lib/python3.6/smtplib.py", line 855, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\xd8' in position 46: ordinal not in range(128)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "Alarm.py", line 48, in <module>
send_mail(subject, msg)
File "Alarm.py", line 25, in send_mail
except SMTPException:
NameError: name 'SMTPException' is not defined