I have recently started using emacs with org mode and mu4e. I have set up two mail accounts in mu4e - work and personal (gmail and zohomail, respectively). Receiving mail is working fine. But I am having trouble with sending email - no matter what I do, mu4e (or the smtp service?) only sends mail through the account configured as default in .msmtprc. I am not versed with elisp, so I have basically copy-pasted and tried two different solutions that are supposed to help mu4e in routing an email through the correct account. Haven't been able to get either of these to work. Here are the relevant sections from .emacs and .msmtprc:


;; for sending mails
(require 'smtpmail) 
;; list of your email adresses:
(setq mu4e-user-mail-address-list '("personal_email_ID"

(require 'mu4e-context)
(setq mu4e-contexts
          :name "zohomail"
          (lambda () (mu4e-message "Enter Personal mail context"))
          (lambda () (mu4e-message "Leave Personal mail  context"))
          (lambda (msg)
            (when msg
              (mu4e-message-contact-field-matches msg
                                                  :to "personal_email_ID")))
          :vars '((user-mail-address . "personal_email_ID")
                  (user-full-name . "user name")
                  (mu4e-drafts-folder . "/zohomail/Drafts")
                  (mu4e-refile-folder . "/zohomail/Archive")
                  (mu4e-sent-folder . "/zohomail/Sent Mail")
                  (mu4e-trash-folder . "/zohomail/Trash")))

          :name "work_mail"
          (lambda () (mu4e-message "Enter WorkMail context"))
          (lambda () (mu4e-message "Leave WorkMail context"))
          (lambda (msg)
            (when msg
              (mu4e-message-contact-field-matches msg
                                                  :to "work_email_ID")))
          :vars '((user-mail-address . "work_email_ID")
                  (user-full-name . "work_user_name")
                  (mu4e-drafts-folder . "/work_mail/Drafts")
                  (mu4e-refile-folder . "/work_mail/Archive")
                  (mu4e-sent-folder . "/work_mail/Sent")
                  (mu4e-trash-folder . "/work_mail/Trash")))))

;;(setq mu4e-context-policy 'pick-first) ;; start with the first (default) context;
;;(setq mu4e-compose-context-policy 'ask) ;; ask for context if no context matches;

;; don't save message to Sent Messages, Gmail/IMAP takes care of this
;; (setq mu4e-sent-messages-behavior 'delete)


;; send function:
(setq send-mail-function 'sendmail-send-it
      message-send-mail-function 'sendmail-send-it)

;; send program:
;; this is exeranal. remember we installed it before.
(setq sendmail-program (executable-find "msmtp"))

;; select the right sender email from the context.
(setq message-sendmail-envelope-from 'header)

(defvar my-mu4e-account-alist
     (mu4e-sent-folder "/zohomail/Saved Items")
     (mu4e-drafts-folder "/zohomail/Drafts")
     (user-mail-address "home_email_ID")
     (smtpmail-default-smtp-server "smtp.zoho.in")
     (smtpmail-smtp-user "personal_email_ID")
     (smtpmail-smtp-server "smtp.zoho.in")
     (smtpmail-stream-type starttls)
     (smtpmail-smtp-service 465))
     (mu4e-sent-folder "/work_mail/Saved Items")
     (mu4e-drafts-folder "/work_mail/Drafts")
     (user-mail-address "work_email_ID")
     (smtpmail-default-smtp-server "smtp.gmail.com")
     (smtpmail-smtp-user "work_email_ID")
     (smtpmail-smtp-server "smtp.gmail.com")
     (smtpmail-stream-type starttls)
     (smtpmail-smtp-service 587))))

(defun my-mu4e-set-account ()
  "Set the account for composing a message."
  (let* ((account
          (if mu4e-compose-parent-message
              (let ((maildir (mu4e-message-field mu4e-compose-parent-message :maildir)))
                (string-match "/\\(.*?\\)/" maildir)
                (match-string 1 maildir))
            (completing-read (format "Compose with account: (%s) "
                                     (mapconcat #'(lambda (var) (car var))
                                                my-mu4e-account-alist "/"))
                             (mapcar #'(lambda (var) (car var)) my-mu4e-account-alist)
                             nil t nil nil (caar my-mu4e-account-alist))))
         (account-vars (cdr (assoc account my-mu4e-account-alist))))
    (if account-vars
        (mapc #'(lambda (var)
                  (set (car var) (cadr var)))
      (error "No email account found"))))

(add-hook 'mu4e-compose-pre-hook 'my-mu4e-set-account)

(setq message-sendmail-extra-arguments '(list "-a" account))

;;mu4e address completion
(add-hook 'mu4e-compose-mode-hook 'company-mode)

This is the .msmtprc:


# Set default values for all the accounts.
logfile ~/.maildir/msmtp.log
tls_trust_file ~/.maildir/certificates/root-certificates.pem

account zohomail
auth on
host smtp.zoho.in
port 465
protocol smtp
from personal_email_ID
user personal_email_ID
passwordeval security find-generic-password....
tls_starttls off

account work_mail
auth on
host smtp.gmail.com
port 465
protocol smtp
from work_email_ID
user work_email_ID
passwordeval security find-generic-password...
tls on
tls_starttls off

account default : work_mail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.