I am using the Python library smtplib to send Email messages with Office365 as the SMTP server. Everything was fine until a few days ago when my From: header seemed to not be processed. The Python code I am using:
import smtplib
from email.mime.text import MIMEText
def login():
server = smtplib.SMTP(mail.mail_server, mail.mail_port)
server.starttls()
server.login(mail.mail_username, mail.mail_password)
return server
def send(subject, body):
msg = MIMEText(body)
msg["From"] = mail.mail_from
msg["To"] = ", ".join(mail.mail_to)
msg["Subject"] = subject
server = login()
server.sendmail(mail.mail_username, mail.mail_to, msg.as_string())
server.quit()
send("test", "test")
What is strange is that if I log onto Outlook or OWA, I can see my header if I view the sent message details. I sent a test header of From: xxxx <info#...>
However on the recipients inbox message, the header is simply the Office365 User's name and the info address as shown above (From: Name <info#...>). The xxxx custom header is gone.
What can be causing my header to be dropped?
While it may not be an ideal solution, I ended up setting up a new email account with the name I wanted and used that, instead of trying to force the From: header.
Related
I am developing a simple system for obtaining stock information and sending it to me by email. The problem is occurring with the function that should send the email.
I'm trying to use Gmail to do this and it works on my machine, but when it runs on the Google Cloud Function instance, an error is generated. The error says:
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbt\n5.7.14 DI7EWTXsfkIgcqDF1iU9gzO45O2rHEDek8EzvslcMMuOLXixqGFX_YhwsIz9P9OsZkRDf\n5.7.14 F7fWu8KmILP4m54Yb925Tl93FSuXLCUiWn7REseAjcak7RpZ_2tgWliAGFufsPSW>\n5.7.14 Please log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 b18sm13789218iln.46 - gsmtp')
And here is my simple code
import smtplib, ssl, os, sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
SMTP_SERVER = os.getenv('SMTP_SERVER', 'smtp.gmail.com')
SMTP_PORT = os.getenv('SMTP_PORT', 587)
EMAIL_USER = os.getenv('EMAIL_USER')
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD')
def send_mail(email_from, to, subject, body_message, from_name=None):
from_str = '{} <{}>'.format(from_name, email_from) if not from_name is None else email_from
msg = MIMEMultipart()
msg['From'] = from_str
msg['To'] = to
msg['Subject'] = subject
body_html = body_message
msg.attach(MIMEText(body_html, 'html'))
text = msg.as_string()
server = smtplib.SMTP(SMTP_SERVER,SMTP_PORT)
server.ehlo() # Can be omitted
server.starttls()
server.ehlo() # Can be omitted
server.login(EMAIL_USER, EMAIL_PASSWORD)
server.sendmail(email_from, to, text)
server.quit()
The above function works perfectly on my machine. I already enabled insecure apps on this account.
I saw a post here that was talking about something like that. At one point in the post you can find the quote below.
Still not working? If you still get the SMTPAuthenticationError but now the code is 534, its because the location is unknown. Follow this link:
https://accounts.google.com/DisplayUnlockCaptcha
So, this is the error that I'm getting but I don't know how to solve it inside of a Cloud Function.
Here is the link to the full discussion: Login credentials not working with Gmail SMTP
I wanted fetch issues from from project in jira using jira api and send it to groupemail via jira automatically. I am using PYTHON. I have tried fetching data using jira api. But am stuck sending email . Could someone please help here. How can we do it either SMTP or any API.?
You can use the smtplib module in Python to send the email messages. Make sure you do have the SMTP host & SMTP port values for your account with you.
The below is a simple script to send email from your account.
import os
import smtplib
from email.message import EmailMessage
RECEIVER_EMAIL_LIST = [] #list of recipients
SMTP_SERVER = os.environ['smtp_server']
SMTP_PORT = os.environ['smtp_port']
SENDER_EMAIL = os.environ['my_email']
SENDER_PASSWORD = os.environ['my_email_password']
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.starttls()
server.login(SENDER_EMAIL, SENDER_PASSWORD)
message = EmailMessage()
email_body_content = 'Your content here'
message.set_content(email_body_content)
message['Subject'] = 'Subject goes here'
message['From'] = SENDER_EMAIL
message['To'] = ', '.join(RECEIVER_EMAIL_LIST)
server.send_message(message)
For documentation, click here.
I was wasting another day of my life messing around with python and I was thinking about how to spoof an email address using smtplib in python. I thought it could be possible to spoof the from address by using the code below but it didn't work. I have seen projects on github that seem to be able to actually spoof the email but if i were to create a program of my own that does this, how exactly would it work? Any ideas? Code:
import smtplib
username = (mygmailusername)
password = (mypassword)
fakefrom = "donaldtrump#gmail.com"
toEmail = (toAddress)
server = smtplib.SMTP("smtp.gmail.com",587)
server.starttls()
server.login(username,password)
server.sendmail(fakefrom,toEmail,"this is the fbi. OPEN UP")
server.close()
Most providers check the account with the sender address, and disallow if the sender address dosn't match with sender. Try if you have a setup box network, to use your provider network smtp server without authentification on smtp 25 port, and most of time the mail can be sent. But with that method, many receiver servers will tag directly this mail as spam/dangerous. You can try that directly in your message tool (outlook,...) it s the same behavior as you code.
Sorry I'm a bit late. The problem, here lies with the second-to-last line:
server.sendmail(fakefrom,toEmail,"this is the fbi. OPEN UP")
You are trying to use the 'fake from' address to actually send the email which isn't what you want to be doing. You just want to 'spoof' it and make the recipient think that the email came from a different address. You do that by defining the sender details in the message body.
The code that you would need to use to make this work would be:
import smtplib
username = (mygmailusername)
password = (mypassword)
fake_from = "donaldtrump#gmail.com"
fake_name = "Donald Trump"
to_email = (toAddress)
to_name = (toName)
subject = "Bonjour"
content = "This is the fbi. OPEN UP"
message = f"From: {fake_name} <{fake_from}>\nTo: {to_name} <{to_email}>\nSubject: {subject}\n\n{content}"
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(username, to_email, message.encode())
server.close()
I am working on script to send emails.
Emails are supposed to be send from a shared mailbox in outlook.
This shared mailbox doesn't have a password and here is the trick: on the one hand I need the authenticate and provide a password, on the other hand I have no password to provide.
I ran the scripted with my professional outlook mailbox (email address + password) and it works.
Here is my config:
MY_ADDRESS = "---emailaddressofthesender---"
PASSWORD = ""
MESSAGE = "some message"
# set up the SMTP server
s = smtplib.SMTP(host='smtp-mail.outlook.com', port=587)
s.starttls()
s.login(MY_ADDRESS, PASSWORD)
msg = MIMEMultipart() # create a message
# setup the parameters of the message
msg['From']=MY_ADDRESS
msg['To']="---emailaddresseofthereceiver---"
msg['Subject']="This is TEST"
# add in the message body
msg.attach(MIMEText(MESSAGE, 'plain'))
# send the message via the server set up earlier.
s.send_message(msg)
del msg
# Terminate the SMTP session and close the connection
s.quit()
I tried:
Read the doc smtplib. Unless mistaken, I found no mentions about
authentications without password.
PASSWORD = "": authentication error
PASSWORD = None: authentication error
s.login(MY_ADDRESS): PASSWORD argument is missing
remove s.login() method from the script: Client was not authenticated to send anonymous mail during MAIL FROM [PR0P264CA0184.FRAP264.PROD.OUTLOOK.COM]
The only working solution I have now is to send from my mailbox credentials and not from the shared mailbox. For the sake of this project, this solution is not acceptable.
Many thanks in advance for your help
Add this right before the s.send() step and it will work.
s.SentOnBehalfOfName = 'SharedFolder'
I'm trying really hard to check why this is happening but not really sure if its on gmail's server side or in a part of the script I'm using.
The problem is that I'm trying to send an email to a mailing list (I have found many posts explaining how to include various emails but none explaining if there is a limitation or workaround to send it to a single email which contains multiple addresses).
In this case I want to send the email to many different people which make part of the BI team of our company (bi#company.com), normally sending an email to this address will result in everyone from the team getting the email, but I can't manage to make i work, and I don't want to include all emails in the list because there are too much and it will have to be manually changed every time.
When I try this with another single person email it works perfectly
import smtplib
sender = 'server#company.com'
receivers = ['bi#company.com']
q = ""
message = """From: Error alert <Server-company>
To: BI <bi#company.com>
Subject: Error e-mail
%s
""" % q
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Alright. I found out why this was not working in my organization. Hopefully this can help someone else out. The outlook groups were setup to only work from an internal email address.
Sending emails programmatically, may use a relay method, which has no authentication. As such the email will never go through. The fix is to have the Network ops people (or exchange group) at your company turn on receiving external emails. The emails will then go through.
make recivers a list of emails you want to send:
example:
recivers = ['a#gmail.com','b#gmail.com,...]
try like this:
import smtplib
from email.mime.text import MIMEText
sender = 'server#company.com'
receivers = ['a#company.com','b]#company.com'....]
server = smtplib.SMTP('smtp#serv.com',port)
server.starttls()
msg = MIMEText('your_message')
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(receivers)
server.login('username','password')
server.sendmail(sender, recipients, msg.as_string())
server.quit()
I finally decided to stick to writing the full list of email addresses inside the list. Apparently if mailing lists are managed locally the re distribution of the emails cant be done.