การจัดการชื่อแทน

ชื่อแทนที่ใช้ "ส่งอีเมล" หมายถึงที่อยู่อีเมลที่บัญชีใช้ส่งอีเมลได้ แต่ละบัญชีจะมีชื่อแทนอย่างน้อย 1 ชื่อเพื่อใช้เป็นอีเมลหลักของบัญชี

ชื่อแทนที่ใช้ส่งจะสอดคล้องกับคุณลักษณะ"ส่งอีเมลในชื่อ" ในอินเทอร์เฟซบนเว็บ

นอกจากนี้ ชื่อแทนยังใช้เพื่อจัดการลายเซ็นของบัญชีด้วย คุณต้องมีความเข้าใจเบื้องต้นเกี่ยวกับชื่อแทนที่ใช้ส่งจึงจะเปลี่ยนลายเซ็นของอีเมลได้ วิดีโอด้านบนจะแสดงวิธีวนซ้ำชื่อแทนการส่งในชื่อและแก้ไขลายเซ็นสำหรับอีเมลหลักของผู้ใช้

ดูข้อมูลเกี่ยวกับวิธีสร้าง รายการ รับ อัปเดต หรือลบชื่อแทน โปรดดูSendAsข้อมูลอ้างอิง

การสร้างและยืนยันชื่อแทน

คุณต้องสร้าง ชื่อแทนก่อนใช้ ในบางกรณี ผู้ใช้จะต้องยืนยันการเป็นเจ้าของชื่อแทนด้วย

หาก Gmail กำหนดให้มีการยืนยันผู้ใช้สำหรับชื่อแทน ระบบจะแสดงชื่อแทนโดยมีสถานะ pending ระบบจะส่งข้อความยืนยันไปยังอีเมลเป้าหมายโดยอัตโนมัติ เจ้าของอีเมลต้องผ่านกระบวนการยืนยันก่อนจึงจะสามารถใช้ได้

ชื่อแทนที่ไม่ต้องมีการยืนยันจะมีสถานะการยืนยันเป็น accepted

ใช้วิธีการยืนยันเพื่อส่งคำขอการยืนยันอีกครั้ง หากจำเป็น

การตั้งค่า SMTP

ชื่อแทนสำหรับที่อยู่ภายนอกควรส่งอีเมลผ่านเอเจนต์การส่งอีเมล SMTP ระยะไกล (MSA) หากต้องการกำหนดค่า SMTP MSA สำหรับชื่อแทน ให้ใช้ช่อง 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 ข้อมูลโค้ด/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()