Fear clouds the mind -- in which Mail.app refuses to send mail via SSH tunnel and we learn about localhost, IPv6, debug Mail.app, and mostly anger and frustration
(If you're here looking for a solution to SSH tunneling in Mail.app, skip to the bottom.)
When I talk about fear and peace of mind and how the mind works, I often wish I had specific examples for illustration. Friday and today delivered that wish for me. When people talk about fear they usually mean something life-threatening. When I talk about fear I mean something not life-threatening, but nevertheless disruptive. Day-to-day, its non-life-threatening stuff behind our dysfunctional behavior. Changing how we deal with non-life-threatening fear can profoundly improve our lives.
On Friday, I was confronted with some kind of bug in Mail.app. I couldn't get it to send mail through an SSH tunnel. I spent the entire day troubleshooting. I left work without a solution and as angry as I can remember being in years. I was seriously considering giving up on software development altogether. No exaggeration. I was that angry. (As I type that I'm truly stunned. Some stupid little email problem and I'm looking for a career change? Man was my fear of failure out of control on Friday. Sheesh! At least it provides a useful example.)
I used to throw temper tantrums when I was that angry -- mostly punching or kicking inanimate objects, occasionally throwing one object at another. That is self-evidently self-destructive behavior. The stuff I was punching or kicking or throwing was usually my stuff. Either I was trying to destroy something I cared about or I was hurting my hand or foot trying to express my anger. 
I once played a lot of video games and I would get angry when I wasn't playing well. I used games to measure my self-worth and got really angry when I came up short. Some of the anger I had on Friday reminded me of the days when I would cuss, slap the controls, or punch the cabinet (or all three) after loosing or failing to get a perfect score or failing to set the high score. I always hated pinball machines for having a built-in penalty (tilt) for that kind of inappropriate behavior. I was really, really good at video games 'cos I played them a lot. But I also got really, really mad at myself when I didn't perform as well as I expected.
At my worst I'm completely intolerant of my own failure. And that's what happened on Friday. Mail.app was doing something stupid and I should have been able to fix it. When I couldn't fix it and when I couldn't make any progress against the problem, I just got more and more angry. As my anger increased, my ability to usefully investigate the problem diminished while my expectations increased. It became ever more important that I be able to prove myself and ever more difficult to actually pull that off. That negative cycle fed on itself until I was nearly ready to quit my job, quit my career. It's not a rational experience.
What I was experiencing was a simple case of inadequacy. I couldn't figure out how to fix Mail.app. That by itself is harmless. But my *fear of* inadequacy kicked in. My mind dutifully reminded me of all the various strategies I've used in the past when I've been inadequate and afraid at the same time. Most of those strategies are variations on the get-angry-at-myself theme with a few get-angry-and-break-something variations mixed in.
One of those strategies might have succeeded: try to find a different way to solve the problem. I had been investigating some resources on the net about using launchd to spontaneously create an ssh tunnel on demand. But it depends on having 'nc' (netcat) on the remote server. We don't have nc installed so I dug around for an RPM. I couldn't install it for various reasons. The technical details aren't important for this story.
When that failed it was just another failure. But I was still swimming in fear of failure and my mind, noticing the fear and the failure, dutifully reminded me of other things I could be afraid of: I wasn't billing any hours that way, haven't billed enough hours this month, if I don't bill enough hours I might have to pay some bills with credit cards. As I write this now all of those fears are pretty trivial but Friday they were completely gripping. I might as well have been standing between a mother bear and her cub. Well, not quite *that* afraid. I wasn't actually in mortal peril, but there was a definite sense of danger. Maybe more like driving too fast in an icy parking lot -- something that could wreck my car and cost thousands of dollars to fix, but not something that would necessarily kill me. It was threatening enough that I was seriously considering a career change by the end of the day.
Today was a sharp contrast. First, I changed my expectation. I probably won't fix Mail.app, but maybe I can get some more information about what's going wrong. Or maybe I can get enough information to get specific useful help from the Apple Genius Bar or a mailing list or whatever. I don't know what is going on, so the next step is to gather more information and ask for help. If I'm free from fear, it's entirely okay that I don't know what's going on. Friday, when I was feeling consumed by fear, the not-knowing part was a judgment against my competence as a developer.
I installed Thunderbird to confirm that it was an bug in Mail.app and not something wrong with my SSH tunnel. I had already confirmed this using telnet on Friday. The SSH tunnel was fine, but Mail.app couldn't use it for some opaque reason. But I wanted another example. In my experience, at least some of the local Geniuses don't know what to do with information when it comes from the command line. Thunderbird lets me show them that the competition works and their application doesn't. Hopefully that would motivate Apple to help me.
While I was waiting for Thunderbird to download I also googled for "mail.app debug". Did you know there's an AppleScript to turn on socket logging in Mail.app? It's here:
/Library/Scripts/Mail Scripts/Turn on Logging.scpt
And there's a wide array of other debugging options here:
What I found out is that Mail.app was in fact talking to the mail server but was introducing itself to the server with an IPv6 address. Without the tunnel Mail.app introduces itself with the IPv4 address. Here was the debug output, first with the SSH tunnel (the addresses below have been changed to protect the innocent ;-):
EHLO [^T???^Nf???p? ^V??IPv6:::1]
Second, without the tunnel:
And that's the moral of this story. In the absense of fear I can clearly see and even document that it's the computer that's stupid, not me. That lets me get on with my day (by writing a long blog post ;-)
Thunderbird had finished downloading and worked fine with the SSH tunnel. I thought about switching to Thunderbird. But while putting a few more finishing touches on this post, I googled for "mail.app localhost ip v6". Didn't find anything useful, but google asked if I meant "mail.app localhost ipv6?" So I tried that and found this helpful comment by someone named Ashley Clark on a blog entry describing exactly the same problem I was having:
Specifying localhost in Mail resolves to the IPv6 localhost address, when connecting to the mail server then the EHLO/HELO commands pass in the IPv6 localhost address....When you specify 127.0.0.1 in your settings then Mail passes that address instead and everything works.
That comment exactly matched the debug output I had just read and sure enough, my problem was fixed by configuring my outgoing mail server in Mail.app using 127.0.0.1 instead of localhost.
The important lesson is about how fear clouds the mind. I was so close to identifying the problem on Friday. One of the other suggested answers in the comments on the above blog called for commenting out a line in /etc/hosts. I made other modifications to /etc/hosts in my troubleshooting on Friday. I asked myself "what is that ::1 in there?" (the line to comment out). In my fear-clouded mind on Friday I didn't think of googling for "mail.app debug", even though I was exactly asking myself how to get some kind of useful information about how Mail.app was trying to talk to the SSH tunnel. By contrast, today without the fear clouding my mind, that was my first search. My solution to this stupid problem this morning was about 15 minutes of searching and reading (and about four hours of writing about it. ;-) compared to a whole day wasted in anger and frustration. Had I noticed the fear on Friday and released it, I might have actually had time to do some billable work. Speaking of which, I've got some programming to catch up on.
 Most people would never guess that about me. A few people might remember some tantrums -- people from junior high and high school, and maybe my suite mates from Freshman year of college. The tantrums gradually stopped as I became less self-destructive.