LAS
LAS

Reputation: 179

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 = '[email protected]'

    # 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: '[email protected]'.
    #            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()

Upvotes: 2

Views: 3165

Answers (1)

Mike
Mike

Reputation: 790

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 = "[email protected]"
password = "mypassword"
to = ["[email protected]"]

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).

enter image description here

  • You'll see something like the following:

enter image description here

  • Scroll down, looking for the following image and click on it (Configure Mail...).

enter image description here

  • Look for the info at mail server section.

enter image description here

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.

Upvotes: 4

Related Questions