Call a Python script from React - python

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!

Related

How do I input text into a Python script on a remote server?

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?

Can't send email (gmail) via python

I have a code which was working like a half year ago. It basiclly sends email.
import smtplib
import socket
gmail_user="SENDERMAIL"
gmail_password="SENDERPASS"
to = 'SENDTOTHIS'
email_text = "ADSADSADSA"
try:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.ehlo()
server.login(gmail_user, gmail_password)
server.starttls()
server.sendmail(gmail_user, to, email_text)
server.close()
#I was using this code below and it was working. I tried above code but it also did not work.
#server = smtplib.SMTP("smtp.gmail.com:587")
#server.ehlo()
#server.starttls()
#server.ehlo()
#server.login(gmail_user, gmail_password)
#server.sendmail(gmail_user, to, email_text)
#server.close()
print("Done")
except Exception as exception:
print(exception)
Here's exception
(534, b'5.7.14
5.7.14 KL7_2qGSLW9IBjP8dKKgP67bEgyKNc5ls76dnVDZcUlVQjJUQb0JX9BIVi_Agb84vKNOKB
5.7.14 fshB0ngZ_Tn8ocDpDHKavRKXmluVjHo5YM7ADKENtWn4aVTxyvaBlbXRGpA1EBh91bdV-o
5.7.14 pwiAWUHXKmRQEuSNSiFcv68DP4a7ghIu9YKnTyqtUEhGd4HgKtxa4Jz0mhSQDjD13UQWYB
5.7.14 -YEL5Sd2h5YxN8kkSAsK-J_hXMbpy7wNyeCov8lq1Aa3spZzgo> Please log in via
5.7.14 your web browser and then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 f132-v6sm3660398wme.24 - gsmtp')
I did try to
logined gmail
add device to trusted devices
turned on IMAP via gmail
let less secure apps
tried this:
https://support.google.com/mail/answer/7126229?visit_id=636711453029417344-336837064&rd=2#cantsignin
There are to many ways to solve this problem. I hope this code helps.
The only thing you need to do is filling the required variables.
import socket
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#
message = "Your message" # Type your message
msg = MIMEMultipart()
password = "********" # Type your password
msg['From'] = "from#gmail.com" # Type your own gmail address
msg['To'] = "To#gmail.com" # Type your friend's mail address
msg['Subject'] = "title" # Type the subject of your message
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
I can also advise to use a simpler library (a wrapper on top of smtplib, to make sure there are no other factors involved).... like yagmail (disclaimer: I'm the developer).
Try to see if this works:
import yagmail
yag = yagmail.SMTP("username", "password")
yag.send(subject="hi")

Script works from PC but not on server

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

Crash when sending email in Python 2.7.10

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.

Send email via python in c++

my question is whether I can send an email via python in a c++ program.
That is my actual python script.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
# From
fromaddr = "...#gmail.com"
# To
toaddr = "..."
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
# subject
msg['Subject'] = "..."
# Text
body = "..."
msg.attach(MIMEText(body, 'plain'))
#smtplib import
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("username", "password")
text = msg.as_string()
#send email
server.sendmail(fromaddr, toaddr, text)
My plan ist it, that I will open python and import the script via the commandline in the programm.
Is there a better way to do it?
The problem is that c++ open via system("python") the commandline, but now I have to write import mail.py in the command line.
Can I do this automaticly with an other order? I will open python, import mail and quit with one order. Is that possible?
Thank You!
Here is the program for an email.
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(email, password)
smtpserver.sendmail(email, recipient, text)
print 'Message Sent!'
smtpserver.close()
The variables: email is your email address, password is the password to your email, recipient is the receiver of the email, and text is the text to send.

Categories