The tempmail
package provides a simple way to interact with temporary email services. It allows you to create a temporary email address, retrieve emails, and extract information from them.
To use the tempmail
package, you need to import it in your Go program:
import "github.com/Mixtre/tempmail"
To create a temporary email address, you need to specify an alias and a domain. The TempMail
function returns a *Mail
struct that represents the temporary email address:
func TempMail(alias string, domain string) (*Mail, error)
alias
: The alias for the temporary email address (e.g. "myalias")domain
: The domain for the temporary email address (e.g.tempmail.MailtoPlus
,tempmail.FexpostCom
, etc.)
Available domains:
tempmail.MailtoPlus
tempmail.FexpostCom
tempmail.FexboxOrg
tempmail.MailboxInUa
tempmail.RoverInfo
tempmail.ChitthiIn
tempmail.FextempCom
tempmail.AnyPink
tempmail.MerepostCom
Example:
mail, err := tempmail.TempMail("myalias", tempmail.MailtoPlus)
if err!= nil {
log.Fatal(err)
}
You can print the temporary email address using the String()
method of the Mail
struct:
fmt.Println("Temporary Email Address:", mail.String())
This will output the temporary email address in the format alias@domain
.
To retrieve emails, you can use the GetMails
method of the Mail
struct. It returns a MailResponse
struct that contains a list of emails:
func (mail *Mail) GetMails() *MailResponse
No parameters are required for this function.
Example:
mailResponse := mail.GetMails()
if mailResponse == nil {
log.Fatal("Error retrieving emails")
}
fmt.Println(mailResponse)
To get the IDs of the emails, you can iterate over the MailList
field of the MailResponse
struct:
for _, email := range mailResponse.MailList {
fmt.Println("Email ID:", email.MailID)
}
You can extract information from emails using the GetMail
method of the Mail
struct. It returns a MailDetail
struct that contains information about the email:
func (mail *Mail) GetMail(id int) *MailDetail
id
: The ID of the email to retrieve (e.g. 123)
Example:
mailDetail := mail.GetMail(123)
if mailDetail == nil {
log.Fatal("Error retrieving email")
}
You can extract the following information from the MailDetail
struct:
Subject
: The subject of the emailFrom
: The sender of the emailTo
: The recipient of the emailText
: The text content of the emailHtml
: The HTML content of the emailAttachments
: A list of attachmentsDate
: The date the email was received
You can print the email content using the String()
method of the MailDetail
struct:
fmt.Println("Email Content:")
fmt.Println("Subject:", mailDetail.Subject)
fmt.Println("From:", mailDetail.From)
fmt.Println("To:", mailDetail.To)
fmt.Println("Text:", mailDetail.Text)
fmt.Println("Html:", mailDetail.Html)
fmt.Println("Attachments:")
for _, attachment := range mailDetail.Attachments {
fmt.Println("Attachment ID:", attachment.AttachmentID)
fmt.Println("Attachment Name:", attachment.Name)
}
This will output the email content in a human-readable format.
To get the IDs of the attachments, you can iterate over the Attachments
field of the MailDetail
struct:
for _, attachment := range mailDetail.Attachments {
fmt.Println("Attachment ID:", attachment.AttachmentID)
}
You can extract attachments from emails using the GetAttachmentLink
method of the Mail
struct. It returns a URL that you can use to download the attachment:
func (mail *Mail) GetAttachmentLink(AttachmentID int, attachments []Attachment, MailID int) string
AttachmentID
: The ID of the attachment to retrieve (e.g. 123)attachments
: A list of attachments for the emailMailID
: The ID of the email that the attachment belongs to (e.g. 123)
Example:
attachmentLink := mail.GetAttachmentLink(123, mailDetail.Attachments, 123)
if attachmentLink == "" {
log.Fatal("Error retrieving attachment link")
}
fmt.Println("Attachment Link:", attachmentLink)
This will output the URL of the attachment.