Python how to find my smtp port no and host - python

I am trying to use the smtplib package in python to send mail
What should be the host and porr no that I need to specify to the SMTP constructor

This is my working example, I have used this in production.
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login("Sender's email id", "Sender's password")
header = 'To:' + <RECEIVERS EMAIL ID> + '\n' + 'From: ' + <SENDERS'S EMAIL ID> + '\n' + 'Subject:<SUBJECT>'
message = <MESSAGE>
smtpserver.sendmail(SENDER, RECEIVER, message)
smtpserver.close()

Probably the default smtp.gmail.com with port 587.
Please, try yagmail. Disclaimer: I'm the maintainer, but I feel like it can help everyone out!
It really provides a lot of defaults: I'm quite sure you'll be able to send an email directly with:
import yagmail
yag = yagmail.SMTP(username, password)
yag.send(to_addrs, contents = msg)
You'll have to install yagmail first with either:
pip install yagmail # python 2
pip3 install yagmail # python 3
Once you will want to also embed html/images or add attachments, you'll really love the package!
It will also make it a lot safer by preventing you from having to have your password in the code.

Related

Sending mail with through python with roundcube

Am trying to send mail with python using my rouncube webmail but when i run it outputs this error TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I checked in my mail and my port is "290" which have provided but when i run the function the mail doesn't send. I know how to send with gmail by turning on less secure app access but don't know how to work around to send with my webmail.
I need your suggestion to work around it.
from smtplib import SMTP_SSL as SMTP
import logging, logging.handlers, sys
from email.mime.text import MIMEText
def send_message():
text = '''
Hello,
Trying to send mail from my webmail in python 3.
Sincerely,
My name
'''
message = MIMEText(text, 'plain')
message['Subject'] = "Email Subject"
my_email = 'joe#mycompany.com'
# Email that you want to send a message
message['To'] = my_email
# You need to change here, depending on the email that you use.
# For example, Gmail and Yahoo have different smtp. You need to know what it is.
connection = SMTP('smtp.roundcube.com', 290)
connection.set_debuglevel(True)
# Attention: You can't put for example: 'your_address#email.com'.
# You need to put only the address. In this case, 'your_address'.
connection.login('fred.kings', 'fredsw321a')
try:
# sendemail(<from address>, <to address>, <message>)
connection.sendmail(my_email, my_email, message.as_string())
finally:
connection.close()
send_message()
I'm using:
Ubuntu 18.04.4 LTS.
Python 3.7.4
Jupyter-Notebook 6.0.1
Anaconda 1.7.2
I can successfully send an email through Python with Roundcube, executing the following script in a jupyter-notebook cell:
import smtplib
email = "myemail#mycompany.com"
password = "mypassword"
to = ["destination#dest.com"]
with smtplib.SMTP_SSL('mail.mycompany.com', 465) as smtp:
smtp.login(email, password)
subject = "testing mail sending"
body = "the mail itself"
msg = "Subject: {}\n\n{}".format(subject, body)
smtp.sendmail(email, to, msg)
I found this information as follows (so you can find yours):
Log into you company email.
Look for the following image and click on it (Webmail Home).
You'll see something like the following:
Scroll down, looking for the following image and click on it (Configure Mail...).
Look for the info at mail server section.
If there are using port 465 (such as in my case) it means that you want to use smtplib.SMTP_SSL instead of smtplib.SMTP.
NOTE: The previous worked for me from my personal computer, however, when I tried to use the same code, from my "virtual machine" at office, it does not work, which makes me think that there is a system limitation, and that the problem is not code-related.
Hope it helps, regards.

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 Name or service not known

