The recipient address and the distro lists do not immediately resolve. Outlook is smart enough to recognize the email addresses, but it doesn't recognize my distro list. At least not immediately.
I'm using Office 365 if that matters.
from win32com.client import Dispatch
outlook = Dispatch('Outlook.Application')
Mail_Item = outlook.CreateItem(0)
# This sends no problem
# Mail_Item.To = 'first.last#company.com'
# This does not send
Mail_Item.To = 'Contact_Group_Test'
Mail_Item.Subject = "Subject_text"
Mail_Item.Body = "Body_text"
Mail_Item.Recipients.ResolveAll()
Mail_Item.Send()
While troubleshooting I used Mail_Item.Display() to see the message. After a few seconds it resolves all my addresses, including the contact group. HOWEVER, the contact group itself still doesn't work despite this.
If you couldn't solve, you can try smtplib.
import smtplib
sender = 'from#fromdomain.com'
receivers = ['to#todomain.com']
message = """From: From Person <from#fromdomain.com>
To: To Person <to#todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
If you will send mail from outlook account, you have to write smtp-mail.outlook.com instead of localhost
mtbenj's answer may work for others. I'm just choosing to go the win32com route because of my organization.
For whatever reason using 'Test', and I'm assuming, any one-word contact list takes ~5 seconds to resolve. I counted after the .Display() popup came up. Naming it 'Test_' didn't do it for me either.
I decided to try one named 'Test_Test'. After calling .Recipients.ResolveAll(), it works perfect.
So use an underscore and multiple words.
Related
I have set up an automation process for a few business critical processes and would like to set up channel notifications in slack. When I send emails to recipients within the business it works fine! However, as soon as I change the email to an external email address (such as a gmail address) I get the error 550, b'5.7.54 SMTP; Unable to relay recipient in non-accepted domain. Does anyone know how to resolve this?
My Code
import smtplib
sender_email = "middleofficenotifications#mycompany.co.nz"
rec_email = ['example#gmail.com']
SUBJECT = "This is a TEST"
TEXT = "Please ignore this test, just seeing if the distribution group gets this message. It is DM setting up notifications for code failing/db issues that need to be handled by MO"
message = 'Subject: {}\n\n{}'.format(SUBJECT, TEXT)
server = smtplib.SMTP('outlook.mycompany.co.nz')
server.sendmail(sender_email,rec_email, message)
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'm making a script that notifies people about some pending tickets in JIRA. These notifications are sent by e-mail, I already got the notification to trigger, but I'm having problems sending the emails.
I can send them using gmail but when I tried to do it with my official account (the one that the company gave me) I am not able to send them. IT guys already provided me the 'localhost' because they use SMTP relays and the port, but they keep telling me that I should start SMTP without authentication, I'm not very sure of how to do this.
The example I found on internet was this:
import smtplib
fromaddr = 'Axel.Sa#mydomain.com'
toaddrs = ['Axel.Sa#mydomain.com']
msg = '''
From: {fromaddr}
To: {toaddr}
Subject: testin'
This is a test
.
'''
msg = msg.format(fromaddr=fromaddr, toaddr=toaddrs[0])
server = smtplib.SMTP('localhost:25')
server.starttls()
server.ehlo("mydomain.com")
server.mail(fromaddr)
server.rcpt(toaddrs[0])
server.data(msg)
server.quit()
But I keep getting this error, If someone can tell me the proper way of sending emails by SMTP without authentication I will be very grateful.
Check this stack:
How to send an email without login to server in Python
change your smtplib.SMTP('localhost:25') to smtplib.SMTP('localhost', 25)
I am using python 3.4.3 to send e-mail, and at this time I will be needing the e-mail to be sent under an alias. The account is a gmail account, but I need to be able to put whatever I want as the spoof(alias) 'From' e-mail. I have looked quite hard at how to do this and have had very little luck. Given the amount of threads I've looked at and the actuality that I haven't gotten a workable answer shows the lack of discussion about this specific topic. I hope it's not just that this is something so very easy that everyone but me knows how to do it.
I should mention that I am on a windows 10 machine, but have access to a Ubuntu, and Windows 7 machine as well.
import smtplib
fromreal = 'realmail#gmail.com'
fromshow = 'fakemail#gmail.com'
toaddy = ['rec01#gmail.com', 'rec02#gmail.com']
subject = ' test'
body = 'This is the body test'
content = '''\
From: %s
To: %s
Subject: %s
%s
''' % (fromshow, ', '.join(toaddy), subject, body)
server = 'smtp.gmail.com'
port = 587
mail = smtplib.SMTP(server, port)
mail.ehlo()
mail.starttls()
mail.login(fromreal, 'password')
try:
mail.sendmail(fromshow, toaddy, content)
print('E-mail sent.')
except:
print('E-mail not sent.')
mail.close()
You can use yagmail to send an alias (not changing to the fake email, but at least the alias):
import yagmail
# first is "from" arg; using a dictionary you can give an alias as value
yag=yagmail.SMTP({fromreal:'fakealias'}, 'password')
yag.send(toaddy, subject, body)
How nice it is to have 3 lines instead of 30 ;)
Install using pip install yagmail.
Read more about a lot of other features on the github page.
Among other things, you could use "passwordless" scripts (no need for password in script), really easy to send HTML, inline images and attachments!
Full disclosure: I'm the developer/maintainer of yagmail.
Your code is fine,
Google prevents you from setting an alias email that not belongs to you. That's why you need to set the alias in your gmail account. To do this go to
https://mail.google.com/ -> settings -> (see all settings) -> Accounts -> Send mail as: -> add another email address.
Validate the email address and then you can set your alias as used in your code.
If you get an SMTPAuthenticationError (534, b'5.7.9 Application-specific password required. ...) you should follow the link to set an app-password instead of your real password.
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.