I wrote a small python script to automatically send emails every day at 10 am. The script works fine from my Laptop running Linux Mint 18. But when I deploy the script to my server (droplet taken from DigitalOcean) running Ubuntu 16.04.3 the script gives an STMP authentication error.
Allow less secure apps to sign in and other settings are enabled on my Gmail account.
Here's the script -
import schedule
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromaddr = "yadullahabidi#gmail.com"
toaddr = "yadullahabidi#gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Attendance for Yadullah Abidi"
body = "Good morning Surender Ji. I reached office at 10:00 AM today."
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr,"password")
text = msg.as_string()
def job():
server.sendmail(fromaddr, toaddr, text)
schedule.every().day.at("08:00").do(job)
while True:
schedule.run_pending()
time.sleep(1)
All SMTP actions are blocked with DigitalOcean and you need to create a ticket with Support via the control panel and ask to un-block SMTP. This was implemented to prevent SPAM.
Your other option is to use a third party mail service that allow port 2525 for mail like mandrill or mailgun if the resolution process is too long.
Reference : https://meta.discourse.org/t/digital-ocean-is-blocking-outgoing-mail/66740/24
Related
I have an SMTP setup where I am sending emails to an email address as well as a text to an email address like this. Though the problem is the email receives the full message, which is "Phase monitor, system off" but the phone just receives "Phase monitor, system o". I've sent longer messages and it received them so I don't think it is a length issue. Any ideas why the text is different? Code below.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
emails = ["me#gmail.com", "my_number#tmomail.net"]
server = smtplib.SMTP('smtp.office365.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("username", "password")
for email in emails:
msg = MIMEMultipart()
msg['From'] = 'me#work.com'
msg['To'] = email
msg['Subject'] = 'Chiller Alarm'
body = "Phase monitor, system off"
msg.attach(MIMEText(body))
server.sendmail("me#work.com", email, msg.as_string())
server.quit()
Oddly enough, I just added 8 f's to "off" so it became "offffffff" and it sent with 6 f's. So it there's that?
Answer? When I added \n to the end of the message it sent correctly. I have no clue what is going on with this but at least it is working.
I have a python script saved on an Amazon EC2 instance that sends an email:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = "filleremail#gmail.com"
my_password = r"password"
you = "receiver#gmail.com"
msg = MIMEMultipart('alternative')
msg['Subject'] = "Alert"
msg['From'] = me
msg['To'] = you
#TODO: How do I get email text from user
part2 = MIMEText(email_text)
msg.attach(part2)
# Send the message via gmail's regular server, over SSL - passwords are being sent, afterall
s = smtplib.SMTP_SSL('smtp.gmail.com')
# uncomment if interested in the actual smtp conversation
# s.set_debuglevel(1)
# do the smtp auth; sends ehlo if it hasn't been sent already
s.login(me, my_password)
s.sendmail(me, you, msg.as_string())
s.quit()
I want to submit text from a mobile app using a URL to fill the email_text variable. I have some experience with POST and GET requests but not on connecting them through Python. Can someone please point me in the right direction?
So far I have only been able to send emails. Here's my code:
import smtplib
email_user = 'myemail#gmail.com'
server = smtplib.SMTP ('smtp.gmail.com', 587)
server.starttls()
server.login(email_user, 'email pass')
#SET TIME HERE?
from crontab import CronTab
#EMAIL
message = 'sending this from python!'
server.sendmail(email_user, email_user, message)
server.quit()
I'm struggling to set a time to send the email. If someone can also help me figure out how to add attachments, that would be great!
Assuming you already have your send_email() function working I would do:
import datetime as dt
import time
import smtplib
def send_email():
email_user = 'myemail#gmail.com'
server = smtplib.SMTP ('smtp.gmail.com', 587)
server.starttls()
server.login(email_user, 'email pass')
#EMAIL
message = 'sending this from python!'
server.sendmail(email_user, email_user, message)
server.quit()
send_time = dt.datetime(2018,8,26,3,0,0) # set your sending time in UTC
time.sleep(send_time.timestamp() - time.time())
send_email()
print('email sent')
If you want to send the email regularly, you can do:
import datetime as dt
import time
import smtplib
def send_email():
email_user = 'myemail#gmail.com'
server = smtplib.SMTP ('smtp.gmail.com', 587)
server.starttls()
server.login(email_user, 'email pass')
#EMAIL
message = 'sending this from python!'
server.sendmail(email_user, email_user, message)
server.quit()
def send_email_at(send_time):
time.sleep(send_time.timestamp() - time.time())
send_email()
print('email sent')
first_email_time = dt.datetime(2018,8,26,3,0,0) # set your sending time in UTC
interval = dt.timedelta(minutes=2*60) # set the interval for sending the email
send_time = first_email_time
while True:
send_email_at(send_time)
send_time = send_time + interval
You can also spawn a thread and leave this thread handle the sending of the email.
Best way to send an email using CRON is to use Postfix and mailutils. Follow below steps to send email with cron job results/errors.
Step 1 — Installing Postfix
First, update the package database:
sudo apt update
Next, install mailtuils:
sudo apt install mailutils
Finally, install postfix:
sudo apt install postfix
Near the end of the installation process, you will be presented with a window that looks like the one in the image below. The default option is Internet Site. That’s the recommended option for this tutorial, so press TAB, then ENTER
Select No Configuration
Step 2 — Configuring Postfix
sudo nano /etc/postfix/main.cf
Then paste below code on the empty file.
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only
mydestination = $myhostname, localhost.$your_domain, $your_domain
Save and close the file.
Finally, restart Postfix.
sudo systemctl restart postfix
Step 3 — Testing the SMTP Server
echo "This is the body of the email" | mail -s "This is the subject line" your_email_address
I'm trying to send an email within python, but the program is crashing when I run it either as a function in a larger program or on it's own in the interpreter.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromaddr = "exampleg#gmail.com"
toaddr = "recipient#address.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Hi there"
body = "example"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "Password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
In the interpreter, it seems to fail with server = smtplib.SMTP('smtp.gmail.com', 587)
Any ideas?
My standard suggestion (as I'm the developer of it) is yagmail.
Install: pip install yagmail
Then:
import yagmail
yag = yagmail.SMTP(fromaddr, "pasword")
yag.send(toaddr, "Hi there", "example")
A lot of things can be made easier using the package, such as HTML email, adding attachments and avoiding having to write passwords in your script.
For the instructions to all of this (and more, sorry for the cliches), have a look at the readme on github.
That's because you are getting error when trying to connect Google SMTP server.
Note that if you are using Google SMTP you should use:
Username: Your gmail address
Password: Your gmail password
And you should be already logged in. If you still get an error, you should check what is the problem in this list: https://stackoverflow.com/a/25238515/1600523
Note: You can also use your own SMTP server.
I have a React website up and running and I need to implement a mailer - it's for a wedding website and people would fill up the confirmation form and it'll automatically send e-mail to the organizers.
I've did it before using Ajax + PHP but I've wanted to learn Python and thought this would be a great opportunity.
Ok, so I do have a Python script that correctly sends an e-mail when I run it on my machine:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "from#email"
toaddr = "to#email"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Subject"
body = "Message"
msg.attach(MIMEText(body, 'plain', 'utf-8'))
server = smtplib.SMTP('smtp server', port)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Now I don't know how to proceed with the next steps:
Call Python script (passing parameters) from my React App;
Host Python script on my web server (Dreamhost).
I've already searched about Flask but I'm not sure if this is what I need.
Could someone please enlighten me?
Thanks!