1 ;;; smtpmail.el --- simple SMTP protocol (RFC 821) for sending mail
3 ;; Copyright (C) 1995-1996, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Tomoji Kagatani <kagatani@rbc.ncl.omron.co.jp>
6 ;; Maintainer: Simon Josefsson <simon@josefsson.org>
7 ;; w32 Maintainer: Brian D. Carlstrom <bdc@ai.mit.edu>
8 ;; ESMTP support: Simon Leinen <simon@switch.ch>
9 ;; Hacked by Mike Taylor, 11th October 1999 to add support for
10 ;; automatically appending a domain to RCPT TO: addresses.
11 ;; AUTH=LOGIN support: Stephen Cranefield <scranefield@infoscience.otago.ac.nz>
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 ;; Send Mail to smtp host from smtpmail temp buffer.
33 ;; Please add these lines in your .emacs(_emacs) or use customize.
35 ;;(setq send-mail-function 'smtpmail-send-it) ; if you use `mail'
36 ;;(setq message-send-mail-function 'smtpmail-send-it) ; if you use message/Gnus
37 ;;(setq smtpmail-default-smtp-server "YOUR SMTP HOST")
38 ;;(setq smtpmail-local-domain "YOUR DOMAIN NAME")
39 ;;(setq smtpmail-sendto-domain "YOUR DOMAIN NAME")
40 ;;(setq smtpmail-debug-info t) ; only to debug problems
41 ;;(setq smtpmail-auth-credentials ; or use ~/.authinfo
42 ;; '(("YOUR SMTP HOST" 25 "username" "password")))
43 ;;(setq smtpmail-starttls-credentials
44 ;; '(("YOUR SMTP HOST" 25 "~/.my_smtp_tls.key" "~/.my_smtp_tls.cert")))
45 ;; Where the 25 equals the value of `smtpmail-smtp-service', it can be an
46 ;; integer or a string, just as long as they match (eq).
48 ;; To queue mail, set `smtpmail-queue-mail' to t and use
49 ;; `smtpmail-send-queued-mail' to send.
51 ;; Modified by Stephen Cranefield <scranefield@infoscience.otago.ac.nz>,
52 ;; 22/6/99, to support SMTP Authentication by the AUTH=LOGIN mechanism.
53 ;; See http://help.netscape.com/products/server/messaging/3x/info/smtpauth.html
54 ;; Rewritten by Simon Josefsson to use same credential variable as AUTH
57 ;; Modified by Simon Josefsson <jas@pdc.kth.se>, 22/2/99, to support SMTP
58 ;; Authentication by the AUTH mechanism.
59 ;; See http://www.ietf.org/rfc/rfc2554.txt
61 ;; Modified by Simon Josefsson <simon@josefsson.org>, 2000-10-07, to support
62 ;; STARTTLS. Requires external program
63 ;; ftp://ftp.opaopa.org/pub/elisp/starttls-*.tar.gz.
64 ;; See http://www.ietf.org/rfc/rfc2246.txt, http://www.ietf.org/rfc/rfc2487.txt
69 (autoload 'starttls-any-program-available
"starttls")
70 (autoload 'starttls-open-stream
"starttls")
71 (autoload 'starttls-negotiate
"starttls")
72 (autoload 'mail-strip-quoted-names
"mail-utils")
73 (autoload 'message-make-date
"message")
74 (autoload 'message-make-message-id
"message")
75 (autoload 'rfc2104-hash
"rfc2104")
76 (autoload 'netrc-parse
"netrc")
77 (autoload 'netrc-machine
"netrc")
78 (autoload 'netrc-get
"netrc")
79 (autoload 'password-read
"password-cache")
80 (autoload 'auth-source-search
"auth-source")
83 (defgroup smtpmail nil
84 "SMTP protocol for sending mail."
88 (defcustom smtpmail-default-smtp-server nil
89 "Specify default SMTP server.
90 This only has effect if you specify it before loading the smtpmail library."
91 :type
'(choice (const nil
) string
)
94 (defcustom smtpmail-smtp-server
95 (or (getenv "SMTPSERVER") smtpmail-default-smtp-server
)
96 "The name of the host running SMTP server."
97 :type
'(choice (const nil
) string
)
100 (defcustom smtpmail-smtp-service
25
101 "SMTP service port number.
102 The default value would be \"smtp\" or 25."
103 :type
'(choice (integer :tag
"Port") (string :tag
"Service"))
106 (defcustom smtpmail-local-domain nil
107 "Local domain name without a host name.
108 If the function `system-name' returns the full internet address,
109 don't define this value."
110 :type
'(choice (const nil
) string
)
113 (defcustom smtpmail-sendto-domain nil
114 "Local domain name without a host name.
115 This is appended (with an @-sign) to any specified recipients which do
116 not include an @-sign, so that each RCPT TO address is fully qualified.
117 \(Some configurations of sendmail require this.)
119 Don't bother to set this unless you have get an error like:
120 Sending failed; SMTP protocol error
121 when sending mail, and the *trace of SMTP session to <somewhere>*
122 buffer includes an exchange like:
124 501 <someone>: recipient address must contain a domain."
125 :type
'(choice (const nil
) string
)
128 (defcustom smtpmail-debug-info nil
129 "Whether to print info in buffer *trace of SMTP session to <somewhere>*.
130 See also `smtpmail-debug-verb' which determines if the SMTP protocol should
135 (defcustom smtpmail-debug-verb nil
136 "Whether this library sends the SMTP VERB command or not.
137 The commands enables verbose information from the SMTP server."
141 (defcustom smtpmail-code-conv-from nil
142 "Coding system for encoding outgoing mail.
143 Used for the value of `sendmail-coding-system' when
144 `select-message-coding-system' is called. "
148 (defcustom smtpmail-queue-mail nil
149 "Non-nil means mail is queued; otherwise it is sent immediately.
150 If queued, it is stored in the directory `smtpmail-queue-dir'
151 and sent with `smtpmail-send-queued-mail'."
155 (defcustom smtpmail-queue-dir
"~/Mail/queued-mail/"
156 "Directory where `smtpmail.el' stores queued mail."
160 (defcustom smtpmail-auth-credentials
"~/.authinfo"
161 "Specify username and password for servers, directly or via .netrc file.
162 This variable can either be a filename pointing to a file in netrc(5)
163 format, or list of four-element lists that contain, in order,
164 `servername' (a string), `port' (an integer), `user' (a string) and
165 `password' (a string, or nil to query the user when needed). If you
166 need to enter a `realm' too, add it to the user string, so that it
167 looks like `user@realm'."
169 (repeat (list (string :tag
"Server")
170 (integer :tag
"Port")
171 (string :tag
"Username")
172 (choice (const :tag
"Query when needed" nil
)
173 (string :tag
"Password")))))
177 (defcustom smtpmail-starttls-credentials
'(("" 25 "" ""))
178 "Specify STARTTLS keys and certificates for servers.
179 This is a list of four-element list with `servername' (a string),
180 `port' (an integer), `key' (a filename) and `certificate' (a
182 If you do not have a certificate/key pair, leave the `key' and
183 `certificate' fields as `nil'. A key/certificate pair is only
184 needed if you want to use X.509 client authenticated
186 :type
'(repeat (list (string :tag
"Server")
187 (integer :tag
"Port")
189 (file :tag
"Certificate")))
193 (defcustom smtpmail-warn-about-unknown-extensions nil
194 "If set, print warnings about unknown SMTP extensions.
195 This is mainly useful for development purposes, to learn about
196 new SMTP extensions that might be useful to support."
201 (defcustom smtpmail-queue-index-file
"index"
202 "File name of queued mail index.
203 This is relative to `smtpmail-queue-dir'."
207 ;; End of customizable variables.
210 (defvar smtpmail-address-buffer
)
211 (defvar smtpmail-recipient-address-list
)
213 (defvar smtpmail-queue-counter
0)
215 ;; Buffer-local variable.
216 (defvar smtpmail-read-point
)
218 (defconst smtpmail-auth-supported
'(cram-md5 plain login
)
219 "List of supported SMTP AUTH mechanisms.
220 The list is in preference order.")
222 (defvar smtpmail-mail-address nil
223 "Value to use for envelope-from address for mail from ambient buffer.")
226 (defun smtpmail-send-it ()
227 (let ((errbuf (if mail-interactive
228 (generate-new-buffer " smtpmail errors")
230 (tembuf (generate-new-buffer " smtpmail temp"))
231 (case-fold-search nil
)
233 (mailbuf (current-buffer))
234 ;; Examine this variable now, so that
235 ;; local binding in the mail buffer will take effect.
236 (smtpmail-mail-address
237 (or (and mail-specify-envelope-from
(mail-envelope-from))
239 (smtpmail-code-conv-from
240 (if enable-multibyte-characters
241 (let ((sendmail-coding-system smtpmail-code-conv-from
))
242 (select-message-coding-system)))))
244 (with-current-buffer tembuf
246 ;; Use the same `buffer-file-coding-system' as in the mail
247 ;; buffer, otherwise any `write-region' invocations (e.g., in
248 ;; mail-do-fcc below) will annoy with asking for a suitable
250 (set-buffer-file-coding-system smtpmail-code-conv-from nil t
)
251 (insert-buffer-substring mailbuf
)
252 (goto-char (point-max))
253 ;; require one newline at the end.
254 (or (= (preceding-char) ?
\n)
256 ;; Change header-delimiter to be what sendmail expects.
257 (mail-sendmail-undelimit-header)
258 (setq delimline
(point-marker))
259 ;; (sendmail-synch-aliases)
261 (expand-mail-aliases (point-min) delimline
))
262 (goto-char (point-min))
263 ;; ignore any blank lines in the header
264 (while (and (re-search-forward "\n\n\n*" delimline t
)
265 (< (point) delimline
))
266 (replace-match "\n"))
267 (let ((case-fold-search t
))
268 ;; We used to process Resent-... headers here,
269 ;; but it was not done properly, and the job
270 ;; is done correctly in `smtpmail-deduce-address-list'.
271 ;; Don't send out a blank subject line
272 (goto-char (point-min))
273 (if (re-search-forward "^Subject:\\([ \t]*\n\\)+\\b" delimline t
)
275 ;; This one matches a Subject just before the header delimiter.
276 (if (and (re-search-forward "^Subject:\\([ \t]*\n\\)+" delimline t
)
277 (= (match-end 0) delimline
))
279 ;; Put the "From:" field in unless for some odd reason
280 ;; they put one in themselves.
281 (goto-char (point-min))
282 (if (not (re-search-forward "^From:" delimline t
))
283 (let* ((login smtpmail-mail-address
)
284 (fullname (user-full-name)))
285 (cond ((eq mail-from-style
'angles
)
286 (insert "From: " fullname
)
287 (let ((fullname-start (+ (point-min) 6))
288 (fullname-end (point-marker)))
289 (goto-char fullname-start
)
290 ;; Look for a character that cannot appear unquoted
291 ;; according to RFC 822.
292 (if (re-search-forward "[^- !#-'*+/-9=?A-Z^-~]"
295 ;; Quote fullname, escaping specials.
296 (goto-char fullname-start
)
298 (while (re-search-forward "[\"\\]"
300 (replace-match "\\\\\\&" t
))
302 (insert " <" login
">\n"))
303 ((eq mail-from-style
'parens
)
304 (insert "From: " login
" (")
305 (let ((fullname-start (point)))
307 (let ((fullname-end (point-marker)))
308 (goto-char fullname-start
)
309 ;; RFC 822 says \ and nonmatching parentheses
310 ;; must be escaped in comments.
311 ;; Escape every instance of ()\ ...
312 (while (re-search-forward "[()\\]" fullname-end
1)
313 (replace-match "\\\\\\&" t
))
314 ;; ... then undo escaping of matching parentheses,
315 ;; including matching nested parentheses.
316 (goto-char fullname-start
)
317 (while (re-search-forward
318 "\\(\\=\\|[^\\]\\(\\\\\\\\\\)*\\)\\\\(\\(\\([^\\]\\|\\\\\\\\\\)*\\)\\\\)"
320 (replace-match "\\1(\\3)" t
)
321 (goto-char fullname-start
))))
323 ((null mail-from-style
)
324 (insert "From: " login
"\n")))))
325 ;; Insert a `Message-Id:' field if there isn't one yet.
326 (goto-char (point-min))
327 (unless (re-search-forward "^Message-Id:" delimline t
)
328 (insert "Message-Id: " (message-make-message-id) "\n"))
329 ;; Insert a `Date:' field if there isn't one yet.
330 (goto-char (point-min))
331 (unless (re-search-forward "^Date:" delimline t
)
332 (insert "Date: " (message-make-date) "\n"))
333 ;; Possibly add a MIME header for the current coding system
335 (goto-char (point-min))
336 (and (eq mail-send-nonascii
'mime
)
337 (not (re-search-forward "^MIME-version:" delimline t
))
338 (progn (skip-chars-forward "\0-\177")
339 (/= (point) (point-max)))
340 smtpmail-code-conv-from
342 (coding-system-get smtpmail-code-conv-from
344 (goto-char delimline
)
345 (insert "MIME-version: 1.0\n"
346 "Content-type: text/plain; charset="
347 (symbol-name charset
)
348 "\nContent-Transfer-Encoding: 8bit\n")))
349 ;; Insert an extra newline if we need it to work around
350 ;; Sun's bug that swallows newlines.
351 (goto-char (1+ delimline
))
352 (if (eval mail-mailer-swallows-blank-line
)
354 ;; Find and handle any FCC fields.
355 (goto-char (point-min))
356 (if (re-search-forward "^FCC:" delimline t
)
357 ;; Force `mail-do-fcc' to use the encoding of the mail
358 ;; buffer to encode outgoing messages on FCC files.
359 (let ((coding-system-for-write smtpmail-code-conv-from
))
360 (mail-do-fcc delimline
)))
362 (with-current-buffer errbuf
365 (setq smtpmail-address-buffer
(generate-new-buffer "*smtp-mail*"))
366 (setq smtpmail-recipient-address-list
367 (smtpmail-deduce-address-list tembuf
(point-min) delimline
))
368 (kill-buffer smtpmail-address-buffer
)
370 (smtpmail-do-bcc delimline
)
372 (if (not smtpmail-queue-mail
)
373 (if (not (null smtpmail-recipient-address-list
))
374 (if (not (smtpmail-via-smtp
375 smtpmail-recipient-address-list tembuf
))
376 (error "Sending failed; SMTP protocol error"))
377 (error "Sending failed; no recipients"))
381 (format-time-string "%Y-%m-%d_%H:%M:%S")
382 (setq smtpmail-queue-counter
383 (1+ smtpmail-queue-counter
)))
385 (file-data (convert-standard-filename file-data
))
386 (file-elisp (concat file-data
".el"))
387 (buffer-data (create-file-buffer file-data
))
388 (buffer-elisp (create-file-buffer file-elisp
))
389 (buffer-scratch "*queue-mail*"))
390 (unless (file-exists-p smtpmail-queue-dir
)
391 (make-directory smtpmail-queue-dir t
))
392 (with-current-buffer buffer-data
394 (set-buffer-file-coding-system
395 ;; We will be reading the file with no-conversion in
396 ;; smtpmail-send-queued-mail below, so write it out
398 (coding-system-change-eol-conversion
399 (or smtpmail-code-conv-from
'undecided
)
402 (insert-buffer-substring tembuf
)
403 (write-file file-data
)
404 (set-buffer buffer-elisp
)
407 "(setq smtpmail-recipient-address-list '"
408 (prin1-to-string smtpmail-recipient-address-list
)
410 (write-file file-elisp
)
411 (set-buffer (generate-new-buffer buffer-scratch
))
412 (insert (concat file-data
"\n"))
413 (append-to-file (point-min)
415 (expand-file-name smtpmail-queue-index-file
416 smtpmail-queue-dir
)))
417 (kill-buffer buffer-scratch
)
418 (kill-buffer buffer-data
)
419 (kill-buffer buffer-elisp
))))
422 (kill-buffer errbuf
)))))
425 (defun smtpmail-send-queued-mail ()
426 "Send mail that was queued as a result of setting `smtpmail-queue-mail'."
429 ;; Get index, get first mail, send it, update index, get second
430 ;; mail, send it, etc...
432 (qfile (expand-file-name smtpmail-queue-index-file
433 smtpmail-queue-dir
)))
434 (insert-file-contents qfile
)
435 (goto-char (point-min))
437 (setq file-msg
(buffer-substring (point) (line-end-position)))
439 ;; Insert the message literally: it is already encoded as per
440 ;; the MIME headers, and code conversions might guess the
443 (let ((coding-system-for-read 'no-conversion
))
444 (insert-file-contents file-msg
))
445 (let ((smtpmail-mail-address
446 (or (and mail-specify-envelope-from
(mail-envelope-from))
448 (if (not (null smtpmail-recipient-address-list
))
449 (if (not (smtpmail-via-smtp smtpmail-recipient-address-list
451 (error "Sending failed; SMTP protocol error"))
452 (error "Sending failed; no recipients"))))
453 (delete-file file-msg
)
454 (delete-file (concat file-msg
".el"))
455 (delete-region (point-at-bol) (point-at-bol 2)))
456 (write-region (point-min) (point-max) qfile
))))
458 ;; (defun smtpmail-via-smtp (host,port,sender,destination,smtpmail-text-buffer)
460 (defun smtpmail-fqdn ()
461 (if smtpmail-local-domain
462 (concat (system-name) "." smtpmail-local-domain
)
465 (defsubst smtpmail-cred-server
(cred)
468 (defsubst smtpmail-cred-port
(cred)
471 (defsubst smtpmail-cred-key
(cred)
474 (defsubst smtpmail-cred-user
(cred)
477 (defsubst smtpmail-cred-cert
(cred)
480 (defsubst smtpmail-cred-passwd
(cred)
483 (defun smtpmail-find-credentials (cred server port
)
486 (while (setq el
(pop l
))
487 (when (and (equal server
(smtpmail-cred-server el
))
488 (equal port
(smtpmail-cred-port el
)))
489 (throw 'done el
))))))
491 (defun smtpmail-maybe-append-domain (recipient)
492 (if (or (not smtpmail-sendto-domain
)
493 (string-match "@" recipient
))
495 (concat recipient
"@" smtpmail-sendto-domain
)))
497 (defun smtpmail-intersection (list1 list2
)
500 (when (memq el2 list1
)
504 (defvar starttls-extra-args
)
505 (defvar starttls-extra-arguments
)
507 (defun smtpmail-open-stream (process-buffer host port
)
508 (let ((cred (smtpmail-find-credentials
509 smtpmail-starttls-credentials host port
)))
510 (if (null (and cred
(starttls-any-program-available)))
512 (open-network-stream "SMTP" process-buffer host port
)
513 (let* ((cred-key (smtpmail-cred-key cred
))
514 (cred-cert (smtpmail-cred-cert cred
))
518 (when (and (stringp cred-key
) (stringp cred-cert
)
520 (setq cred-key
(expand-file-name cred-key
)))
522 (setq cred-cert
(expand-file-name cred-cert
))))
523 (list "--key-file" cred-key
"--cert-file" cred-cert
))))
524 (starttls-extra-arguments
526 starttls-extra-arguments
527 (when (and (stringp cred-key
) (stringp cred-cert
)
529 (setq cred-key
(expand-file-name cred-key
)))
531 (setq cred-cert
(expand-file-name cred-cert
))))
532 (list "--x509keyfile" cred-key
"--x509certfile" cred-cert
)))))
533 (starttls-open-stream "SMTP" process-buffer host port
)))))
535 ;; `password-read' autoloads password-cache.
536 (declare-function password-cache-add
"password-cache" (key password
))
538 (defun smtpmail-try-auth-methods (process supported-extensions host port
)
539 (let* ((mechs (cdr-safe (assoc 'auth supported-extensions
)))
540 (mech (car (smtpmail-intersection mechs smtpmail-auth-supported
)))
541 (auth-info (auth-source-search :max
1
543 :port
(or port
"smtp")))
544 (auth-user (plist-get (nth 0 auth-info
) :user
))
545 (auth-pass (plist-get (nth 0 auth-info
) :secret
))
546 (auth-pass (if (functionp auth-pass
)
549 (cred (if (and auth-user auth-pass
) ; try user-auth-* before netrc-*
550 (list host port auth-user auth-pass
)
551 ;; else, if auth-source didn't return them...
552 (if (stringp smtpmail-auth-credentials
)
553 (let* ((netrc (netrc-parse smtpmail-auth-credentials
))
554 (port-name (format "%s" (or port
"smtp")))
555 (hostentry (netrc-machine netrc host port-name
559 (netrc-get hostentry
"login")
560 (netrc-get hostentry
"password"))))
561 ;; else, try `smtpmail-find-credentials' since
562 ;; `smtpmail-auth-credentials' is not a string
563 (smtpmail-find-credentials
564 smtpmail-auth-credentials host port
))))
565 (prompt (when cred
(format "SMTP password for %s:%s: "
566 (smtpmail-cred-server cred
)
567 (smtpmail-cred-port cred
))))
569 (or (smtpmail-cred-passwd cred
)
570 (password-read prompt prompt
))))
572 (when (and cred mech
)
575 (smtpmail-send-command process
(upcase (format "AUTH %s" mech
)))
576 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
577 (not (integerp (car ret
)))
580 (when (eq (car ret
) 334)
581 (let* ((challenge (substring (cadr ret
) 4))
582 (decoded (base64-decode-string challenge
))
583 (hash (rfc2104-hash 'md5
64 16 passwd decoded
))
584 (response (concat (smtpmail-cred-user cred
) " " hash
))
585 ;; Osamu Yamane <yamane@green.ocn.ne.jp>:
586 ;; SMTP auth fails because the SMTP server identifies
587 ;; only the first part of the string (delimited by
588 ;; new line characters) as a response from the
589 ;; client, and the rest as distinct commands.
591 ;; In my case, the response string is 80 characters
592 ;; long. Without the no-line-break option for
593 ;; `base64-encode-string', only the first 76 characters
594 ;; are taken as a response to the server, and the
595 ;; authentication fails.
596 (encoded (base64-encode-string response t
)))
597 (smtpmail-send-command process
(format "%s" encoded
))
598 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
599 (not (integerp (car ret
)))
601 (throw 'done nil
)))))
603 (smtpmail-send-command process
"AUTH LOGIN")
604 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
605 (not (integerp (car ret
)))
608 (smtpmail-send-command
609 process
(base64-encode-string (smtpmail-cred-user cred
) t
))
610 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
611 (not (integerp (car ret
)))
614 (smtpmail-send-command process
(base64-encode-string passwd t
))
615 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
616 (not (integerp (car ret
)))
620 ;; We used to send an empty initial request, and wait for an
621 ;; empty response, and then send the password, but this
622 ;; violate a SHOULD in RFC 2222 paragraph 5.1. Note that this
623 ;; is not sent if the server did not advertise AUTH PLAIN in
624 ;; the EHLO response. See RFC 2554 for more info.
625 (smtpmail-send-command process
626 (concat "AUTH PLAIN "
627 (base64-encode-string
629 (smtpmail-cred-user cred
)
632 (if (or (null (car (setq ret
(smtpmail-read-response process
))))
633 (not (integerp (car ret
)))
634 (not (equal (car ret
) 235)))
638 (error "Mechanism %s not implemented" mech
)))
639 ;; Remember the password.
640 (when (null (smtpmail-cred-passwd cred
))
641 (password-cache-add prompt passwd
)))))
643 (defun smtpmail-via-smtp (recipient smtpmail-text-buffer
)
645 (host (or smtpmail-smtp-server
646 (error "`smtpmail-smtp-server' not defined")))
647 (port smtpmail-smtp-service
)
648 ;; `smtpmail-mail-address' should be set to the appropriate
649 ;; buffer-local value by the caller, but in case not:
650 (envelope-from (or smtpmail-mail-address
651 (and mail-specify-envelope-from
652 (mail-envelope-from))
657 (supported-extensions '()))
660 ;; get or create the trace buffer
662 (get-buffer-create (format "*trace of SMTP session to %s*" host
)))
664 ;; clear the trace buffer of old output
665 (with-current-buffer process-buffer
666 (setq buffer-undo-list t
)
669 ;; open the connection to the server
670 (setq process
(smtpmail-open-stream process-buffer host port
))
671 (and (null process
) (throw 'done nil
))
673 ;; set the send-filter
674 (set-process-filter process
'smtpmail-process-filter
)
676 (with-current-buffer process-buffer
677 (set-buffer-process-coding-system 'raw-text-unix
'raw-text-unix
)
678 (make-local-variable 'smtpmail-read-point
)
679 (setq smtpmail-read-point
(point-min))
682 (if (or (null (car (setq greeting
(smtpmail-read-response process
))))
683 (not (integerp (car greeting
)))
684 (>= (car greeting
) 400))
691 (smtpmail-send-command process
(format "EHLO %s" (smtpmail-fqdn)))
693 (if (or (null (car (setq response-code
694 (smtpmail-read-response process
))))
695 (not (integerp (car response-code
)))
696 (>= (car response-code
) 400))
699 (smtpmail-send-command
700 process
(format "HELO %s" (smtpmail-fqdn)))
702 (if (or (null (car (setq response-code
703 (smtpmail-read-response process
))))
704 (not (integerp (car response-code
)))
705 (>= (car response-code
) 400))
707 (dolist (line (cdr (cdr response-code
)))
709 (with-case-table ascii-case-table
710 (mapcar (lambda (s) (intern (downcase s
)))
711 (split-string (substring line
4) "[ ]")))))
712 (and (eq (length name
) 1)
713 (setq name
(car name
)))
715 (cond ((memq (if (consp name
) (car name
) name
)
716 '(verb xvrb
8bitmime onex xone
720 auth
=login auth starttls
))
721 (setq supported-extensions
722 (cons name supported-extensions
)))
723 (smtpmail-warn-about-unknown-extensions
724 (message "Unknown extension %s" name
)))))))
727 (smtpmail-find-credentials smtpmail-starttls-credentials host port
)
728 (member 'starttls supported-extensions
)
729 (numberp (process-id process
)))
731 (smtpmail-send-command process
(format "STARTTLS"))
732 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
733 (not (integerp (car response-code
)))
734 (>= (car response-code
) 400))
736 (starttls-negotiate process
)
737 (setq do-starttls nil
))
738 (setq do-ehlo nil
))))
740 (smtpmail-try-auth-methods process supported-extensions host port
)
742 (if (or (member 'onex supported-extensions
)
743 (member 'xone supported-extensions
))
745 (smtpmail-send-command process
(format "ONEX"))
746 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
747 (not (integerp (car response-code
)))
748 (>= (car response-code
) 400))
751 (if (and smtpmail-debug-verb
752 (or (member 'verb supported-extensions
)
753 (member 'xvrb supported-extensions
)))
755 (smtpmail-send-command process
(format "VERB"))
756 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
757 (not (integerp (car response-code
)))
758 (>= (car response-code
) 400))
761 (if (member 'xusr supported-extensions
)
763 (smtpmail-send-command process
(format "XUSR"))
764 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
765 (not (integerp (car response-code
)))
766 (>= (car response-code
) 400))
769 ;; MAIL FROM:<sender>
771 (if (or (member 'size supported-extensions
)
772 (assoc 'size supported-extensions
))
774 (with-current-buffer smtpmail-text-buffer
776 (+ (- (point-max) (point-min))
777 ;; Add one byte for each change-of-line
778 ;; because of CR-LF representation:
779 (count-lines (point-min) (point-max)))))
782 (if (member '8bitmime supported-extensions
)
784 ;; Code should be added here that transforms
785 ;; the contents of the message buffer into
786 ;; something the receiving SMTP can handle.
787 ;; For a receiver that supports 8BITMIME, this
788 ;; may mean converting BINARY to BASE64, or
789 ;; adding Content-Transfer-Encoding and the
790 ;; other MIME headers. The code should also
791 ;; return an indication of what encoding the
792 ;; message buffer is now, i.e. ASCII or
798 ;; (smtpmail-send-command process (format "MAIL FROM:%s@%s" (user-login-name) (smtpmail-fqdn)))
799 (smtpmail-send-command process
(format "MAIL FROM:<%s>%s%s"
804 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
805 (not (integerp (car response-code
)))
806 (>= (car response-code
) 400))
809 ;; RCPT TO:<recipient>
811 (while (not (null (nth n recipient
)))
812 (smtpmail-send-command process
(format "RCPT TO:<%s>" (smtpmail-maybe-append-domain (nth n recipient
))))
815 (setq response-code
(smtpmail-read-response process
))
816 (if (or (null (car response-code
))
817 (not (integerp (car response-code
)))
818 (>= (car response-code
) 400))
822 (smtpmail-send-command process
"DATA")
824 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
825 (not (integerp (car response-code
)))
826 (>= (car response-code
) 400))
830 (smtpmail-send-data process smtpmail-text-buffer
)
833 (smtpmail-send-command process
".")
835 (if (or (null (car (setq response-code
(smtpmail-read-response process
))))
836 (not (integerp (car response-code
)))
837 (>= (car response-code
) 400))
841 ;; (smtpmail-send-command process "QUIT")
842 ;; (and (null (car (smtpmail-read-response process)))
843 ;; (throw 'done nil))
846 (with-current-buffer (process-buffer process
)
847 (smtpmail-send-command process
"QUIT")
848 (smtpmail-read-response process
)
850 ;; (if (or (null (car (setq response-code (smtpmail-read-response process))))
851 ;; (not (integerp (car response-code)))
852 ;; (>= (car response-code) 400))
853 ;; (throw 'done nil))
854 (delete-process process
)
855 (unless smtpmail-debug-info
856 (kill-buffer process-buffer
)))))))
859 (defun smtpmail-process-filter (process output
)
860 (with-current-buffer (process-buffer process
)
861 (goto-char (point-max))
864 (defun smtpmail-read-response (process)
865 (let ((case-fold-search nil
)
866 (response-strings nil
)
867 (response-continue t
)
868 (return-value '(nil ()))
871 (while response-continue
872 (goto-char smtpmail-read-point
)
873 (while (not (search-forward "\r\n" nil t
))
874 (unless (memq (process-status process
) '(open run
))
876 (accept-process-output process
)
877 (goto-char smtpmail-read-point
))
879 (setq match-end
(point))
880 (setq response-strings
881 (cons (buffer-substring smtpmail-read-point
(- match-end
2))
884 (goto-char smtpmail-read-point
)
885 (if (looking-at "[0-9]+ ")
886 (let ((begin (match-beginning 0))
888 (if smtpmail-debug-info
889 (message "%s" (car response-strings
)))
891 (setq smtpmail-read-point match-end
)
893 ;; ignore lines that start with "0"
894 (if (looking-at "0[0-9]+ ")
896 (setq response-continue nil
)
898 (cons (string-to-number
899 (buffer-substring begin end
))
900 (nreverse response-strings
)))))
902 (if (looking-at "[0-9]+-")
903 (progn (if smtpmail-debug-info
904 (message "%s" (car response-strings
)))
905 (setq smtpmail-read-point match-end
)
906 (setq response-continue t
))
908 (setq smtpmail-read-point match-end
)
909 (setq response-continue nil
)
911 (cons nil
(nreverse response-strings
)))))))
912 (setq smtpmail-read-point match-end
))
916 (defun smtpmail-send-command (process command
)
917 (goto-char (point-max))
918 (if (= (aref command
0) ?P
)
919 (insert "PASS <omitted>\r\n")
920 (insert command
"\r\n"))
921 (setq smtpmail-read-point
(point))
922 (process-send-string process command
)
923 (process-send-string process
"\r\n"))
925 (defun smtpmail-send-data-1 (process data
)
926 (goto-char (point-max))
928 (if (and (multibyte-string-p data
)
929 smtpmail-code-conv-from
)
930 (setq data
(string-as-multibyte
931 (encode-coding-string data smtpmail-code-conv-from
))))
933 (if smtpmail-debug-info
934 (insert data
"\r\n"))
936 (setq smtpmail-read-point
(point))
937 ;; Escape "." at start of a line
938 (if (eq (string-to-char data
) ?.
)
939 (process-send-string process
"."))
940 (process-send-string process data
)
941 (process-send-string process
"\r\n"))
943 (defun smtpmail-send-data (process buffer
)
944 (let ((data-continue t
) sending-data
)
945 (with-current-buffer buffer
946 (goto-char (point-min)))
948 (with-current-buffer buffer
949 (setq sending-data
(buffer-substring (point-at-bol) (point-at-eol)))
951 (setq data-continue
(not (eobp))))
952 (smtpmail-send-data-1 process sending-data
))))
954 (defun smtpmail-deduce-address-list (smtpmail-text-buffer header-start header-end
)
955 "Get address list suitable for smtp RCPT TO: <address>."
957 (with-current-buffer smtpmail-address-buffer
959 (let ((case-fold-search t
)
960 (simple-address-list "")
964 (insert-buffer-substring smtpmail-text-buffer header-start header-end
)
965 (goto-char (point-min))
966 ;; RESENT-* fields should stop processing of regular fields.
969 (if (re-search-forward "^Resent-\\(to\\|cc\\|bcc\\):"
971 "^Resent-\\(to\\|cc\\|bcc\\):"
972 "^\\(To:\\|Cc:\\|Bcc:\\)")))
974 (while (re-search-forward addr-regexp header-end t
)
976 (setq this-line
(match-beginning 0))
978 ;; get any continuation lines
979 (while (and (looking-at "^[ \t]+") (< (point) header-end
))
981 (setq this-line-end
(point-marker))
982 (setq simple-address-list
983 (concat simple-address-list
" "
984 (mail-strip-quoted-names (buffer-substring this-line this-line-end
)))))
986 (insert " " simple-address-list
"\n")
987 (subst-char-in-region (point-min) (point-max) 10 ? t
) ; newline --> blank
988 (subst-char-in-region (point-min) (point-max) ?
, ? t
) ; comma --> blank
989 (subst-char-in-region (point-min) (point-max) 9 ? t
) ; tab --> blank
991 (goto-char (point-min))
992 ;; tidyness in case hook is not robust when it looks at this
993 (while (re-search-forward "[ \t]+" header-end t
) (replace-match " "))
995 (goto-char (point-min))
996 (let (recipient-address-list)
997 (while (re-search-forward " \\([^ ]+\\) " (point-max) t
)
999 (setq recipient-address-list
(cons (buffer-substring (match-beginning 1) (match-end 1))
1000 recipient-address-list
)))
1001 (setq smtpmail-recipient-address-list recipient-address-list
))))))
1003 (defun smtpmail-do-bcc (header-end)
1004 "Delete [Resent-]BCC: and their continuation lines from the header area.
1005 There may be multiple BCC: lines, and each may have arbitrarily
1006 many continuation lines."
1007 (let ((case-fold-search t
))
1009 (goto-char (point-min))
1010 ;; iterate over all BCC: lines
1011 (while (re-search-forward "^\\(RESENT-\\)?BCC:" header-end t
)
1012 (delete-region (match-beginning 0)
1013 (progn (forward-line 1) (point)))
1014 ;; get rid of any continuation lines
1015 (while (and (looking-at "^[ \t].*\n") (< (point) header-end
))
1016 (replace-match ""))))))
1020 ;;; smtpmail.el ends here