I am using a university server that runs linux (Ubuntu 16.04). I am trying to use this server to send email messages (using python, code below), but apparently outgoing SMTP connections are blocked (I say this because the code works on my mac, it only hangs on the server). Is there any way to unblock them?

I asked my system administrator, of course, but I figured that there may be something I can do in the meanwhile. I already had to add exports in order to make the server connect to the internet; in particular I added this to .bash_profile :

export https_proxy='...'
export http_proxy='...'

It is likely that I have to something like that to make it work? I read on a webpage (from the university) that I need to have " SMTP Authentification (SMTP AUTH) enabled" in order to be able to send emails. But looking online did not bring up anything useful. What does it mean to be SMTP AUTH enabled? Does it mean to connect using a secure connection when connecting to the email server?

This is the python code I use to send emails:

server = smtplib.SMTP_SSL(smtp_ssl_host, smtp_ssl_port) <--- this is where is hangs
server.login(username, password)
server.sendmail(username, targets, msg.as_string())

As a note, I am using my yandex account to send these emails

  • This question is too broad. You really should split it up into two or three questions. For example: #1 How does one send email when port 25 is blocked? #2 What is SMTP Authentication and how do I set it up? Also anything Python specific should probably be left off, perhaps asked on a programming site. (TBH, there should be plenty of information online already about most of this....they are common questions.) – B Layer Sep 3 '17 at 8:17
  • @BLayer Thank you for your comment; I haven't done so because I don't know whether those were different issues or not. I was not able to find any info though! Searching for "What is SMTP Authentication and how do I set it up?" and similar returns several results on how to turn it on on outlook, apple mail, etc. but this is not what I want. Same thing for the block on port 25 (actually in my case 465). In any case I will do some more searching and then eventually ask separate questions :) If you have pointers on where to look, it'll be appreciated :) – Ant Sep 3 '17 at 8:26
  • Sure. I'll see if I have any good links handy and post them a little later. – B Layer Sep 3 '17 at 8:27
  • BTW, you can't send email unless you have a SMTP relay (an "an outbound email server") to send to. Unless you have that there's little point in researching the rest. Do you have this? E.g. for home email your ISP provides a hostname and port that you configure in your email client. That's the SMTP relay. What are you going to use? – B Layer Sep 3 '17 at 8:32
  • @BLayer I use my yandex account. I connect to their server, login, and send the email. The code works perfectly on my mac; it only hangs when I use it on the remote server :) – Ant Sep 3 '17 at 8:35

As I mention in my comment there are really several questions being asked. I will talk here a little bit about the port 25 issue.

Port 25 is the standard unencrypted SMTP port and it is quite common for system administrators to configure an organization's firewall to block outbound traffic on that port. This is done for two reasons primarily:

  1. To prevent malware, rogue software, spammers, etc. from sending email for nefarious purposes.
  2. To prevent legitimate users from sending email over an unencrypted channel.

The second one presumes that email sent over an encrypted connection (e.g. using SSL/TLS) is allowed by administrators for authorized users. The standard port for secure outbound SMTP is 587. If in your case this is allowed then that solves your problem. Establish an encrypted connection to your SMTP relay server over port 587. You will have to figure out how to do this in your code (or ask how separately/somewhere else).

Unfortunately, #1 often supersedes #2 and even secured email communications are not allowed by admins. If so, check whether your email provider (whoever mantains the SMTP relay) accepts traffic on alternate ports. For instance, some will give the option of using port 10025. Alternatively, there are third party email providers that can forward emails. That is they accept email on port 10025 and simply forward it to your primary provider's relay on port 25. Here's one example, first one my search turned up: "Alternate-port SMTP" service. This one has a fee. You may be able to find someone offering this for free (for low-volume, at least).

Or perhaps your administrator allows special exceptions to the policy. For instance, if you work for a company and demonstrate that the access is critical for the business they may allow the traffic on 25/587 for a limited number of originating hosts and/or for a specific SMTP relay address.

If you're still not successful then your next best bet is probably SSH tunnelling. You will need access to some machine outside of your firewall that runs an SSH server (e.g. sshd). Then you can set up a special kind of connection that listens on one port and forwards outbound to another port on another host somewhere. I won't go into the details of how to do this because there are already many descriptions online.

Update: I should add that if you use an alternate port or set up an SSH tunnel without discussing with your admin and they find out later there's a chance they may not look too kindly on it. If they're not happy you can kiss that option goodbye. On the other hand if you discuss it with them ahead of time they might forbid it outright. You'll have to weigh your options. As long as what you do does not negatively impact security a reasonable admin would be okay with it. They're not all reasonable, though. :)

| improve this answer | |
  • very informative, thank you. I will work on what you said! :) – Ant Sep 3 '17 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.