Created On: November 19, 2024, Last Updated On: November 19, 2024

Flask Mail

Public

GoDaddy Domain and GMAIL setup

By Ozzie Ghani3 new





Step-by-Step Guide to Set Up GoDaddy for Gmail

1. Log In to Your GoDaddy Account

  1. Visit GoDaddy.
  2. Log in using your credentials.

2. Access Your Domain Settings

  1. Click on your profile icon in the top-right corner.
  2. Select "My Products" from the dropdown menu.
  3. Under the Domains section, locate ozzieghani.com and click "DNS".

3. Add/Edit DNS Records for Email Setup

You need to set up DNS records, including MX, SPF, and optionally DKIM records, to work with Google's email services.


4. Set Up MX Records

  1. Delete any existing MX records that do not point to Google's mail servers.
  2. Add the following MX records:
    • Host: @
    • Points to: ASPMX.L.GOOGLE.COM
    • Priority: 1
    • TTL: 1 Hour
  3. Add additional MX records:
    • Host: @, Points to: ALT1.ASPMX.L.GOOGLE.COM, Priority: 5, TTL: 1 Hour
    • Host: @, Points to: ALT2.ASPMX.L.GOOGLE.COM, Priority: 5, TTL: 1 Hour
    • Host: @, Points to: ALT3.ASPMX.L.GOOGLE.COM, Priority: 10, TTL: 1 Hour
    • Host: @, Points to: ALT4.ASPMX.L.GOOGLE.COM, Priority: 10, TTL: 1 Hour

5. Set Up SPF Record (TXT Record)

  1. Look for existing TXT records for SPF. If there is one, you may need to modify it.
  2. Add a new TXT record:
    • Host: @
    • TXT Value: v=spf1 include:_spf.google.com ~all
    • TTL: 1 Hour
  3. Note: If you already have an SPF record, combine it with the existing one, making sure there is only one SPF record per domain.

6. Set Up DKIM (DomainKeys Identified Mail)

To set up DKIM, you’ll need to enable it from your Google Workspace admin console if you are using Google Workspace.

  1. Log in to the Google Workspace Admin Console: Visit admin.google.com and sign in with your admin account.
  2. Navigate to Security Settings:
    • Go to Apps > Google Workspace > Gmail > Authenticate Email.
  3. Generate a DKIM Key:
    • Click on Generate New Record and select ozzieghani.com as the domain.
    • Choose the key bit length (2048 is recommended).
    • Generate the key and copy the DKIM TXT record details.
  4. Add the DKIM Record in GoDaddy:
    • In GoDaddy, click "Add" to create a new TXT record.
    • Host: google._domainkey
    • TXT Value: Paste the DKIM value generated in the Google Admin Console.
    • TTL: 1 Hour


7. Set Up DMARC Record (Optional but Recommended)

DMARC helps protect your domain from email spoofing.

  1. Add a new TXT record:
    • Host: _dmarc
    • TXT Value: v=DMARC1; p=none; rua=mailto:postmaster@ozzieghani.com
    • TTL: 1 Hour
  2. Note: You can adjust the DMARC policy (p=none) to p=quarantine or p=reject once you're sure that your emails are correctly authenticated.

8. Verify Your DNS Changes

  1. Once you've added the records, it may take some time for the DNS changes to propagate (usually within an hour).
  2. Use online tools like MXToolbox to check if your MX, SPF, and DKIM records are correctly set up.

9. Test Email Sending

  • Try sending an email using your Flask app and check if it’s successfully delivered to the recipient's inbox.



Sample Flask App



from flask import Flask
from flask_mail import Mail, Message

app = Flask(__name__)


# Configure Flask-Mail
app.config['MAIL_SERVER'] = 'smtp.gmail.com'  # Replace with your SMTP server
app.config['MAIL_PORT'] = 587  # Common port for TLS
app.config['MAIL_USE_TLS'] = True  # Use TLS
app.config['MAIL_USE_SSL'] = False  # Do not use SSL
app.config['MAIL_USERNAME'] = 'info@example.com'  # Replace with your email
app.config['MAIL_PASSWORD'] = '****************q'  # Replace with your email password 
app.config['MAIL_DEFAULT_SENDER'] = 'Cool Company <info@example.com>'  # Replace with your default sender email
app.config['MAIL_DEBUG'] = True

mail = Mail(app)





@app.route("/")
def send_email():
    try:
        # Print debug information
        print("Attempting to send an email...")

        # Create a message
        msg = Message(
            subject="Test Email",
            sender=app.config['MAIL_DEFAULT_SENDER'],
            recipients=["abc@example.com"],  # Replace with the recipient's email
            body="This is a test email sent from a Flask app using Flask-Mail.",
            reply_to=app.config['MAIL_DEFAULT_SENDER'],
            bcc=['xyz@example.com']
        )
        
        # Send the email
        mail.send(msg)

        print("Email sent successfully!")  # Log success
        return "Email sent successfully!"
    except Exception as e:
        # Log and return the error
        print(f"Failed to send email: {str(e)}")
        return f"Failed to send email: {str(e)}"


if __name__ == "__main__":
    app.run(debug=True)