title: Gmail TLS hanshake failure in haskell
body: <p>I am trying to write a little script to send an email via gmail.<br>
  I can connect to gmail but when I try to hanshake it fails.<br>
  Any pointers to get the handshake a go?</p>
  
  <p>Here is the code:</p>
  
  <pre><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 -&gt; IO ()
  tListen ctx =
      forever $ recvData ctx &gt;&gt;= B.putStrLn
  
  cWrite :: Handle -&gt; String -&gt; IO ()
  cWrite h s  = do
      hPrintf h "%s\r\n" s
      printf    "&gt; %s\n" s
  
  cListen :: Handle -&gt; IO () 
  cListen h =
      forever $ hGetLine h &gt;&gt;= putStrLn
  
  emailGmail2 = do
      let
          host = "smtp.gmail.com"
          port = 587
          params = defaultParamsClient
      g &lt;- newGenIO :: IO SystemRandom
      h &lt;- connectTo host (PortNumber (fromIntegral port))
      hSetBuffering h LineBuffering
      cWrite h "EHLO"
      cWrite h "STARTTLS"
      --cListen h
      con &lt;- contextNewOnHandle h params g
      handshake con
      tListen con
  </code></pre>
  
  <p>And here's the error:  </p>
  
  <blockquote>
    <p>HandshakeFailed (Error_Packet_Parsing "Failed reading: invalid header
    type: 50\nFrom:\theader\n\n")</p>
  </blockquote>