The postal
https://github.com/drewr/postal
I want to send mail with clojure, but I don't know how to set sender name.
This my code.
(defn smtp []
{:host (env :mail-host)
:user (env :mail-user)
:pass (env :mail-pass)
:ssl :yes!!!11})
(defn mail [request]
{:from "demo#gmail.com"
:to "demo2#gmail.com"
:subject "subject"
:body "body"})
(defn send [request]
(p/send-message (smtp) (mail request)))
Thanks!
You need to change the address from demo#gmail.com to Your Name <demo#gmail.com>
(defn mail [request]
{:from "Tornado <demo#gmail.com>"
:to "Hurricane <demo2#gmail.com>"
:subject "subject"
:body "body"})
Related
I have to save a username in a session and I can't seem to make it work. This is my code (sesh is noir session):
(defn set-loggedin [username password]
(do
(sesh/put! :username (:user username))
(sesh/put! :password (:pass password))))
(defn login-handler [username password]
(let [user (datab/login username password)]
(if (empty? user)
(view/login-form "Wrong username/password")
(do
(set-loggedin username password)
(response/redirect "/main")))))
(defroutes app-routes
(GET "/" [] (view/login-form "" ))
(POST "/" [username password] (login-handler username password))
(GET "/main" [] (view/main-page))
(route/resources "/")
(route/not-found "Not Found"))
(def app
(sesh/wrap-noir-session
(wrap-defaults app-routes (assoc-in site-defaults [:security :anti-forgery]
false))))
This is the main page in hiccup and it doesn't show the user when I login:
(defn main-page []
(html[:head [:title "main"]] [:body
(sesh/get :username)] ))
Any help would be appreciated!
In your set-loggedin function, you are trying to get the :user key from 'username', which I suspect is a string and the :pass key from 'password', which is probably a string.
The do is also unnecessary in a function definition so try this:
(defn set-loggedin [username password]
(sesh/put! :username username)
(sesh/put! :password password)))
I'm building a server using Cloujre's Compojure. The default route is compojure.route/not-found, is there a way of getting the request that reached this route? I'd like print all requests that end up there.
You can use this kinda approach:
(def handler (-> your-routes
wrap-my-request-middleware ;; it has to be in this order
...))
Let's log in here the uri
(defn wrap-my-request-middleware
[handler]
(fn [request]
(let [response (handler request)]
(when (= 404 (:status response))
;; do whatever you like in here
(log/info (str "Request path: " (:uri request))))
response)));; fn needs to return reponse...
I've just saved the current logged-in user in session. Now how do I get user's id from session in Selmer templates?. Thanks
(defn login-form [{:keys [flash]}]
(layout/render
"login_form.html"
(merge (select-keys flash [:id :pass :errors]))))
(defn authenticate [id pass]
(when-let [user (db/get-user {:id id})]
(when (hashers/check pass (:pass user))
id)))
(defn login! [{:keys [params]}]
(if-let [id (authenticate (:id params) (:pass params))] ;TODO: Refactor
(do
>>>>>>>>> (update (response/found "/") :session {:id id}))
(layout/render "login_form.html" {:invalid-credentials? true})))
(defroutes auth-routes
(GET "/accounts/login/" request (login-form request))
(POST "/accounts/login/" request (login! request)))
Update
I've updated my login function. and it works.
(defn login! [{:keys [params session]}]
(if-let [id (authenticate (:id params) (:pass params))] ;TODO: Refactor
(-> (response/found "/")
(assoc :session
(assoc session :id id)))
(layout/render "login_form.html" {:invalid-credentials? true})))
To print the user's id, I've changed the home-routes.
(defn home-page [opts]
(layout/render "home.html" opts))
(defroutes home-routes
(GET "/" {:keys [flash session]} (home-page (or flash session))))
Now I can print user's id in home.html template. But If I use any other url then user's id stopped `displaying``.
Question > So do I need to pass {:keys [flash session]} in every routes?
You can make use of the wrap-identity middleware which Luminus provides by making sure it's in your wrap-base stack, then change :id to :identity in your session. You can now call (layout/render "home.html") (even without providing opts!) and you can access the identity using {{identity}}
I am new to Clojure and using Luminus to build a website, I have been trying to integrate OpenID to my site but I am failing so bad. I have this code example:
https://github.com/cemerick/friend-demo/blob/master/src/clj/cemerick/friend_demo/openid.clj
demo: http://friend-demo.herokuapp.com/openid/
I am trying to implement it to my site but I keep getting errors, in the end I just wanted to copy it exactly to see if its working on my localhost. So I have this code in my home.clj
But it just doesn't work, whenever I click Login button I get "Not Found" at localhost:3000/login
Does it mean I need to handle /login somewhat? but it is not documented in the example code above.
(def providers [{:name "Steam" :url "http://steamcommunity.com/openid"}
{:name "Yahoo" :url "http://me.yahoo.com/"}])
(defroutes home-routes
(GET "/" req
(h/html5
pretty-head
(pretty-body
; (github-link req)
[:h2 "Authenticating with various services using OpenID"]
[:h3 "Current Status " [:small "(this will change when you log in/out)"]]
(if-let [auth (friend/current-authentication req)]
[:p "Some information delivered by your OpenID provider:"
[:ul (for [[k v] auth
:let [[k v] (if (= :identity k)
["Your OpenID identity" (str (subs v 0 (* (count v) 2/3)) "…")]
[k v])]]
[:li [:strong (str (name k) ": ")] v])]]
[:div
[:h3 "Login with…"]
(for [{:keys [name url]} providers
:let [base-login-url (context-uri req (str "/login?identifier=" url))
dom-id (str (gensym))]]
[:form {:method "POST" :action (context-uri req "login")
:onsubmit (when (.contains ^String url "username")
(format "var input = document.getElementById(%s); input.value = input.value.replace('username', prompt('What is your %s username?')); return true;"
(str \' dom-id \') name))}
[:input {:type "hidden" :name "identifier" :value url :id dom-id}]
[:input {:type "submit" :class "button" :value name}]])
[:p "…or, with a user-provided OpenID URL:"]
[:form {:method "POST" :action (context-uri req "login")}
[:input {:type "text" :name "identifier" :style "width:250px;"}]
[:input {:type "submit" :class "button" :value "Login"}]]])
[:h3 "Logging out"]
[:p [:a {:href (context-uri req "logout")} "Click here to log out"] "."])))
(GET "/logout" req
(friend/logout* (resp/redirect (str (:context req) "/")))))
(def page (friend/authenticate
home-routes
{:allow-anon? true
:default-landing-uri "/"
:workflows [(openid/workflow
:openid-uri "/login"
:credential-fn identity)]}))
The /login callback is handled by openid-workflow which essentially becomes a wrapper around your home-routes. You are likely using home-routes as your request handler when you should use page instead.
I'm trying to send email from Clojure using the following code:
Helper function that sends email:
(defn- send [recipient from subject msg host content-type & attachments]
(let [att-ds (map (fn [at] {:ds (ByteArrayDataSource. (:source at)
(:type at))
:name (:name at)
:description (:description at)})
attachments)
mpmail (MultiPartEmail.)]
(doto mpmail
(.setHostName host)
(.setFrom from)
(.setSubject subject)
(.setContent msg content-type)
(.setCharset "utf-8"))
(.addTo mpmail recipient)
(doseq [ds att-ds]
(.attach mpmail (:ds ds) (:name ds) (:description ds)))
(.send mpmail)))
Usage:
(send "sender#my.domain"
"recipient#my.domain"
"SUBJECT"
"MSG"
"my.smtp.server"
"text/plain"
{:source (.getBytes "Attachment")
:type "text/plain"
:name "test.txt"
:description "test"})
Running the above from the REPL (or from my app) results in recipient#my.domain receiving an email with the subject "SUBJECT" and body "MSG" but without any traces of attachment. No exceptions are raised anywhere.
I have tried this with two different smtp servers.
Thanks for any help.
Try replace (.setContent msg) with (.setMsg msg). May be when you call setContent it thinks you manually set content and ignores following attach methods.
Try to use my code snippet http://middlesphere-1.blogspot.ru/2014/11/clojure-how-to-send-mail-with-attachment.html
attachment filename can be in unicode.