Send email to a mailing list using Python - python

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.

Related

Sending email via Outlook using Python, contact group doesn't resolve

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.

Any idea of how to spoof an email sender in python using smtplib?

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()

Python sending e-mail with SMTP without authentication

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)

Email sent via sendmail showing wrong 'from' address when sent via text message

I have a cronjob that runs a python script to periodically send an email with status updates to me. On the weekend I prefer this to work via text, so I send it to myphonenumber#vtext.com. This works fine, except the 'from' address is incorrect when it goes to the phone. It shows correctly when it is sent to a regular email address. For some reason when it goes through a text it shows the 'from' address as "myusername#servername.wndowsdomain.local". This may simply be a verizon text message thing specifically, but if possible I'd like to figure out how to fix it as I hope to add some reply functionality to this tool.
The machine that the script is on is running Ubuntu 12.04.
Here is the simple python function I have that sends the email:
def sendMail(to,text):
msg = MIMEMultipart('alternative')
msg['Subject'] = "Weekend Report"
msg['From'] = 'servername#mydomain.com'
msg['To'] = to
msg.attach(MIMEText(text,'plain'))
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(msg.as_string())
Would I be better off using something like smtplib and a gmail account or something perhaps?
Sendmail: Setting envelope sender
You may try to set "envelope sender" address too (sender address used in SMTP session).
p=Popen(["/usr/sbin/sendmail","-t","-i","-fservername#mydomain.com"],stdin=PIPE)
Comment: I have added missing -i command line option unrelated to setting envelope sender.

Apple Mail Lists Senders Beyond Defined Senders

I have a question about sending emails in Python to recipients who will be viewing them in the Apple Mail client. I'll be sending emails from my gmail account to people who will be viewing them in Apple Mail. I'm trying to make sure the recipients, my coworkers, see only the sender I intend for them to see, an alias for my office email. When viewed through Apple Mail, though, the email shows both the sender I define and the actual sending address. Here's what I'm running:
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('Warning, deadline X approaching.')
msg['To'] = 'recipient#theoffice.com'
msg['Subject'] = 'Deadline Issues'
msg['From'] = 'automatedwarning#theoffice.com'
msg['From'] = 'automatedwarning#theoffice.com'
server = smtplib.SMTP('smtp.gmail.com')
server.starttls()
server.login('sender','password')
server.sendmail('sender#gmail.com','recipient#theoffice.com',msg.as_string())
server.quit()
The sender is shown as automatedwarning#theoffice.com in all clients except Apple Mail, in which both sender#gmail.com and automatedwarning#theoffice.com are listed. So, how and, if possible, why?
Also, why do I have to set the sender twice to get the displayed sender to change? If I set it only once, it doesn't change in any client.
Thanks for your help!
When you use Google as an SMTP server, it will set the from address to your address. You should consider a commercial solution (like sendgrid or mailchimp's mandrill service.)
http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server

Categories

Resources