Postmark Rails Gem 使用教程
1. 项目的目录结构及介绍
Postmark Rails Gem 是一个用于通过 Postmark API 发送邮件的 Rails 插件。以下是其基本的目录结构:
postmark-rails/
├── CHANGELOG.rdoc
├── CONTRIBUTING.md
├── Gemfile
├── LICENSE
├── README.md
├── Rakefile
├── postmark-rails.gemspec
├── lib/
│ ├── postmark.rb
│ ├── postmark/
│ │ ├── version.rb
│ │ └── ...
│ └── ...
├── spec/
│ ├── spec_helper.rb
│ └── ...
└── ...
目录介绍
- CHANGELOG.rdoc: 记录项目的变更历史。
- CONTRIBUTING.md: 贡献指南。
- Gemfile: 定义项目的依赖。
- LICENSE: 项目的许可证信息。
- README.md: 项目的基本介绍和使用说明。
- Rakefile: 定义 Rake 任务。
- postmark-rails.gemspec: 项目的 gem 规范文件。
- lib/: 包含项目的主要代码。
- spec/: 包含项目的测试代码。
2. 项目的启动文件介绍
Postmark Rails Gem 的启动文件主要位于 lib/
目录下。以下是一些关键文件:
- lib/postmark.rb: 这是 gem 的主文件,负责加载其他必要的文件和模块。
- lib/postmark/version.rb: 定义 gem 的版本号。
启动文件介绍
-
postmark.rb: 这个文件负责初始化 gem 并加载其他模块。它通常包含以下内容:
require 'postmark/version' require 'postmark/api_client' require 'postmark/action_mailer'
-
version.rb: 这个文件定义了 gem 的版本号,通常包含以下内容:
module Postmark VERSION = '1.23.0' end
3. 项目的配置文件介绍
Postmark Rails Gem 的配置文件主要涉及 Rails 应用的配置。以下是一些关键配置:
配置文件介绍
-
config/application.rb: 在这个文件中,你需要设置 Postmark 作为邮件发送方法,并配置 API 令牌。
config.action_mailer.delivery_method = :postmark config.action_mailer.postmark_settings = { api_token: Rails.application.credentials.postmark_api_token }
-
config/credentials.yml.enc: 在这个加密文件中,你需要保存 Postmark 的 API 令牌。
postmark_api_token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
配置步骤
-
添加 Postmark Rails Gem 到 Gemfile:
gem 'postmark-rails'
-
运行 bundle install:
bundle install
-
设置 Postmark API 令牌:
-
运行
rails secret
生成密钥。 -
运行
rails credentials:edit
编辑加密文件,并添加以下内容:postmark_api_token: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
-
-
配置邮件发送方法:
-
在
config/application.rb
中添加以下配置:config.action_mailer.delivery_method = :postmark config.action_mailer.postmark_settings = { api_token: Rails.application.credentials.postmark_api_token }
-
通过以上步骤,你可以成功配置并使用 Postmark Rails Gem 发送邮件。