Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set host address in python smtplib module?

In linux environment, I want to connect to our company's mail server and send anonymous emails, it's host address in 10.18.93.128, and port is 25.

I don't know where to insert those information in code below, here gmail's server is used, but I want to use our mail server:

import smtplib  
fromaddr = '[email protected]'  
toaddrs  = '[email protected]'  
msg = 'Hello'  


# Credentials (if needed)  
username = 'yyyyy'  
password = 'xxxxx'  

# The actual mail send  
server = smtplib.SMTP('smtp.gmail.com:587')  
server.starttls()  
server.login(username,password)  
server.sendmail(fromaddr, toaddrs, msg)  
server.quit()  

My questions:

  1. Where do I enter our mail server's host adress 10.18.93.128 and port above?
  2. Is it possible to send email using a non-existing email account? It means I will not log in to an account, just send anonymous emails.

Thanks Best Regards

like image 365
alwbtc Avatar asked Jan 26 '26 01:01

alwbtc


1 Answers

Just replace the smtp.gmail.com:587 part:

server = smtplib.SMTP('10.18.93.128:25')

You may have to omit the starttls() call depending on the configuration of your company email server.

It also depends on your email server whether or not it'll allow sending without logging in. The SMTP standard does not demand you log in, but company policy may.

Note that just because you have to log in to the mail server, you may still be able to send email using whatever from address you choose. Enforcing limitations on the from address is another policy decision a mail server can make.

like image 59
Martijn Pieters Avatar answered Jan 27 '26 13:01

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!