mailify

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 18, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

Mailify

Mailify is a Go package for validating email addresses by checking their format, verifying the existence of MX records for the domain, and attempting to connect to the mail servers using SMTP.

CLI

You can use this as a cli tool on your local machine by installing the mailify cli tool

go install github.com/yourusername/mailify-cli@latest

For more details, checkout CLI DOCS

Installation

To install the package, run:

go get github.com/adarsh-jaiss/mailify

Usage

Creating a Client

To create a new client, use the NewClient function:

client, err := mailify.NewClient("sender@example.com")
if err != nil {
    log.Fatalf("Failed to create mailify client: %v", err)
}
Validating an Email Address

To validate an email address, use the ValidateEmail method:

result, err := client.ValidateEmail("recipient@example.com")
if err != nil {
    log.Fatalf("Failed to validate email: %v", err)
}

fmt.Println("Validation result:", client.FormatValidationResult("recipient@example.com", result))
Getting Mail Servers

To get the mail servers for a domain, use the GetMailServers method:

mailServers, err := client.GetMailServers("example.com")
if err != nil {
    log.Fatalf("Failed to get mail servers: %v", err)
}

fmt.Println("Mail servers:", mailServers)

To get the mail servers for a recipient email, use the GetMailServersFromReceipientEmail method:


mailServers, err := client.GetMailServersFromReceipientEmail("recipient@example.com")
if err != nil {
    log.Fatalf("Failed to get mail servers: %v", err)
}

fmt.Println("Mail servers:", mailServers)
Validate all the email addresses in an Excel file

This section demonstrates how to validate all email addresses in an Excel file using the ProcessAndValidateEmailsViaExcel method. The method takes the path to the Excel file and the sender's email as parameters. If there is an error during the processing of the file, it will print an error message and terminate the execution.

Example usage:

	err = client.ProcessAndValidateEmailsViaExcel("emails.xlsx",client.SenderEmail)
	if err!= nil {
         fmt.Printf("Error processing file: %v\n", err)
         return
	}
Example

Here is a complete example demonstrating how to use the package : check examples

Documentation

Overview

Package mailify provides functionalities for handling and processing emails. This package includes features for sending, receiving, and managing email communications in a secure and efficient manner.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	SenderEmail string
}

Client represents an email client with a sender email address.

func NewClient

func NewClient(SenderEmail string) (*Client, error)

NewClient creates a new Client instance with the provided sender email address. It returns a pointer to the Client and an error, if any.

Parameters:

  • SenderEmail: A string representing the sender's email address.

Returns:

  • *Client: A pointer to the newly created Client instance.
  • error: An error if there is any issue during the creation of the Client.

func (*Client) ExtractDomainFromEmailAddress

func (c *Client) ExtractDomainFromEmailAddress(receipientEmail string) (string, error)

ExtractDomainFromEmailAddress extracts the domain part from the given email address. It takes a recipient email as input and returns the domain as a string. If the email format is invalid, it returns an error.

Parameters:

receipientEmail (string): The email address from which to extract the domain.

Returns:

string: The domain part of the email address.
error: An error if the email format is invalid.

func (*Client) FormatValidationResult

func (c *Client) FormatValidationResult(recipientEmail string, result *ValidationResult) string

Helper function to format validation results FormatValidationResult formats the validation result of an email address into a human-readable string.

Parameters:

  • email: The email address that was validated.
  • result: A pointer to a ValidationResult struct containing the validation details.

Returns:

A formatted string summarizing the validation results, including the email address, validation status,
presence of MX records, catch-all status, and any error message.

func (*Client) GetHostname

func (c *Client) GetHostname() (string, error)

getHostname gets the fully qualified domain name for HELO command GetHostname attempts to retrieve the fully qualified domain name (FQDN) of the current host. It first tries to get the hostname using os.Hostname(). If that fails, it returns a fallback hostname "verifier.local". If successful, it then attempts to resolve the IP addresses associated with the hostname using net.LookupIP(). If that fails, it returns the hostname. If successful, it performs a reverse DNS lookup on the first IPv4 address found using net.LookupAddr(). If that succeeds and returns at least one name, it returns the first name with the trailing dot removed. If all attempts fail, it returns the hostname with ".local" appended.

func (*Client) GetMailServers

func (c *Client) GetMailServers(domain string) ([]string, error)

func (*Client) GetMailServersFromReceipientEmail

func (c *Client) GetMailServersFromReceipientEmail(email string) ([]string, error)

GetMailServersFromReceipientEmail extracts the domain from the given email address and retrieves the mail servers associated with that domain.

Parameters:

email (string): The recipient's email address.

Returns:

[]string: A slice of mail server addresses.
error: An error object if there was an issue extracting the domain or retrieving the mail servers.

func (*Client) GetSMTPServer

func (c *Client) GetSMTPServer(mailServer string) (*SMTPDetails, error)