i was making a simple daemon in python which takes a mail queue and delivers them to the recipients. Everything is working pretty good except from the smtplib which is actually the most important part.
What happens?
When im running the script im getting the following error:
root#vagrant-ubuntu-trusty-64:/mailer/tests# python daemon_run.py
[Errno -2] Name or service not known
From what i found on the internet this error occurs when it cant connect to the SMTP server. Most users suggested fixes on postman which i dont use since i take advantage of google's services.
The code
headers = "\r\n".join(["from: " + "my_email#gmail.com",
"subject: " + "Testing",
"to: " + "recipient#gmail.com",
"mime-version: 1.0",
"content-type: text/html"])
content = headers + "\r\n\r\n" + template_content
server = smtplib.SMTP('smtp.google.com', 587)
server.ehlo()
server.starttls()
server.login('my_email#gmail.com', 'pass')
server.sendmail('my_email#gmail.com', 'recipient#gmail.com', content)
server.close()
Please note that i'm using exactly the same login details in PHPMailer which actually works.
Any ideas?
It seems like the goold old typo hit again. Gmail's SMTP is smtp.gmail.com and not smtp.google.com

Python script should send me mail when there is any exception

I want my Python script to send me mail me when there is any exception. I have tried some code related to SMTP that I found, but unfortunately it's not executing and showing an error. Please help me to find out the main problem.
The SMTP code:
import smtplib
import string
SERVER = 'localhost'
SUBJECT = "Test email from Python"
TO = "abc.def#defghij.com"
FROM = "python#mydomain.com"
text = "Sample of mail"
BODY = string.join(("From: %s" % FROM,"To: %s" % TO,"Subject: %s" %SUBJECT ,"",text), "\r\n")
server = smtplib.SMTP(SERVER) #Here in this line it showing error
server.sendmail(FROM,TO,BODY)
server.quit()
** error message * server = smtplib.SMTP(SERVER)
socket.error: (10047, 'Address family not supported by protocol family: See http://wiki.python.org/jython/NewSocketModule#IPV6addresssupport')
I tested this code with Jython 2.5.3 and Python 2.7 and it works if I change SERVER from localhost to real SMTP server. I took SMTP server address from my email client configuration.
You can try this with yagmail, it will make it all much more convenient:
from yagmail import SMTP
SMTP(FROM).send(TO, SUBJECT, text)
It really requires only that much, and if you do some setup you're registered and safe (no need to write your username and password in script).
Read more about it at github.

python smtp gmail authentication error (sending email through gmail smtp server)

I have the following code
import smtplib
from email.mime.text import MIMEText
smtpserver = 'smtp.gmail.com'
AUTHREQUIRED = 1 # if you need to use SMTP AUTH set to 1
smtpuser = 'admin#myhost.com' # for SMTP AUTH, set SMTP username here
smtppass = '123456' # for SMTP AUTH, set SMTP password here
RECIPIENTS = ['online8#gmail.com']
SENDER = 'admin#myhost.com'
msg = MIMEText('dsdsdsdsds\n')
msg['Subject'] = 'The contents of iii'
msg['From'] = 'admin#myhost.com'
msg['To'] = ''online8#gmail.com''
mailServer = smtplib.SMTP('smtp.gmail.com',587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(smtpuser, smtppass)
mailServer.sendmail(smtpuser,RECIPIENTS,msg.as_string())
mailServer.close()
this code works fine on my desktop. but it failed with this error
smtplib.SMTPAuthenticationError: (535, '5.7.1 Username and Password not accepted. Learn more at\n5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 21sm4713429agd.11')
on my linux server.
Not sure what went wrong, should i open some port on my linux server?
Port 587 obviously needs to be open, but it probably is (or you wouldn't have gotten the detailed error msg in question). Python 2.5 vs 2.6 should make no difference. I think the issue has to do with "solving a captcha" once on the computer for which logins are currently getting rejected; follow the detailed instructions at the URL in the error message, i.e., http://mail.google.com/support/bin/answer.py?answer=14257
import random,time
for i in range(1,100):
y=random.randint(30,300)
time.sleep(y)
print ("Mailing for fun, Mail No: " + str(i))
msg = MIMEText('Testing mailing \n Mail No:' + str(i))
msg['Subject'] = 'Mail Number: ' + str(i)
Randomizing the mail interval to check smtp behavior :)
With a bit addition n modification, I got this to work to check our intermittent mail bouncing.

Categories

Resources