Author Topic: Lisp to e-mail content to helpdesk.  (Read 11600 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Lisp to e-mail content to helpdesk.
« Reply #30 on: October 17, 2019, 09:54:38 PM »
If the horse proposal has been submitted to a committee expect they’ll want a hippogriff for the price of a thumbelina.

An aside, I did a quick test of _get-users-email-addy on an active directory based network today and it works. Cheers.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

domenicomaria

  • Swamp Rat
  • Posts: 725
Re: Lisp to e-mail content to helpdesk.
« Reply #31 on: February 09, 2024, 03:43:46 AM »
Regarding _get-users-email-addy the following should work (I say should because I just translated it from vb and I'm not on an active directory network at home so I can't test it). Would be great if someone could verify if it works or not, thanks.

Code: [Select]
(defun _get-users-email-addy ( / network user domain conn qry ldaprs fld result )
    (vl-catch-all-apply
       '(lambda ( )
            (setq
                network (vlax-create-object "Wscript.Network")
                user    (vlax-get network 'username)
                domain  (vlax-get network 'userdomain)
                conn    (vlax-create-object "ADODB.Connection")
            )
            (vlax-put conn 'provider "ADsDSOObject")
            (vlax-invoke conn 'open "ADSI")
            (setq
                qry    (strcat "select mail from 'LDAP://" domain "' ")
                qry    (strcat qry "WHERE objectClass = 'user' and ")
                qry    (strcat qry "samAccountName = '" user "'")
                ldaprs (vlax-invoke conn 'execute qry)
                fld    (vlax-invoke (vlax-get ldaprs 'fields) 'item "mail")
                result (vlax-get fld 'value)
            )
        )
    )
    (vl-catch-all-apply 'vlax-release-object (list fld))
    (vl-catch-all-apply 'vlax-release-object (list ldaprs))
    (vl-catch-all-apply 'vlax-release-object (list conn))
    (vl-catch-all-apply 'vlax-release-object (list network))
    (if (eq 'str (type result)) result)
)

(_get-users-email-addy) >> yourname@yourdomain.com

Cheers.

why doesn't it work for me?

Lonnie

  • Newt
  • Posts: 185
Re: Lisp to e-mail content to helpdesk.
« Reply #32 on: May 21, 2024, 12:35:44 PM »
Sorry I did not see this.

Here is what I got.

Command: (_GET-USERS-EMAIL-ADDY)
"lshaw@coname.net"

I ran this

Code: [Select]
(defun _get-users-email-addy (/ network user domain conn qry ldaprs fld result)
  (setq result nil) ; Initialize result to nil
  (vl-catch-all-apply
    '(lambda ()
       (setq
         network (vlax-create-object "Wscript.Network")
         user    (vlax-get network 'username)
         domain  (vlax-get network 'userdomain)
         conn    (vlax-create-object "ADODB.Connection")
       )
       (vlax-put conn 'provider "ADsDSOObject")
       (vlax-invoke conn 'open "ADSI")
       (setq
         qry    (strcat "select mail from 'LDAP://" domain "' ")
         qry    (strcat qry "WHERE objectClass = 'user' and ")
         qry    (strcat qry "samAccountName = '" user "'")
       )
       (princ (strcat "\nLDAP Query: " qry)) ; Print the LDAP query for debugging
       (setq ldaprs (vlax-invoke conn 'execute qry))
       (setq fld (vlax-invoke (vlax-get ldaprs 'fields) 'item "mail"))
       (setq result (vlax-get fld 'value))
    )
  )
  (if (eq 'str (type result))
    (progn
      (princ (strcat "\nEmail Address: " result)) ; Print the result for debugging
      result
    )
    (progn
      (princ "\nFailed to retrieve email address.")
      nil
    )
  )
)

and got

AP Query: select mail from 'LDAP://DCE' WHERE objectClass = 'user' and samAccountName = 'lshaw'
Email Address: lshaw@coname.net"lshaw@coname.net"


Lonnie

  • Newt
  • Posts: 185
Re: Lisp to e-mail content to helpdesk.
« Reply #33 on: May 21, 2024, 12:36:38 PM »
I never did Publish the lisp.

Code: [Select]
;; Call IT Helpdesk Script

;; Using Outlook, this script will email selected objects in a temporary DWG file.
;; Credits to Ron Perez (ronjonp) for the Outlook example:
;; http://www.theswamp.org/index.php?topic=26953.msg324794#msg324794
;; http://www.theswamp.org/index.php?topic=55486.0
;; Lonnie Shaw 2020-01-06

;; Enter the Email Addresses Here as Strings
(setq *IT-Email* "lshaw@coname.net"
      *CC-Email* "bsmead@coname.net"
      *BCC-Email* "bseverson@coname.net; dsmith@coname.net; tmcauley@coname.net; jnewton@coname.net; pwyman@coname.net; rstock@coname.net; jarreola@coname.net"
      *Fileset1* (strcat "\nI need assistance from a CAD area Lead. Thank you!"
                         "\nExample file from"
                         "\n" (getvar "dwgprefix")
                         "\"" (getvar "dwgname") "\"")
)

(defun c:CRGhelp (/ _catch cc file outlook email sf si ni)

  ;; Load the Visual LISP extension
  (vl-load-com)

  ;; Define a helper function to catch errors
  (defun _catch (f a) (not (vl-catch-all-error-p (vl-catch-all-apply f a))))

  ;; Alert the user to select objects and send mail from Outlook
  (alert "Please create a scratch file by selecting the elements you are having problems with. \n\nAfter you select objects to mail, remember to send mail from Outlook.")

  (if
    (and
      ;; Get selected objects
      (or (ssget "_I") (prompt "\nSelect object(s) to eMail: ") (ssget))
      ;; Create a temporary DWG file
      (setq file (vl-filename-mktemp "" nil ".dwg"))
      (_catch 'vla-WBlock
              (list (cond (*AcadDoc*)
                          ((setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object)))))
                    file
                    (vla-get-activeselectionset *AcadDoc*)))
      ;; Create an Outlook application instance
      (setq outlook (vlax-get-or-create-object "Outlook.Application"))
      (setq sf (vl-catch-all-apply 'vlax-invoke (list (setq ns (vlax-invoke outlook 'GetNameSpace "MAPI")) 'GetDefaultFolder 5)))
      (setq si (vl-catch-all-apply 'vlax-get (list (vlax-get sf 'items) 'count)))
      ;; Create a new email item
      (setq email (vlax-invoke-method outlook 'CreateItem 0))
      ;; Set email subject
      (_catch 'vlax-put (list email 'Subject "Helpdesk Ticket: "))
      ;; Attach the temporary DWG file
      (_catch 'vlax-invoke (list (vlax-get email 'Attachments) 'Add file))
      ;; Add recipients
      (_catch 'vlax-invoke (list (vlax-get email 'Recipients) 'Add *IT-Email*))
      (setq cc (vl-catch-all-apply 'vlax-invoke (list (vlax-get email 'Recipients) 'Add *CC-Email*)))
      (setq bcc (vl-catch-all-apply 'vlax-invoke (list (vlax-get email 'Recipients) 'Add *BCC-Email*)))
      ;; Set recipient types
      (_catch 'vlax-put (list cc 'Type 2))  ;; CC
      (_catch 'vlax-put (list bcc 'Type 3)) ;; BCC
      ;; Set the email body
      (_catch 'vlax-put (list email 'Body *Fileset1*))
    )
    ;; Display the email in Outlook and handle file cleanup
    (progn
      (princ "\nOutlook active...")
      (vlax-invoke email 'Display :vlax-true)
      (if (= (vl-catch-all-apply 'vlax-get (list email 'Sent)) :vlax-true)
          (vl-file-delete file)
          (progn
            (if (= (setq ni (vl-catch-all-apply 'vlax-get (list (vlax-get sf 'items) 'count))) si)
                (vl-file-delete file) ;; No email was added to Sent Items - delete the file
            )
          )
      )
    )
  )
  ;; Release COM objects
  (foreach x (list sf ns email outlook) (and x (vlax-release-object x)))
  (princ)
)



« Last Edit: May 21, 2024, 01:49:08 PM by Lonnie »