Sending an email, via python, from one outlook account to another - python

I'm writing a program to help the school's Biology Department and it involves sending an email. I've gathered all of the variables I need for the content of the message, it's just the actual sending of the email that's causing some issues.
I've looked around and realized that I was using code that sent to gmail, whereas the servers at school use Outlook 2010. Once I remembered that, I looked around for some python code that sent emails, but so far nothing has worked.
It all seems very complicated for just sending an email, but I need some help as to were to go from here.
gsal.org.uk is our school's web server address, which is an outlook server.
The current error that I am receiving is smtplib.SMTPException: STARTTLS extension not supported by server., but I keep receiving various errors with everything I try.
This is the code:
fromaddr = "test#gsal.org.uk"
toaddrs = "technicians#gsal.org.uk"
msg = "\r\n".join([
"From: user",
"To: user",
"Subject: Practical Request",
"",
"Test"
])
server = smtplib.SMTP_SSL('smtp.gmail.com:465')
server.ehlo()
server.starttls()
# server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
messagebox.showinfo("Success!", "Your request has been sent successfully.")
All I want to do is send an email from an email account (specified within the program) to an email account that will soon be set up at school called technicians#gsal.org.uk. Any help appreciated.
EDIT
Thought I'd add the fact that if I remove the starttls() line, I get this:
smtplib.SMTPSenderRefused: (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1 https://support.google.com/mail/answer/14257 hw1sm42144009wjb.6 - gsmtp', 'test#gsal.org.uk')
I've read that link, but it seems to be talking about gmail? I want to use outlook? I understand that I need to do authentication, but how?

In order to fight spam and protect resources, a normally configured SMTP server provides its services only to its owner (e.g. a company, organization or registered customers). Your school server probably accepts mail only if it is:
addressed to the school's own domain, or
coming from an authenticated person (e.g. a student, teacher), or
coming from a host with IP address belonging to the school's LAN (when allowed by the admin)
Further, a SMTP server communicates with other servers on TCP port 25 and with users submitting new mail (which is your case) on port 465 or 587. (Port 25 was used in the past for all mail, this is now deprecated)
Communication on port 465 is always encrypted (TLS). Communication on port 587 starts in plaintext, but with the STARTTLS command, the encryption is turned on. A successfull STARTTLS is usually required to allow authentication (login).
Why your progam does not work?
You are trying to start TLS on an TLS connection. It has been started. Either don't STARTTLS or change the port to 587.
You are using gmail's server to send a message not addressed to gmail. In this case you must login with a name and a password as a registered gmail user.
What should help? Contact the local admin for details about using the school's own server.

Related

Can receive but not send emails with aiosmtpd SMTP server

I have setup a local SMTP server with aiosmtpd where I received emails after setting the proper DNS records:
mail.mydomain.com A 1 minute 1.2.3.4
mydomain.com MX 1 minute 10 mail.mydomain.com.
Just running aiosmtpd in the command line is enough to receive emails:
python.exe -m smtpd -c DebuggingServer -n 0.0.0.0:25
The problem is now sending emails which for whatever reason seems not to work. I have set the SPF record this way:
mydomain.com TXT 1 minute "v=spf1 a mx include: mydomain.com ~all"
I have tried with this too:
mydomain.com TXT 1 minute "v=spf1 ip4:1.2.3.4 ~all"
I'm using this code to send the email while the SMTP server is running:
import smtplib
sender = 'from#mydomain.com'
receivers = ['to#mydomain.com']
message = """From: From Person <from#mydomain.com>
To: To Person <to#mydomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('127.0.0.1')
smtpObj.sendmail(sender, receivers, message)
print ("Successfully sent email")
except smtplib.SMTPException:
print ("Error: unable to send email")
I have tried to add as destination email my #gmail.com email address too but I don't get anything.
I can see that the email is sent to the SMTP server but I don't get the email back:
---------- MESSAGE FOLLOWS ----------
b'From: From Person <from#mydomain.com>'
b'To: To Person <to#mydomain.com>'
b'Subject: SMTP e-mail test'
b'X-Peer: 127.0.0.1'
b''
b'This is a test e-mail message.'
------------ END MESSAGE ------------
Any idea on what am I doing wrong?
Generally, email is sent on the internet from email clients connecting to mail transfer agents (MTAs) that relay emails to mail delivery agents (MDAs) that store the email for the recipient to read. That is, GMail operates both as an MTA for its users, relaying GMail users' emails to remote mail servers, and as an MDA that receives emails slated for delivery to its users' mailboxes. Emails are transferred between servers using the SMTP protocol, and aiosmtpd is an implementation of the server-end of the SMTP protocol, whereas the built-in smtplib module is an implementation of the client-end.
When you run aiosmtpd in its default configuration, it is simply a dumb SMTP server that receives emails and spits them out on the terminal without relaying them or storing them. That is to say, aiosmtpd by itself is not an MTA or an MDA, unless you write code that implements storage of email (e.g. using the 'mailbox' built-in module), or you write code that implements relaying of email.
When you use the SMTP client smtplib.SMTP() to connect to 127.0.0.1, you are simply connecting to aiosmtpd, which won't do anything interesting with the email that you give it (unless you add code that does something interesting).
If you want to send email to remote email providers, e.g. GMail, then you need to either submit the email to a fixed MTA, or you need to implement the relaying mechanism yourself using DNS lookups.
If all you need is a simple MTA to relay email to remote hosts, then you don't need aiosmtpd or any other kind of Python programming - postfix is an excellent choice. Further reading: aiosmtpd is not an MTA
If you want to implement the relaying mechanism yourself, then you need to look up the MX record for the recipient's address (e.g. gmail.com) and then instruct smtplib.SMTP to connect to the host specified in the MX record. However, I wouldn't recommend trying to do this, as there are many pitfalls and cases to consider. For example - what to do if there are multiple MX records or no MX records at all. Furthermore, the remote SMTP server might not respond, or it may respond with a 4xx or 5xx error code, in which case you need to take appropriate action and retry at a later time.

Python smtplib hangs when sendmessage() is called

I'm trying to send a message from a gmail account using smtplib. I've been messing with it in the shell, and everything works fine until I call sendmail(), when it just freezes until I close the terminal. No errors, just freezing. My commands look like this:
>>> import smtplib
>>> connection = smtplib.SMTP("smtp.gmail.com", 587)
>>> connection.ehlo()
>>> connection.starttls()
>>> connection.login("gmail_id", "gmail_pwd")
>>> connection.sendmail("addr_from", "addr_to", "message")
Has anybody had this problem? I've tried pinging smtp.gmail.com and using telnet smtp.gmail.com 587, and they both connect.
Use connection.set_debuglevel(1) to enable tracking/debugging SMTP session.
https://docs.python.org/2/library/smtplib.html#smtplib.SMTP.set_debuglevel
I had a similar issue just now, I had tried to login to my gmail account using python3.7's smtplib and was getting:
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials')
I went to my gmail inbox using a browser, saw that I received a security alert email with the message
Less secure app blocked: Google blocked the app you were trying to use
because it doesn't meet our security standards.Some apps and devices
use less secure sign-in technology, which makes your account more
vulnerable. You can turn off access for these apps, which we
recommend, or turn on access if you want to use them despite the
risks. Google will automatically turn this setting OFF if it's not
being used.
After I followed the instructions in the aforementioned email and enabled less secure apps access on my gmail account, I tried sendmail still using the initial SMTP connection, but it just hung with no output.
I then proceeded to quit the initial SMTP connection and created a new one (post less secure apps enabled) like below and it worked!
conn = smtplib.SMTP('smtp.gmail.com', 587)

How to create a free SMTP server

I am not at all familiar with SMTP but I am working on sending emails through Python code. I have the code but I need to pass SMTP host name for it to actually work. Is there any service which provides a free SMTP service that I leverage for testing out my code? I looked around to create my own SMTP server but couldn't find something that provides a step by step guide to create a SMTP server. I want to create a free server(or if there is any free service) that will provide me with a host name(ip address) so that I can put that host name in my python code and execute it from any machine.
If anyone can point me in the right direction it will be helpful.
import smtplib
username = 'user'
password = 'pwd'
from_addr = 'username#gmail.com'
to_addrs = 'username#gmail.com'
msg = "\r\n".join([
"From: username#gmail.com",
"To: username#gmail.com",
"Subject: subject",
"",
"message"
])
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username, password)
server.sendmail(from_addr, to_addrs, msg)
server.quit()
You can use mutt linux command also here.
See :
https://docs.python.org/3/library/smtplib.html
https://support.google.com/a/answer/176600?hl=en
You need service like https://mailtrap.io/. You'll get SMTP server address (eventually port number) that you point your application to. All e-mails produced by your application will be then intercepted by mailtrap (thus not delivered to the real To: address).
They offer free variant that seems to be suitable for your needs.

Python smtplib Tmobile is blocking

I am trying to send a message with python using the smtplib library.
Here is the code I am using:
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login(EMAIL, PASS)
server.sendmail(EMAIL, phone_number +"#"+ carrier_address, msg)
server.quit()
I am using the port 465 to use a secure connection when connecting, but this still gets the message blocked when sending to a tmobile phone number. It seems that any other carrier works.
Has anyone else run into this issue? My website depends on these texts, and they have to be on time, so this is a very bad bug.
Is there a workaround to this or any other library out there to make this work?
Assuming you are getting the 550 block message from T-Mobile, I found this discussion that goes over the problem. Ths gist is downwards, but here's the main quote:
Sender Policy Framework 550 status message is part of spam validation screening. "550" is part of a long list of SMTP status codes to help diagnose connection issues. Mail relays connecting to #tmomail.net DNS and MX (mail) records must match or be authorized on behalf of another domain. The mismatch can occur in the registered domain or the shared ISP network connection.
I don't know how to update DNS or MX records as I am just a humble pythoner trying to text some folks. If you worked through your error already, what worked for you? Thanks.

How i can send automatic email when i confirm a form request on Odoo 8?

i'm a new Odoo developer and i need to send automatic email when i confirm a form request, and i can input manually sender and receiver the email.
Any one have a sample, tutorial or anyone can help me, i dont know the steps or configure of mail server because i use localhost, Thank you
Go to setting-> Technical setting-> Email-> Outgoing Mail Servers
Set SMTP server, SMTP port other credentials
eg:
SMTP Server: smtp.gmail.com
SMTP port: 587
connection security: TLS(STARTTLS)
Once done, Test the connection is setup properly or not by clicking Test connection button.
You can send mail by calling send_mail()

Categories

Resources