After understanding MIME and boundaries in email, the only problem that was not totally apparent was that everything needs to end with \r\n (carriage return/line feed).