ข้อกำหนดเบื้องต้น
โปรเจ็กต์ Google Cloud ที่เปิดใช้งานการเรียกเก็บเงินแล้ว
บัญชี Mailtarget ที่มีสิทธิ์เข้าถึง API
Vertex AI API ที่เปิดใช้งานในโปรเจ็กต์ GCP ของคุณแล้ว
ติดตั้ง Python 3.10 ขึ้นไปแล้ว
โดเมนบริษัทที่คุณเป็นเจ้าของ (เช่น yourcompany.com)
⚠️ ต้องใช้โดเมนของบริษัท
คุณต้องใช้โดเมนของบริษัทของคุณเองในการส่งอีเมล (เช่น [email protected]) โดเมนอีเมลฟรี เช่น Gmail, Yahoo, Outlook หรือผู้ให้บริการอีเมลสาธารณะใดๆ ไม่ได้รับอนุญาตให้ใช้เป็นที่อยู่ผู้ส่ง
โดเมนของคุณสามารถโฮสต์บนผู้ให้บริการใดก็ได้ — Google Workspace, Microsoft 365, Cloudflare หรือโฮสต์ DNS ใดๆ ตราบใดที่คุณเป็นเจ้าของโดเมนและสามารถกำหนดค่าระเบียน DNS ได้ คุณก็พร้อมใช้งานแล้ว
💡 เราขอแนะนำอย่างยิ่งให้ใช้โดเมนย่อย (เช่น mail.yourcompany.com) หรือโดเมนเฉพาะใหม่สำหรับการส่งอีเมล วิธีนี้จะช่วยรักษาชื่อเสียงของโดเมนหลักของคุณให้แยกต่างหาก และให้สภาพแวดล้อมที่ปลอดภัยสำหรับการทดสอบ
ตั้งค่าสภาพแวดล้อม GCP ของคุณ
ติดตั้ง Google Cloud CLI และยืนยันตัวตน:
# Install gcloud CLI (if not already installed)
# https://cloud.google.com/sdk/docs/install
# Authenticate
gcloud auth login
gcloud auth application-default login
# Set your project
gcloud config set project YOUR_PROJECT_ID
# Enable Vertex AI API gcloud services enable aiplatform.googleapis.comรับคีย์ API ของ Mailtarget
ก. เข้าสู่ระบบ app.mailtarget.co
ข. ไปที่ การตั้งค่า → คีย์ API
ค. คลิก สร้างคีย์ API
ง. คัดลอกคีย์
export MAILTARGET_API_KEY=your_api_key_here
ติดตั้งส่วนประกอบที่จำเป็น
pip install google-cloud-aiplatform requests
กำหนดค่าเครื่องมือ Mailtarget
สร้างฟังก์ชัน Python ที่เอเจนต์ Vertex AI ของคุณจะใช้ในการส่งอีเมล:
import requests
import os
def send_email(
to_email: str,
to_name: str,
subject: str,
body_html: str,
body_text: str = ""
) -> dict:
"""Send an email via Mailtarget API.
Args:
to_email: Recipient email address.
to_name: Recipient name.
subject: Email subject line.
body_html: HTML content of the email.
body_text: Plain text fallback (optional).
Returns:
dict with transmissionId on success, or error details.
"""
response = requests.post(
"https://transmission.mailtarget.co/v1/layang/transmissions",
headers={
"Authorization": f"Bearer {os.environ['MAILTARGET_API_KEY']}",
"Content-Type": "application/json"
},
json={
"to": [{"email": to_email, "name": to_name}],
"from": {
"email": "[email protected]",
"name": "Your Company"
},
"subject": subject,
"bodyHtml": body_html,
"bodyText": body_text,
"optionsAttributes": {
"openTracking": True,
"clickTracking": True,
"transactional": True
}
}
)
return response.json()แทนที่ [email protected] และ Your Company ด้วยโดเมนผู้ส่งที่ได้รับการยืนยันและชื่อบริษัทของคุณ
สร้างเอเจนต์ Vertex AI
เชื่อมต่อเครื่องมือเข้ากับเอเจนต์ Vertex AI โดยใช้ Agent Development Kit (ADK) หรือ LangChain:
ตัวเลือก A: การใช้ Google ADK
from google.adk.agents import Agent
agent = Agent(
name="email_agent",
model="gemini-2.0-flash",
description="An agent that sends emails via Mailtarget.",
instruction="""You are an email assistant. When asked to send
an email, use the send_email tool. Write professional HTML
emails with clean formatting. Always confirm the transmissionId
after sending.""",
tools=[send_email],
)
# Run the agent
response = agent.run(
"Send a welcome email to [email protected] (John Smith) "
"with a friendly onboarding message."
)
print(response)ตัวเลือก B: การใช้ LangChain
from langchain_google_vertexai import ChatVertexAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.tools import tool
from langchain_core.prompts import ChatPromptTemplate
@tool
def send_email(to_email: str, to_name: str, subject: str,
body_html: str, body_text: str = "") -> dict:
"""Send an email via Mailtarget API."""
import requests, os
response = requests.post(
"https://transmission.mailtarget.co/v1/layang/transmissions",
headers={
"Authorization": f"Bearer {os.environ['MAILTARGET_API_KEY']}",
"Content-Type": "application/json"
},
json={
"to": [{"email": to_email, "name": to_name}],
"from": {"email": "[email protected]",
"name": "Your Company"},
"subject": subject,
"bodyHtml": body_html,
"bodyText": body_text,
"optionsAttributes": {"openTracking": True,
"clickTracking": True}
}
)
return response.json()
llm = ChatVertexAI(model="gemini-2.0-flash")
prompt = ChatPromptTemplate.from_messages([
("system", "You are an email assistant. Use the send_email "
"tool when asked to send emails."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])
agent = create_tool_calling_agent(llm, [send_email], prompt)
executor = AgentExecutor(agent=agent, tools=[send_email])
result = executor.invoke({
"input": "Send a welcome email to [email protected] (John Smith) "
"with a friendly onboarding message."
})
print(result["output"])ปรับใช้กับ Vertex AI Agent Engine (เวอร์ชันใช้งานจริง)
สำหรับการปรับใช้ในเวอร์ชันใช้งานจริง ให้ใช้ Vertex AI Agent Engine:
import vertexai
from vertexai import agent_engines
vertexai.init(project="YOUR_PROJECT_ID", location="us-central1")
# Deploy the agent
remote_agent = agent_engines.create(
agent_engine=agent,
requirements=["requests"],
display_name="Mailtarget Email Agent",
)
# Query the deployed agent
session = remote_agent.create_session(user_id="user-1")
response = remote_agent.stream_query(
session=session,
message="Send a test email to [email protected]"
)
for chunk in response:
print(chunk)
🚀 สิ่งที่คุณเพิ่งสร้างขึ้น
เอเจนต์ AI ของ Vertex ของคุณสามารถทำงานเหล่านี้ได้โดยอัตโนมัติ:
เขียนเนื้อหาอีเมลในรูปแบบหรือโทนเสียงใดก็ได้
ออกแบบอีเมล HTML ด้วยการจัดรูปแบบที่เหมาะสม
ส่งผ่านโครงสร้างพื้นฐานการผลิตของ Mailtarget
ติดตามการเปิดและการคลิกโดยอัตโนมัติ
ขยายขนาดบนรันไทม์ที่จัดการโดย Google Cloud
เกิดอะไรขึ้นข้างในระบบ?
คุณบอกเอเจนต์ว่าจะส่งอะไรและส่งไปให้ใคร
Vertex AI จะเขียนเนื้อหา สร้าง HTML และเรียกใช้เครื่องมือ Mailtarget
Mailtarget จะจัดการเรื่องการส่ง การตรวจสอบสิทธิ์ (SPF/DKIM/DMARC) การติดตาม และความน่าเชื่อถือ
ขั้นสูง: เพิ่มเครื่องมือ Mailtarget เพิ่มเติม
ขยายขีดความสามารถของเอเจนต์ของคุณด้วยฟังก์ชันเพิ่มเติม:
def list_sending_domains() -> dict:
"""List all verified sending domains in Mailtarget."""
response = requests.get(
"https://transmission.mailtarget.co/v1/domain/sending",
headers={"Authorization": f"Bearer {os.environ['MAILTARGET_API_KEY']}"}
)
return response.json()
def create_template(template_id: str, name: str, html: str) -> dict:
"""Create a reusable email template in Mailtarget."""
response = requests.post(
"https://transmission.mailtarget.co/v1/template",
headers={
"Authorization": f"Bearer {os.environ['MAILTARGET_API_KEY']}",
"Content-Type": "application/json"
},
json={"id": template_id, "name": name, "html": html}
)
return response.json()
def send_template_email(to_email: str, to_name: str,
subject: str, template_id: str,
substitution_data: dict) -> dict:
"""Send an email using a stored Mailtarget template."""
response = requests.post(
"https://transmission.mailtarget.co/v1/layang/transmissions",
headers={
"Authorization": f"Bearer {os.environ['MAILTARGET_API_KEY']}",
"Content-Type": "application/json"
},
json={
"to": [{"email": to_email, "name": to_name}],
"from": {"email": "[email protected]",
"name": "Your Company"},
"subject": subject,
"templateId": template_id,
"substitutionData": substitution_data
}
)
return response.json()
Thêm các công cụ này vào danh sách công cụ của người đại lý để có đầy đủ khả năng quản lý email.
การแก้ไขปัญหา
ปัญหา | แก้ไข |
| ตรวจสอบว่าคีย์ API ของ Mailtarget ถูกต้องและตั้งค่า MAILTARGET_API_KEY แล้ว |
| ตรวจสอบว่าอีเมลผู้ส่งใช้โดเมนของบริษัทที่ได้รับการยืนยันแล้ว |
อีเมลไม่มาถึง | ตรวจสอบโฟลเดอร์สแปม ตรวจสอบระเบียน SPF/DKIM สำหรับโดเมนของคุณ |
ข้อผิดพลาดด้านสิทธิ์ของ Vertex AI | ตรวจสอบให้แน่ใจว่า aiplatform.googleapis.com เปิดใช้งานอยู่และคุณได้รับการตรวจสอบสิทธิ์แล้ว |
เอเจนต์ไม่เรียกใช้เครื่องมือ | ตรวจสอบ docstring ของฟังก์ชัน — Gemini ใช้เพื่อตัดสินใจว่าจะเรียกใช้เครื่องมือเมื่อใด |
โดเมนอีเมลฟรีถูกปฏิเสธ | ใช้โดเมนของบริษัทของคุณ — Gmail, Yahoo, Outlook ฯลฯ ไม่ได้รับอนุญาต |