GetSMTPServer attempts to find an available SMTP server for the given mail server. It performs a DNS lookup to get all IP addresses (both IPv4 and IPv6) associated with the mail server, and then tries to connect to common SMTP ports (587, 25, 465) on each IP address.

If a connection is successfully established, it returns the SMTP server details including the server name, port, protocol, and IP address. If no available SMTP servers are found, it returns an error.

Parameters:

  • mailServer: The domain name of the mail server to look up.

Returns:

  • *SMTPDetails: A struct containing the details of the SMTP server if found.
  • error: An error if no available SMTP servers are found or if there is a lookup failure.

func (*Client) ProcessAndValidateEmailsViaExcel added in v1.1.0

func (c *Client) ProcessAndValidateEmailsViaExcel(filename string, senderEmail string) error

ProcessAndValidateEmails reads an Excel file, validates emails, and writes results back ProcessAndValidateEmailsViaExcel processes and validates emails from an Excel file. It reads the email addresses from the specified Excel file, validates each email, and writes the validation results back to the Excel file in a new column.

Parameters:

  • filename: The path to the Excel file containing the email addresses.
  • senderEmail: The email address of the sender (not used in the current implementation).

Returns:

  • error: An error if any issue occurs during the process, otherwise nil.

The function performs the following steps:

  1. Opens the specified Excel file.
  2. Reads all rows from the first sheet ("Sheet1").
  3. Creates a map of headers from the first row.
  4. Adds a new column header for email validation results if it doesn't exist.
  5. Iterates over each row, validates the email address, and writes the validation result to the new column.
  6. Saves the modified Excel file with the validation results.

The function prints progress and summary information to the console.

func (*Client) TryConnectingSMTP

func (c *Client) TryConnectingSMTP(smtpDetails *SMTPDetails, recipientEmail, localName string, useTLS bool) (*ValidationResult, error)

TryConnectingSMTP attempts to establish an SMTP connection and validate an email address. It performs the following steps: 1. Creates a new validation result indicating the domain has MX records. 2. Creates a new dialer with a timeout. 3. Formats the address based on IP version (IPv4 or IPv6). 4. Handles connection based on the port (SMTPS or plain/STARTTLS). 5. Creates an SMTP client. 6. Performs HELO/EHLO command. 7. Initiates STARTTLS if available and not already using TLS. 8. Sends MAIL FROM command. 9. Sends RCPT TO command. 10. Interprets the response to determine if the email address is valid.

Parameters: - smtpDetails: Details of the SMTP server (IP address, port, server name). - senderEmail: The email address of the sender. - recipientEmail: The email address of the recipient to be validated. - localName: The local name to use in the HELO/EHLO command. - useTLS: A boolean indicating whether to use TLS.

Returns: - A pointer to a ValidationResult struct containing the validation outcome. - An error if any step in the process fails.

func (*Client) ValidateEmail

func (c *Client) ValidateEmail(recipientEmail string) (*ValidationResult, error)

ValidateEmail validates the recipient's email address by checking its format, verifying the existence of MX records for the domain, and attempting to connect to the mail servers using SMTP.

Parameters:

  • recipientEmail: The email address of the recipient to be validated.
  • senderEmail: The email address of the sender.

Returns:

  • *ValidationResult: A struct containing the validation result, including whether the email is valid, if MX records were found, and any error messages.
  • error: An error object if an error occurred during the validation process.

The function performs the following steps:

  1. Checks if the recipient email contains an "@" symbol and splits it into local and domain parts.
  2. Retrieves the MX records for the domain.
  3. Gets the local hostname for the HELO command.
  4. Attempts to connect to each mail server using SMTP, first without TLS and then with TLS if the initial attempt fails.
  5. Returns the validation result and any errors encountered during the process.

type SMTPDetails

type SMTPDetails struct {
	// Server is the address of the SMTP server.
	Server string
	// Port is the port number on which the SMTP server is listening.
	Port string
	// Protocol is the protocol used by the SMTP server (e.g., "SMTP", "SMTPS").
	Protocol string
	// UsedTLS indicates whether TLS is used for the connection.
	UsedTLS bool
	// IPAddress is the IP address of the SMTP server.
	IPAddress string
}

SMTPDetails holds the details required to connect to an SMTP server.

type ValidationResult

type ValidationResult struct {
	// IsValid indicates whether the email address is valid.
	IsValid bool
	// IsCatchAll indicates whether the domain has a catch-all address.
	IsCatchAll bool
	// HasMX indicates whether the domain has MX records.
	HasMX bool
	// ErrorMessage contains any error message encountered during validation.
	ErrorMessage string
	// SMTPDetails contains the SMTP server details used for validation.
	SMTPDetails *SMTPDetails
}

ValidationResult represents the result of an email validation check.

Directories

Path Synopsis
cli
cmd
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL