ניהול כינויים

כתובות אימייל חלופיות לשליחה מייצגות את כתובות האימייל שמהן החשבון יכול לשלוח אימיילים. לכל חשבון יש תמיד כתובת אימייל חלופית אחת לפחות שמייצגת את כתובת האימייל הראשית של החשבון.

כתובות אימייל חלופיות לשליחה תואמות לתכונה 'שליחת אימייל בשם' בממשק האינטרנט.

כתובות אימייל חלופיות משמשות גם לניהול חתימות בחשבון. כדי לשנות את חתימות האימייל, צריך להבין את העיקרון הבסיסי של כתובות אימייל חלופיות לשליחה. בסרטון שלמעלה מוסבר איך לעבור בחזרה על כתובות אימייל חלופיות לצורך שליחה ולשנות את החתימה של כתובת האימייל הראשית של המשתמש.

למידע נוסף על יצירה, הצגת רשימה, אחזור, עדכון או מחיקה של כתובות אימייל חלופיות, קראו את חומר העזרה SendAs.

יצירת כינויים ואימות שלהם

צריך ליצור כתובות אימייל חלופיות לפני שמשתמשים בהן. במקרים מסוימים, המשתמשים צריכים לאמת גם את הבעלות על כתובת האימייל החלופית.

אם מערכת Gmail דורשת אימות משתמש לכתובת אימייל חלופית, הכתובת הזו תוחזר עם הסטטוס pending. הודעת אימות נשלחת באופן אוטומטי לכתובת האימייל היעד. הבעלים של כתובת האימייל צריך להשלים את תהליך האימות כדי שניתן יהיה להשתמש בה.

לכתובות אימייל חלופיות שלא מחייבות אימות יש סטטוס אימות accepted.

אם צריך, משתמשים בשיטה verify כדי לשלוח מחדש את בקשת האימות.

הגדרות SMTP

כתובות אימייל חלופיות לכתובות חיצוניות צריכות לשלוח אימייל דרך סוכן SMTP מרוחק לשליחת אימייל (MSA). כדי להגדיר את ה-MSA של SMTP לכתובת אימייל חלופית, צריך להזין את פרטי החיבור בשדה smtpMsa.

ניהול חתימות

אפשר גם להגדיר חתימות אימייל לכל כתובת אימייל חלופית. לדוגמה, כדי להגדיר את החתימה לכתובת הראשית של המשתמש:

Java

gmail/snippets/src/main/java/UpdateSignature.java
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.ListSendAsResponse;
import com.google.api.services.gmail.model.SendAs;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;

/* Class to demonstrate the use of Gmail Update Signature API */
public class UpdateSignature {
  /**
   * Update the gmail signature.
   *
   * @return the updated signature id , {@code null} otherwise.
   * @throws IOException - if service account credentials file not found.
   */
  public static String updateGmailSignature() throws IOException {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(GmailScopes.GMAIL_SETTINGS_BASIC);
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);

    // Create the gmail API client
    Gmail service = new Gmail.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Gmail samples")
        .build();

    try {
      SendAs primaryAlias = null;
      ListSendAsResponse aliases = service.users().settings().sendAs().list("me").execute();
      for (SendAs alias : aliases.getSendAs()) {
        if (alias.getIsPrimary()) {
          primaryAlias = alias;
          break;
        }
      }
      // Updating a new signature
      SendAs aliasSettings = new SendAs().setSignature("Automated Signature");
      SendAs result = service.users().settings().sendAs().patch(
              "me",
              primaryAlias.getSendAsEmail(),
              aliasSettings)
          .execute();
      //Prints the updated signature
      System.out.println("Updated signature - " + result.getSignature());
      return result.getSignature();
    } catch (GoogleJsonResponseException e) {
      // TODO(developer) - handle error appropriately
      GoogleJsonError error = e.getDetails();
      if (error.getCode() == 403) {
        System.err.println("Unable to update signature: " + e.getDetails());
      } else {
        throw e;
      }
    }
    return null;
  }
}

Python

gmail/snippet/settings snippets/update_signature.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def update_signature():
  """Create and update signature in gmail.
  Returns:Draft object, including updated signature.

  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """
  creds, _ = google.auth.default()

  try:
    # create gmail api client
    service = build("gmail", "v1", credentials=creds)

    primary_alias = None

    # pylint: disable=E1101
    aliases = service.users().settings().sendAs().list(userId="me").execute()
    for alias in aliases.get("sendAs"):
      if alias.get("isPrimary"):
        primary_alias = alias
        break

    send_as_configuration = {
        "displayName": primary_alias.get("sendAsEmail"),
        "signature": "Automated Signature",
    }

    # pylint: disable=E1101
    result = (
        service.users()
        .settings()
        .sendAs()
        .patch(
            userId="me",
            sendAsEmail=primary_alias.get("sendAsEmail"),
            body=send_as_configuration,
        )
        .execute()
    )
    print(f'Updated signature for: {result.get("displayName")}')

  except HttpError as error:
    print(f"An error occurred: {error}")
    result = None

  return result.get("signature")


if __name__ == "__main__":
  update_signature()