You have taken the brave first step of deciding to write this letter. You may feel that you have come to your senses after a period of bad behavior, where you have hurt your spouse. People feel compelled to write a letter like this for a variety of reasons. You may need to expunge your guilt or want to gain the forgiveness of your spouse. A strong motivator may be your wish to reconcile with your partner and save your marriage. Even if this outcome is unlikely, or even impossible, writing this letter may still feel like the right thing to do.
Be honest with yourself about the purpose of writing this letter. Ask yourself if you have another agenda, or want to make yourself feel better, or genuinely want to take responsibility for the problems in your marriage. Begin your letter by stating the reason for writing. You could decide to say something like, "I have thought about our situation and want to write to say how sorry I am".
Explain honestly why you behaved in the way you did. Avoid inadvertently blaming the other person for your actions. It is very human to try to justify your behavior, but this is not the reason you are writing this letter. Give a couple of examples of your selfish actions, expressing your sincere regret. However, do not become embroiled in past battles, as this is not constructive.
Express what made you begin to see the error of your ways. This may be, for instance, when you realized what you stood to lose. If you wish to have a reconciliation with your spouse, express this clearly. Promise that you will not repeat the behavior, but only do this if you are sure you can keep your promise. State that you accept that if you hurt your spouse again, you will have thrown away your last chance. If reconciliation is out of the question, say that you wish to be there for the other person in times of need.