產生電子郵件動作連結

行動應用程式有時需要與使用者互動,並提示他們採取 某些動作會比直接傳送電子郵件

Firebase 用戶端 SDK 可讓使用者將含有 可用來重設密碼、驗證電子郵件地址,以及 電子郵件登入這些範本式電子郵件是由 Google 發送 客製化程度有限。

如果想改用自己的電子郵件範本和電子郵件 服務,本頁面說明如何使用 Firebase Admin SDK 以程式輔助的方式為上述流程產生動作連結,請在 這個提示可以包含在傳送給使用者的電子郵件中。

它具有以下優點:

  • 自訂電子郵件範本。包括新增樣式和 自訂品牌宣傳, 變更用語和標誌;改用名字來引導使用者 等等。
  • 根據內容套用不同的範本。舉例來說,如果使用者 然後驗證電子郵件才訂閱電子報 使用提示另一個例子是電子郵件連結登入: 情境可能是同一位使用者,或是由另一位使用者發出邀請 內容。需要在電子郵件中加入背景資訊。
  • 將自訂電子郵件範本本地化。
  • 可從安全的伺服器環境產生連結。
  • 可讓使用者自訂透過行動應用程式或 以及如何傳遞其他狀態資訊等
  • 可以在發生以下情況時,自訂要用於行動應用程式流程的動態連結網域: 建立電子郵件動作連結,甚至指定其他動態連結 以網域或行動應用程式為準

初始化 ActionCodeSettings

產生電子郵件動作連結之前,您可能需要將 ActionCodeSettings 執行個體。

ActionCodeSettings 可讓您透過繼續網址傳遞其他狀態, 使用者按一下電子郵件連結後即可使用。這麼做也能讓使用者 使用者可在操作完成後返回應用程式此外, 您可以指定是否要直接從行動裝置處理電子郵件動作連結 應用程式。

如果是要使用行動應用程式開啟的連結,您需要啟用 Firebase Dynamic Links 並執行幾項工作,以偵測這些連結 行動應用程式請參閱 設定 Firebase Dynamic Links 用於電子郵件動作

如要初始化 ActionCodeSettings 例項,請提供下列資料:

參數 類型 說明
url 字串

設定不同意義的連結 (狀態/繼續網址) 不同的情境:

  • 在網頁動作小工具中處理連結時,這就是深層連結 連結。continueUrl
  • 如果直接在應用程式中處理連結,則這是 continueUrl 查詢參數 動態連結。
iOS ({bundleId: string}|未定義) 設定軟體包 ID。如果發生以下情況,系統將嘗試在 Apple 應用程式中開啟連結 已安裝您必須在 Play 管理中心註冊應用程式。
android ({packageName: string, installApp:boolean|undefined, minVersion:字串|未定義}|未定義) 設定 Android 套件名稱。系統將嘗試在 Android 應用程式 (如有安裝)。如果傳遞 installApp,則會 會指定是否要安裝 Android 應用程式 (裝置支援的話),並 使用者尚未安裝應用程式如果這個欄位不含 packageName,系統會擲回錯誤,說明 「packageName」必須搭配這個欄位提供。 如果指定的是 minimumVersion,那麼 應用程式安裝成功後,系統會引導使用者前往 Play 商店升級應用程式。 您必須在控制台中註冊 Android 應用程式。
handleCodeInApp (布林值|未定義) 用來在行動應用程式或網頁上開啟電子郵件動作連結 連結。預設值為 false。如果設為 true,動作代碼連結 會以通用連結或 Android 應用程式連結的形式傳送,且會開啟 。如果是 false,則程式碼會傳送到 網頁小工具會重新導向至 已安裝。
dynamicLinkDomain (字串|未定義) 設定目前連結要使用的動態連結網域 (或子網域) (如果是使用 Firebase Dynamic Links 開啟)做為多重動態 每個專案都可以設定連結網域 明確選擇一種如未提供,表示最舊的網域 加以排序。

以下範例說明如何傳送 會以 Firebase Dynamic Link (Apple) 的形式在行動應用程式中開啟 應用程式 com.example.ios 或 Android 應用程式 com.example.android。 安裝 (如果尚未安裝,且最低版本為 12)。深層連結 會包含「繼續」網址酬載 https://www.example.com/checkout?cartId=1234。使用的動態連結網域是 coolapp.page.link,必須設為與 Firebase 動態搭配使用 連結。

Node.js

const actionCodeSettings = {
  // URL you want to redirect back to. The domain (www.example.com) for
  // this URL must be whitelisted in the Firebase Console.
  url: 'https://www.example.com/checkout?cartId=1234',
  // This must be true for email link sign-in.
  handleCodeInApp: true,
  iOS: {
    bundleId: 'com.example.ios',
  },
  android: {
    packageName: 'com.example.android',
    installApp: true,
    minimumVersion: '12',
  },
  // FDL custom domain.
  dynamicLinkDomain: 'coolapp.page.link',
};

Java

ActionCodeSettings actionCodeSettings = ActionCodeSettings.builder()
    .setUrl("https://www.example.com/checkout?cartId=1234")
    .setHandleCodeInApp(true)
    .setIosBundleId("com.example.ios")
    .setAndroidPackageName("com.example.android")
    .setAndroidInstallApp(true)
    .setAndroidMinimumVersion("12")
    .setDynamicLinkDomain("coolapp.page.link")
    .build();

Python

action_code_settings = auth.ActionCodeSettings(
    url='https://www.example.com/checkout?cartId=1234',
    handle_code_in_app=True,
    ios_bundle_id='com.example.ios',
    android_package_name='com.example.android',
    android_install_app=True,
    android_minimum_version='12',
    dynamic_link_domain='coolapp.page.link',
)

