I have a python function to send an email notification. I have included the login call to authenticate with my local SMTP server, however emails are being returned stating '553-mail rejected because your IP is in the PBL'. Further reading on https://www.spamhaus.org/pbl reveals that apparently I am not being prevented from sending email, I am simply required to authenticate first.
I have tried base64 encoding to avoid sending the password as plain text without success.
I have also looked at the Pysasl library, but I am a little unsure how I might use this for authenticating with my SMTP server.
Would anyone have any guidance as to the correct use of either base64 encoding, the pysasl library or if a better method exists for satisfying the correct authentication requirements?
My code is below.
def emailNotify(self,userid):
SMTPserver = 'localhost'
sender = '***' # blanked
username = '***' # blanked
receiver = '***' # blanked
pwd = '***' # blanked
text_subtype = 'plain'
msg = 'requested data was posted to dashboard for '+userid
subject = 'confirmation of POST\'d data for '+userid
try:
msg = MIMEText(msg, text_subtype)
msg['Subject'] = subject
msg['From'] = sender
conn = smtplib.SMTP(SMTPserver)
conn.login(username, pwd)
try:
conn.sendmail(sender, receiver, msg.as_string())
finally:
conn.quit()
except:
print('message sending failed')
Thanks in advance.
You have 2 ways of building a program for sending mail:
build a bullet proof solution:
analyze what server announces as its capabilities
choose the one that better meets your requirement
use that authentication method
actually send the mail
simply that means that you have to implement code for reading and decoding what the server returns (which can be weird) and also all various authentication methods, not speaking of TLS
use a custom connection method adapted to that server
read the documentation of the server to know what it declares to support
test it manually first through a mere telnet connection then in an interactive Python session (idle is enough here, but you can choose your prefered Python shell)
carefully program the way you have just tested - but leave relevant error messages in cases the server capabilities change...
IMHO much simpler...
Related
I am trying to send email using python. My code was working fine before Google disabled 'less secure apps'. My email address and password are both correct.
server = smtplib.SMTP_SSL("smtp.gmail.com", 465)
serverEmail = "EMAILADDRESS"
serverPw = "QWERTY"
server.login(serverEmail, serverPw)
subject = "Rejection"
body = "Hi! You've been unfortunately declined access to our system."
message = f'Subject: {subject}\n\n{body}'
server.sendmail("EMAILADDRESS", doctorEmail['email'], message)
server.quit()
I get this error now:
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted.
I get this error when i use server.starttls():
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.
2-step verification turn on then head over to App password
After is generated passwords
import smtplib
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as connection:
email_address = 'your_email_sender#gmail.com'
email_password = 'App_Passwords_is_generated'
connection.login(email_address, email_password )
connection.sendmail(from_addr=email_address, to_addrs='receiver_email#something.com',
msg="subject:hi \n\n this is my message")
This is working for me. You need to generate an app password for this. See https://support.google.com/accounts/answer/185833?hl=en
import smtplib as smtp
connection = smtp.SMTP_SSL('smtp.gmail.com', 465)
email_addr = 'my_email#gmail.com'
email_passwd = 'app_password_generated_in_Google_Account_Settings'
connection.login(email_addr, email_passwd)
connection.sendmail(from_addr=email_addr, to_addrs='recipient#something.com', msg="Sent from my IDE. Hehe")
connection.close()
For some reason, all of my emails are ending up in SPAM folder of the recipient account though.
Google disabled access for 'less secure apps' for a time on June 2, but around 7 PM US Eastern Time they re-enabled it. So if you just waited a few hours, you didn't have to do anything.
I suspect that somehow they got smacked with the 'law of unintended consequences' but it won't surprise me if they turn this access off again at some point.
I have written a code in jython 2.5, which uses the smtplib sendmail to send mail over a smtp server. Below is a simple snippet of the code
mail = MIMEMultipart('mixed')
mail['Subject'] = mail_subject
mail['FROM']=UstrSender
mail['To']=UstrReceivers
mail['Cc']=UstrCC
mail_p2=MIMEText(mail_html, 'html', 'utf-8')
mail.attach(mail_p2)
#Connection to SMTP
#Enter SMTP Server Details, In case your server do require authentication modify authentication parameter below and uncomment
s = smtplib.SMTP(smtpserver)
#s.sendmail(UstrSender, [UstrReceivers, UstrCC], mail.as_string())
sendmail_return = s.sendmail(UstrSender, [UstrReceivers, UstrCC], mail.as_string())
Now, the smtp server mentioned is a cluster of 3 individual servers and there is a lag at times on one of these servers because of a long queue of requests. To identify such issues and the culprit server, need to have a generic script to identify the queue id of the message being sent. Kindly, help on the same, if using docmd we can create such a command to get the queue id of the submitted email.
I tried getting a response from sendmail itself, but since the mail is eventually sent, there is no return from the command.
Thanks,
Dev
I am currently trying to send emails via smtplib with the following code:
import smtplib
import email.utils
from email.mime.text import MIMEText
smtpserver = smtplib.SMTP("smtp.gmail.com",587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login('username#gmail.com', 'pwd')
msg['Subject'] = "subject line"
msg['From'] = 'newusername#gmail.com'
msg['To'] = 'friend#gmail.com'
smtpserver.sendmail(sender, recipients, msg.as_string())
When I do something like this, instead of the recipient getting an email from newusername#gmail.com, they get it from username#gmail.com, which was the email I used for authentication.
Is there a way to change this?
This is an intentional security feature in gmail, and other public mail servers, called SMTP AUTH.
Without this feature, anyone with a gmail address could send mail impersonating anyone else with a gmail address. I could send a message claiming to be from you, and the recipient would have no way of knowing it wasn't from you, and you'd have no way to prove it wasn't from you.
But it wouldn't matter anyway, because spammers would be sending so much more email with your address than you do that email addresses would be effectively meaningless, and the whole email system would fall apart. Which is what almost happened in the late 90s. Only a concerted campaign to require SMTP AUTH on all open submission servers, including blacklisting all mail from servers that didn't comply (even the ones that used POP-before-SMTP, IMAP-before-SMTP, IP/MAC verification, or other alternatives to SMTP AUTH) managed to keep the spammers from ruining everything.
Later, another security/anti-spam measure, called DKIM, was added, which gmail also uses: most servers will throw out any messages that isn't signed by the originating server, indicating that the server trusts that the message came from who it says it came from. Obviously, gmail isn't going to certify that a message came from newusername when it actually came from username.1 And, if they did, people who administer other servers would just blacklist gmail signatures are meaningless.
1. Unless they have some reason to trust that username has the right to send mail as newusername—corporate mail servers sometimes do have a feature like that, allowing you to configure things so, e.g., a secretary can send mail from his boss's address without having his boss's password.
I'm trying to send a message from localhost:1025. I'm running an SMTP debug server using this command python -m smtpd -n localhost:1025.
Here is the code used for sending the mail:
msg = mailer.Message(From='noreply#'+company['host'],
To=req['mail'],
Subject='E-mail confirmation',
Body=Template(open('./confirmation.html').read()).render(company=company, account=account, accode=accode))
mailer.Mailer(company['host'], port=1025).send(msg)
The req['mail'] contains my email address, when I checked my email inbox and spam folders, I didn't find any message - What's, supposedly, caused this problem?
As is made quite clear in the documentation the debugging server does not attempt to deliver email messages. This is to allow testing and verification of the email's content without actually sending any mail.
I made a simple keylogger in C++. I want to send an email with the input the keylogger made using Python. I know you can do it with C++ but I want to use Python.
You I hope that your keylogger is for educational purposes only. Anyway,
here is a simple way to send one e-mail using Python script.
#!/usr/bin/python
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"
Here, you have placed a basic e-mail in message, using a triple quote, taking care to format the headers correctly. An e-mail requires a From, To, and Subject header, separated from the body of the e-mail with a blank line.
To send the mail you use smtpObj to connect to the SMTP server on the local machine and then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these aren't always used to route mail).
If you're not running an SMTP server on your local machine, you can use smtplib client to communicate with a remote SMTP server. Unless you're using a webmail service (such as Hotmail or Yahoo! Mail), your e-mail provider will have provided you with outgoing mail server details that you can supply them, as follows:
smtplib.SMTP('mail.your-domain.com', 25)
Source: http://www.tutorialspoint.com/python/python_sending_email.htm