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.
Related
I tried with "allow less secure app" and "unlockCaptcha", but still not work.
How to know if the problem is with my account or with the code?
Thanks for attention, here teh code:
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
# create message object instance
msg = MIMEMultipart()
message = "Prova"
# setup the parameters of the message
password="pass"
msg['From'] = "senders#example.com"
msg['To'] = "receiver#example.com"
msg['Subject'] = "Prova"
# add in the message body
msg.attach(MIMEText(message, 'plain'))
#create server
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
# Login Credentials for sending the mail
server.login(msg['From'], password)
# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
print(f"successfully sent email to {msg['To']}")
So I have seen lots of great info on here about sending automated emails using python. However, my task is slightly different. The script im working on needs to send an automated email when some condition is true then the email contains the printed output. But since this is for work the file is stored on a shared server and therefore, I cannot be the one to "send" the email, it needs to send automatically from the file. My question is therefore how this email can be sent with no "from" address, or if this is even possible.
Additionally, how can I make sure that the email contains the printed output? Below is the attached code im using. The variables stored in the list are dataframes.
mylist = [right_branchcode, right_branchname, right_sellingcode, right_advcorp, right_childparent, \
right_eliteCategoryId, right_partyid, right_retailmga]
flag = True
for item in mylist:
if len(item) > 0:
print(item)
flag = True
else:
pass
def send_email(audit):
fromaddr = " >"
toaddr = "recip#mail, recip2#mail"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Alert: Audit Mismatch For DimAdvisor"
body = "Current mismatch in dimAdvisor found on : " + temperature
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp-mail.outlook.com', 587)
server.starttls()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Any help would be great. Thanks!
This is how you can send email using Python:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
recipients = ['john.doe#example.com', 'john.smith#example.co.uk']
msg = MIMEMultipart()
msg['From'] = "Can be any string you want, use ASCII chars only " # sender name
msg['To'] = ", ".join(recipients) # for one recipient just enter a valid email address
msg['Subject'] = "Subject"
body = "message body"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587) # put your relevant SMTP here
server.ehlo()
server.starttls()
server.ehlo()
server.login('jhon#gmail.com', '1234567890') # use your real gmail account user name and password
server.send_message(msg)
server.quit()
Please note this line:
msg['From'] = "Can be any string you want, use ASCII chars only " # sender name
You can write any string in the From field regardless of any real email address. I didn't try to leave it empty but I guess you can try it yourself :-)
In my implementation of smtpd.SMTPServer, I can receive messages via the process_message() callback. How could I send a reply message back to the sender?
This is a script that can send email it works for me. I don't know if is that you want but I hope its help you.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "YOUR ADDRESS"
toaddr = "ADDRESS YOU WANT TO SEND TO"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "SUBJECT OF THE MAIL"
body = "YOUR MESSAGE HERE"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "YOUR PASSWORD")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
I'm writing a little script to send emails and I would like to track who opened the email with the python module pytracking like so:
import smtplib
import pytracking
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
fromaddr = "info#XXXXXXXXXX.com"
toaddr = "XXXXXXXXa#gmail.com"
msg = MIMEMultipart()
#Not sure if this is the right location for the pixel
pixel= (pixel_byte_string, mime_type) = pytracking.get_open_tracking_pixel()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "grande"
body = "bla bla bla"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('mail.XXXXXXXX.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('info#XXXXXXXX.com','XXXXXXXX')
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
print('DONE')
I read the full documentation of this module here , but I can not find from where to actually check if the pixel has been opend or which command shell I run to do so.
Can anyone advise how to check if the pixel has been opened?
Thanks
I am looking for a quick example on how to send Gmail emails with multiple CC:'s. Could anyone suggest an example snippet?
I've rustled up a bit of code for you that shows how to connect to an SMTP server, construct an email (with a couple of addresses in the Cc field), and send it. Hopefully the liberal application of comments will make it easy to understand.
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
## The SMTP server details
smtp_server = "smtp.gmail.com"
smtp_port = 587
smtp_username = "username"
smtp_password = "password"
## The email details
from_address = "address1#domain.com"
to_address = "address2#domain.com"
cc_addresses = ["address3#domain.com", "address4#domain.com"]
msg_subject = "This is the subject of the email"
msg_body = """
This is some text for the email body.
"""
## Now we make the email
msg = MIMEText(msg_body) # Create a Message object with the body text
# Now add the headers
msg['Subject'] = msg_subject
msg['From'] = from_address
msg['To'] = to_address
msg['Cc'] = ', '.join(cc_addresses) # Comma separate multiple addresses
## Now we can connect to the server and send the email
s = SMTP_SSL(smtp_server, smtp_port) # Set up the connection to the SMTP server
try:
s.set_debuglevel(True) # It's nice to see what's going on
s.ehlo() # identify ourselves, prompting server for supported features
# If we can encrypt this session, do it
if s.has_extn('STARTTLS'):
s.starttls()
s.ehlo() # re-identify ourselves over TLS connection
s.login(smtp_username, smtp_password) # Login
# Send the email. Note we have to give sendmail() the message as a string
# rather than a message object, so we need to do msg.as_string()
s.sendmail(from_address, to_address, msg.as_string())
finally:
s.quit() # Close the connection
Here's the code above on pastie.org for easier reading
Regarding the specific question of multiple Cc addresses, as you can see in the code above, you need to use a comma separated string of email addresses, rather than a list.
If you want names as well as addresses you might as well use the email.utils.formataddr() function to help get them into the right format:
>>> from email.utils import formataddr
>>> addresses = [("John Doe", "john#domain.com"), ("Jane Doe", "jane#domain.com")]
>>> ', '.join([formataddr(address) for address in addresses])
'John Doe <john#domain.com>, Jane Doe <jane#domain.com>'
Hope this helps, let me know if you have any problems.
If you can use a library, I highly suggest http://libgmail.sourceforge.net/, I have used briefly in the past, and it is very easy to use. You must enable IMAP/POP3 in your gmail account in order to use this.
As for a code snippet (I haven't had a chance to try this, I will edit this if I can):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
#EDIT THE NEXT TWO LINES
gmail_user = "your_email#gmail.com"
gmail_pwd = "your_password"
def mail(to, subject, text, attach, cc):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
#THIS IS WHERE YOU PUT IN THE CC EMAILS
msg['Cc'] = cc
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
mail("some.person#some.address.com",
"Hello from python!",
"This is a email sent with python")
For the snippet I modified this