Go

actionCodeSettings := &auth.ActionCodeSettings{
	URL:                   "https://www.example.com/checkout?cartId=1234",
	HandleCodeInApp:       true,
	IOSBundleID:           "com.example.ios",
	AndroidPackageName:    "com.example.android",
	AndroidInstallApp:     true,
	AndroidMinimumVersion: "12",
	DynamicLinkDomain:     "coolapp.page.link",
}

C#

var actionCodeSettings = new ActionCodeSettings()
{
    Url = "https://www.example.com/checkout?cartId=1234",
    HandleCodeInApp = true,
    IosBundleId = "com.example.ios",
    AndroidPackageName = "com.example.android",
    AndroidInstallApp = true,
    AndroidMinimumVersion = "12",
    DynamicLinkDomain = "coolapp.page.link",
};

詳情請參閱: 電子郵件動作中的通過狀態

如要產生密碼重設連結,請提供現有使用者的電子郵件和 選用的 ActionCodeSettings 物件。這項作業會透過電子郵件進行 動作連結。使用的電子郵件必須屬於現有使用者。

Node.js

// Admin SDK API to generate the password reset link.
const userEmail = 'user@example.com';
getAuth()
  .generatePasswordResetLink(userEmail, actionCodeSettings)
  .then((link) => {
    // Construct password reset email template, embed the link and send
    // using custom SMTP server.
    return sendCustomPasswordResetEmail(userEmail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

Java

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generatePasswordResetLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

Python

email = 'user@example.com'
link = auth.generate_password_reset_link(email, action_code_settings)
# Construct password reset email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

Go

email := "user@example.com"
link, err := client.PasswordResetLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct password reset template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

C#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GeneratePasswordResetLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

產生連結後,即可將其插入自訂密碼重設作業 電子郵件,然後使用自訂 SMTP 伺服器,透過電子郵件傳送給對應的使用者。

如果您不是使用預設密碼重設到達網頁,而是建立 自定義處理常式的 建立自訂電子郵件動作處理常式

如要產生電子郵件驗證連結,請提供現有使用者的未驗證狀態 電子郵件地址,以及一個選用的 ActionCodeSettings 物件。這項作業會解析 的電子郵件動作連結 使用的電子郵件必須屬於現有使用者。

Node.js

// Admin SDK API to generate the email verification link.
const useremail = 'user@example.com';
getAuth()
  .generateEmailVerificationLink(useremail, actionCodeSettings)
  .then((link) => {
    // Construct email verification template, embed the link and send
    // using custom SMTP server.
    return sendCustomVerificationEmail(useremail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

Java

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generateEmailVerificationLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

Python

email = 'user@example.com'
link = auth.generate_email_verification_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

Go

email := "user@example.com"
link, err := client.EmailVerificationLinkWithSettings(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct email verification template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

C#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateEmailVerificationLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

產生連結後,即可插入自訂驗證中 電子郵件,然後使用自訂 SMTP 伺服器,透過電子郵件傳送給對應的使用者。

如果未使用預設的電子郵件驗證到達網頁和建立 您自己的自訂處理常式的 建立自訂電子郵件動作處理常式

您必須先完成下列步驟,才能使用電子郵件連結登入來驗證使用者: 啟用電子郵件連結登入功能

如要產生登入連結,請提供使用者的電子郵件地址和ActionCodeSettings 物件。在此情況下,必須提供 ActionCodeSettings 物件 瞭解使用者點選連結並登入後要導向何處 完成。這項作業會透過電子郵件動作連結完成。

有別於密碼重設和電子郵件驗證,使用的電子郵件地址不會 只需要屬於現有使用者,因為這項作業可用於 透過電子郵件連結,為新使用者註冊應用程式。

Node.js

// Admin SDK API to generate the sign in with email link.
const useremail = 'user@example.com';
getAuth()
  .generateSignInWithEmailLink(useremail, actionCodeSettings)
  .then((link) => {
    // Construct sign-in with email link template, embed the link and
    // send using custom SMTP server.
    return sendSignInEmail(useremail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

Java

String email = "user@example.com";
try {
  String link = FirebaseAuth.getInstance().generateSignInWithEmailLink(
      email, actionCodeSettings);
  // Construct email verification template, embed the link and send
  // using custom SMTP server.
  sendCustomEmail(email, displayName, link);
} catch (FirebaseAuthException e) {
  System.out.println("Error generating email link: " + e.getMessage());
}

Python

email = 'user@example.com'
link = auth.generate_sign_in_with_email_link(email, action_code_settings)
# Construct email from a template embedding the link, and send
# using a custom SMTP server.
send_custom_email(email, link)

Go

email := "user@example.com"
link, err := client.EmailSignInLink(ctx, email, actionCodeSettings)
if err != nil {
	log.Fatalf("error generating email link: %v\n", err)
}

// Construct sign-in with email link template, embed the link and send
// using custom SMTP server.
sendCustomEmail(email, displayName, link)

C#

var email = "user@example.com";
var link = await FirebaseAuth.DefaultInstance.GenerateSignInWithEmailLinkAsync(
    email, actionCodeSettings);
// Construct email verification template, embed the link and send
// using custom SMTP server.
SendCustomEmail(email, displayName, link);

連結產生後,即可插入自訂的登入電子郵件中 然後使用自訂 SMTP 伺服器,透過電子郵件傳送給對應的使用者。

進一步瞭解 使用電子郵件連結透過 Firebase 驗證使用者。 這有助於提供資訊,說明如何在使用者之後完成登入程序 按一下連結並重新導向至應用程式。