Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:14:13+00:00 2026-06-15T02:14:13+00:00

I am trying to write a little script to send an email via gmail.

  • 0

I am trying to write a little script to send an email via gmail.
I can connect to gmail but when I try to hanshake it fails.
Any pointers to get the handshake a go?

Here is the code:

import Network.Socket
import Network
import Network.TLS
import Network.TLS.Extra

import Crypto.Random
import Data.CertificateStore -- to remove
import System.Certificate.X509 --to use I think

import System.IO
import Text.Printf

import Control.Monad (forever)
import qualified Data.ByteString.Char8 as B


main :: IO ()
main = emailGmail2

tListen :: Context -> IO ()
tListen ctx =
    forever $ recvData ctx >>= B.putStrLn

cWrite :: Handle -> String -> IO ()
cWrite h s  = do
    hPrintf h "%s\r\n" s
    printf    "> %s\n" s

cListen :: Handle -> IO () 
cListen h =
    forever $ hGetLine h >>= putStrLn

emailGmail2 = do
    let
        host = "smtp.gmail.com"
        port = 587
        params = defaultParamsClient
    g <- newGenIO :: IO SystemRandom
    h <- connectTo host (PortNumber (fromIntegral port))
    hSetBuffering h LineBuffering
    cWrite h "EHLO"
    cWrite h "STARTTLS"
    --cListen h
    con <- contextNewOnHandle h params g
    handshake con
    tListen con

And here’s the error:

HandshakeFailed (Error_Packet_Parsing “Failed reading: invalid header
type: 50\nFrom:\theader\n\n”)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:14:14+00:00Added an answer on June 15, 2026 at 2:14 am

    This error is partially to do with handling the SMTP protocol. Looking at the RFC for Secure SMTP over TLS RFC 2487 there is an example client-server dialogue.

    S: <waits for connection on TCP port 25>
    C: <opens connection>
    S: 220 mail.imc.org SMTP service ready
    C: EHLO mail.ietf.org
    S: 250-mail.imc.org offers a warm hug of welcome
    S: 250 STARTTLS
    C: STARTTLS
    S: 220 Go ahead
    C: <starts TLS negotiation>
    C & S: <negotiate a TLS session>
    C & S: <check result of negotiation>
    C: <continues by sending an SMTP command>
    . . .
    

    In your code you are sending EHLO and STARTTLS, and then immediately beginning the handshake negotiation. I believe what is happening is that the server is still sending some of the 250 and 220 codes above and the TLS library is trying to interpret these as TLS messages which is causing the problem.

    Indeed if I open another terminal and listen to port 587 with netcat and change the program to connect to localhost I get the same error if I reply with "250 STARTTLS"

    Changing this got the program to work for me:

    import Network.Socket
    import Network
    import Network.TLS
    import Network.TLS.Extra
    
    import Crypto.Random
    import qualified Crypto.Random.AESCtr as RNG
    import System.IO
    import Text.Printf
    
    import Control.Monad (when)
    import Data.List (isPrefixOf)
    
    ciphers :: [Cipher]
    ciphers =
            [ cipher_AES128_SHA1
            , cipher_AES256_SHA1
            , cipher_RC4_128_MD5
            , cipher_RC4_128_SHA1
            ]
    
    main :: IO ()
    main = emailGmail2
    
    cWrite :: Handle -> String -> IO ()
    cWrite h s  = do
        hPrintf h "%s\r\n" s
        printf    "> %s\n" s
    
    cWaitFor :: Handle -> String -> IO ()
    cWaitFor h str = do
        ln <- hGetLine h
        putStrLn ln
        when (not $ str `isPrefixOf` ln) (cWaitFor h str)
    
    emailGmail2 = do
        let
            host = "smtp.gmail.com"
            port = 587
            params = defaultParamsClient{pCiphers = ciphers}
        g <- RNG.makeSystem
        h <- connectTo host (PortNumber (fromIntegral port))
        hSetBuffering h LineBuffering
        cWrite h "EHLO"
        cWaitFor h "250-STARTTLS"
        cWrite h "STARTTLS"
        cWaitFor h "220"
        con <- contextNewOnHandle h params g
        handshake con
        bye con
    

    Also note that I needed to add some ciphers from Network.TLS.Extras as otherwise I got an Illegal Parameter error. I found the code for this and to add logging from Vincent’s tests on the Github page.

    Another note: In case you run into more issues I should point out that I used the command line programs gnutls-cli and ssldump to debug the issue with ciphers mentioned above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a little Emacs Lisp script that reads a csv file
I am currently trying to write a little powershell script (I have no experience
I am trying to write a little script myself to compute all of the
I'm trying to write a little greasemonkey script/bookmarklet/what have you for Google Docs. The
So I am trying to write a little script to crop a users image.
I'm trying to write a little build script, and want to determine if the
i´m trying to write a little script for writting a sqlite table from an
I'm using Mac OS X and I'm trying to write a little script that
I'm trying to write a little script to clean my directories. In fact I
I'm just learning jQuery, and I'm trying to write a little script. To sum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.