*_jemarea_*

Website Development

  • Home
  • Articles
    • All articles
    • Linux
    • WordPress
    • Codeigniter
    • PHP
    • JavaScript
    • MySql
  • About
  • Contact

July 2, 2018 By jerome Leave a Comment

Ubuntu – Catch All Emails from Localhost

So you are working as a web developer on your local machine and as it happens, you will have to test emails sent to potential customers or administrators or both.

If you have only 1 recipient or two, that’s ok, but what if you have to test various recipients or even test newsletters….

… or worst, what if you are downloading an existing application onto your local installation and this application suddenly sends 1000 newsletters because it had been set to do so?!

Well, here is the solution:

Catch all these emails and direct them to only 1 account on your local machine

And this is how it goes:

  1. Install postfix on your computer if it isn’t installed yet:In a terminal, run these commands (do not copy the dollar sign)
    $ sudo apt-get update
    $ sudo apt install postfix

    Type of mail configuration: choose “Internet Site”
    Mail Name: localhost (we will check that later again)

  2. Now open the postfix config file
    $ sudo vi /etc/postfix/main.cf

    Make sure that myhostname is set to localhost only (sometimes the configuration process adds another string to it)

    Add the following line at the very end of the main.cf file:

    canonical_maps = regexp:/etc/postfix/canonical

    You should now have something like this:

    # See /usr/share/postfix/main.cf.dist for a commented, more complete version
    
    
    # Debian specific:  Specifying a file name will cause the first
    # line of that file to be used as the name.  The Debian default
    # is /etc/mailname.
    #myorigin = /etc/mailname
    
    smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
    biff = no
    
    # appending .domain is the MUA's job.
    append_dot_mydomain = no
    
    # Uncomment the next line to generate "delayed mail" warnings
    #delay_warning_time = 4h
    
    readme_directory = no
    
    # See https://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
    # fresh installs.
    compatibility_level = 2
    
    # TLS parameters
    smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
    smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
    smtpd_use_tls=yes
    smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
    smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
    
    # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
    # information on enabling SSL in the smtp client.
    
    smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
    myhostname = localhost
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    mydestination = $myhostname, MYCOMPUTER, localhost.localdomain, , localhost
    relayhost =
    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = all
    inet_protocols = all
    canonical_maps = regexp:/etc/postfix/canonical
    

    (where MYCOMPUTER is the name you have given to your computer)

    Save this file

  3. Now create a new file (if it doesn’t exist yet)
    $ sudo vi /etc/postfix/canonical

    and add the following at the end (or as only line)

    /.*@.*/ MYNAME@localhost

    (where MYNAME is your user name. You can find out running this command in the terminal: whoami)

    Save and close.

  4. run the following command:
    $ sudo postmap canonical

    If you are getting an error like this
    “postmap: fatal: open canonical: No such file or directory”
    .. just repeat the command above. It should be ok the second time.

  5. Create a localhost mail account in your mail client (example Thunderbird)To do so, go to the Account Settings of your mail client, click on:
    – Account Actions
    – Add other Account
    – Choose “Unix Mailspool” (Movemail)
    – Set your (or a specific) name
    – Add to Email Address the address you have set into /etc/postfix/canonical
    – click next and finish
  6. To test if this is working:Go to your terminal and add the following command
    date | mail -s Test your.real.email@something.com(where your.real.email@something.com is your actual or anybody’s email address)If this command doesn’t work, you will very probably be prompted to add the mailutils from repository. Do so.

That’s it! I hope it works for you and enjoy coding!

 

Filed Under: Linux, Management, PHP, Ubuntu

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · jemarea.com