I'm using the exchangelib python EWS library to send emails. Sending from my mailbox works as expected but how can I send an email from a distribution list I have "Send As" permissions already applied (Have tested in Outlook changing the from address)?
The below code snippet will send the email but "me#domain.com" account shows as the sender. If I change "access_type=DELEGATE" to "access_type=IMPERSONATION", I receive an error "ErrorNonExistentMailbox: The SMTP address has no mailbox associated with it.".
credentials = Credentials("me#domain.com", password)
config = Configuration(server=server, credentials=credentials)
account = Account(primary_smtp_address="distlist#domain.com", config=config, autodiscover=False, access_type=DELEGATE)
message = Message(account=account,subject="subject",folder=None)
message.sender = "distlist#domain.com"
message.to_recipients = "me#domain.com"
message.body = "test"
message.send(save_copy=False, copy_to_folder=False)
Any help or guidance is very much appreciated
A distribution list usually doesn't have its own mailbox in Exchange, so you can't use its email address as primary_smtp_address for the account.
Messages have both a sender and an author field. You could try to set the distribution list email in the author field. Then emails may show up as coming from that email address.
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 am able to send e-mails using Python and the google libraries (googleapiclient, google_auth_oauthlib.flow, and google.auth.transport.requests). I'm trying to get an alternate "from" or "reply-to" address, and it's not working. My code for creating a message is like so:
# Returns an e-mail message suitable for submission through the gmail api.
def create_message(sender, to, subject, message_text):
message = MIMEMultipart()
message['to'] = to
#message['from'] = sender
message['from'] = 'derp#nowhere.com'
message['subject'] = subject
message.attach(MIMEText(message_text, 'html'))
return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
As you can see, I've adjusted the from attribute of the message. When this message is sent, it is from the gmail account owner, not "derp". How do I make the message come from a different address?
Add the e-mail address as whom you want to issue e-mail as a "Send Mail As" account: https://mail.google.com/mail/u/2/#settings/accounts. It is necessary for that other account to verify he has control of that e-mail address.
It appears not to be possible to "spoof" e-mails using gmail oauth without this verification process, though if you use the deprecated "less secure apps", it is still possible.
I am trying to send messages to my phone using the SMTP protocol. If I log into my Google Account (for which I've enabled less secure apps) I'm able to send a message to '5551234567#tmomail.net'. The subject and body of the email arrive on my phone as a text message.
However, when I try to do the same with Python's smtplib library, I don't get a message. Here's the code I'm using:
import smtplib
# Establish a secure session with gmail's outgoing SMTP server using your gmail account
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
# the account that will send the emails
server.login('me#gmail.com', 'password')
# sendmail(from, to, msg)
server.sendmail('me#gmail.com', '5551234567#tmomail.net', 'hey there!')
Does anyone know what I can do to get the text message to come through from the smtplib? Any suggestions are very welcome!
Try to check the link below.
If seems like you for forgot
server.ehlo()
How to send an email with Gmail as provider using Python?
Please let us know if you see python message in SENT emails folder of Gmail Inbox.
If yes, try to find a differences between one you sent from browser and one you sent from python API.
After registration on our service user is sent by email with confirmation link.
But when it is sent to Gmail or other mail services it usually drops to spam.
Here is the code:
def email_user(self, subject, message, from_email=None):
send_mail(subject, message, from_email, [self.email])
def activate_email(self, email=None):
if email: self.email = email
self.is_activated = False
self.activation_code = hashlib.sha256(str(self.email) + os.urandom(256)).hexdigest()[:32]
self.save()
subject = u'Welcome to the {0}!'.format(settings.SITE_NAME)
message = render_to_string('users/emails/activation.html', {'activation_code': self.activation_code, 'site_name': settings.SITE_NAME, 'site_domain': settings.SITE_DOMAIN})
self.email_user(subject, message, settings.SITE_EMAIL)
How to add DKIM or other license to this email in order to make Google trust to our server?
We're using Zimbra mail server on our site domain.
P.S. I found this snippet: https://djangosnippets.org/snippets/1995/
Is it suitable somehow in my case or not?
Thank you!
How your mail is treated depends first and foremost on the configuration of the email server which sends the messages generated by your application, and the DNS records associated with it.
Google's guidelines for bulk senders are a great place to start. Check that your mail server (and the emails themselves) comply with the rules.
DKIM is one of these guidelines, so yes: adding DKIM signatures will help. A few other points in the guide:
"Use the same address in the 'From:' header on every bulk mail you send." If you used different From headers while testing or something, this could be the problem.
Publish an SPF record.
Publish a DMARC policy.
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.