-
Notifications
You must be signed in to change notification settings - Fork 1
이메일 검증 로직 구현 #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
이메일 검증 로직 구현 #83
Changes from 1 commit
eb07f28
c8eefab
065239a
5f86ea1
8c13f84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,71 @@ | ||||||||
| use crate::domain::SubscriberEmail; | ||||||||
| use reqwest::Client; | ||||||||
| use secrecy::{ExposeSecret, Secret}; | ||||||||
| use std::time::Duration; | ||||||||
|
|
||||||||
| #[derive(Clone)] | ||||||||
| pub struct EmailClient { | ||||||||
| http_client: Client, | ||||||||
| base_url: String, | ||||||||
| sender: SubscriberEmail, | ||||||||
| authorization_token: Secret<String>, | ||||||||
| } | ||||||||
|
|
||||||||
| impl EmailClient { | ||||||||
| pub fn new( | ||||||||
| base_url: String, | ||||||||
| sender: SubscriberEmail, | ||||||||
| authorization_token: Secret<String>, | ||||||||
| timeout: Duration, | ||||||||
| ) -> Self { | ||||||||
| let http_client = Client::builder().timeout(timeout).build().unwrap(); | ||||||||
| Self { | ||||||||
| http_client, | ||||||||
| base_url, | ||||||||
| sender, | ||||||||
| authorization_token, | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| pub async fn send_email( | ||||||||
| &self, | ||||||||
| recipient: SubscriberEmail, | ||||||||
| subject: String, | ||||||||
| html_content: String, | ||||||||
| text_content: String, | ||||||||
| ) -> Result<(), String> { | ||||||||
| let url = format!("{}/email", self.base_url); | ||||||||
| let request_body = SendEmailRequest { | ||||||||
| from: self.sender.as_ref().to_owned(), | ||||||||
| to: recipient.as_ref().to_owned(), | ||||||||
| subject: subject.to_owned(), | ||||||||
| html_body: html_content.to_owned(), | ||||||||
| text_body: text_content.to_owned(), | ||||||||
| }; | ||||||||
|
|
||||||||
| let _ = self | ||||||||
| .http_client | ||||||||
| .post(&url) | ||||||||
| .header( | ||||||||
| "X-Postmark-Server-Token", | ||||||||
| self.authorization_token.expose_secret(), | ||||||||
| ) | ||||||||
| .json(&request_body) | ||||||||
| .send() | ||||||||
| .await | ||||||||
| .map_err(|e| format!("Failed to send email: {}", e))? | ||||||||
| .error_for_status(); | ||||||||
|
||||||||
| .error_for_status(); | |
| .error_for_status() | |
| .map_err(|e| format!("Email request failed with HTTP error: {}", e))?; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| pub mod domain; | ||
| pub mod email_client; | ||
| pub mod git; | ||
| pub mod link; | ||
| pub mod schedule; | ||
|
|
||
| pub use domain::*; | ||
| pub use email_client::*; | ||
| pub use git::*; | ||
| pub use link::*; | ||
| pub use schedule::*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using unwrap() here can cause a panic if the HTTP client cannot be built. It is recommended to handle this error gracefully, such as by propagating the error using Result.