Emails which are sent by python script drop in Spam on GMail - python

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.

Related

exchangelib: Sending an email as a distribution list

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.

Gmail API Oauth2 Send from different address

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.

Python smtplib: Unable to send sms messages to phone

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.

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)

Google Appengine sending emails: [Error] unauthorized sender

I'm trying to use google app engine's mail service on my site. It's showing some error whenever I visit the page that sends the email. The error says that I am using an unauthorized sender for the message. Here's the code that sends the email:
mail.send_mail(sender="myapp#appspot.gserviceaccount.com",
to=input_dict["email"],
subject="Mondays user activation",
body=content)
When I try out the site locally (using dev_appserver.py) it doesn't show the error, but it doesn't send the email (Note: I have to add the --enable_sendmail option when I try it locally). The error only shows up when I publish the site.
Does anybody know what I'm doing wrong? Thanks in advance for your help!
What is myapp#appspot.gserviceaccount.com? You might not be able to send mail from that address.
App Engine applications can send email messages on behalf of the app's
administrators, and on behalf of users with Google Accounts.
The email address of the sender, the From address. The sender address
must be one of the following types:
The address of a registered administrator for the application. You can add administrators to an application using the Administration
Console.
The address of the user for the current request signed in with a Google Account. You can determine the current user's email address
with the Users API. The user's account must be a Gmail account, or be
on a domain managed by Google Apps.
Any valid email receiving address for the app (such as xxx#APP-ID.appspotmail.com).
Any valid email receiving address of a domain account, such as support#example.com. Domain accounts are accounts outside of the
Google domain with email addresses that do not end in #gmail.com or
#APP-ID.appspotmail.com.
https://developers.google.com/appengine/docs/python/mail/sendingmail
First follow these steps
https://cloud.google.com/appengine/docs/python/mail/#who_can_send_mail
Then you need to manually add the sender email in cloud console
How to add an authorized sender
You may also have to add the email address you which to send the email from to the App Engine application settings Email API authorized senders.
See https://cloud.google.com/appengine/docs/python/mail/#Python_Sending_mail
Add the unauthorized email address as an administrator here:
https://console.developers.google.com/project/_/permissions/projectpermissions

Categories

Resources