Send emails using the GMAIL SMTP server

Send emails from command line using the GMAIL smtp server.

Make sure you turn on Allow less secure apps settings for the email id you are going to use. (GMAIL, Account, Security, Less secure app access)

Connect using the starttls option in openssl to convert the plain connection to encrypted…

`openssl s_client -starttls smtp -connect smtp.gmail.com:587 -crlf -ign_eof`

or connect to a ssl sockect directly…

`openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof`

Type in commands
`EHLO localhost`

after that, authenticate to the server using the base64 encoded username/password

AUTH PLAIN AG15ZW1haWxAZ21haWwuY29tAG15cGFzc3dvcmQ=

to get this from the commandline:

`echo -ne ‘\00user@gmail.com\00password’ | base64 AHVzZXJAZ21haWwuY29tAHBhc3N3b3Jk`

Example session:

openssl s_client -connect smtp.gmail.com:465 -crlf -ign_eof
[... lots of openssl output ...]
220 mx.google.com ESMTP m46sm11546481eeh.9
EHLO localhost
250-mx.google.com at your service, [1.2.3.4]
250-SIZE 35882577
250-8BITMIME
250-AUTH LOGIN PLAIN XOAUTH
250 ENHANCEDSTATUSCODES
AUTH PLAIN AG5pY2UudHJ5QGdtYWlsLmNvbQBub2l0c25vdG15cGFzc3dvcmQ=
235 2.7.0 Accepted
MAIL FROM:
250 2.1.0 OK m46sm11546481eeh.9
rcpt to:
250 2.1.5 OK m46sm11546481eeh.9
DATA
354 Go ahead m46sm11546481eeh.9
Subject: it works

yay!
.
250 2.0.0 OK 1339757532 m46sm11546481eeh.9
quit
221 2.0.0 closing connection m46sm11546481eeh.9
read:errno=0

Reference: https://stackoverflow.com/questions/11046135/how-to-send-email-using-simple-smtp-commands-via-gmail

Advertisements

Deploying Flask App on AWS ec2 instance with Mysql without using elastic bean stalk

 

Prerequisites:

Your flask app works on your local machine and the code is hosted(Github).

Steps

1.

Launch a Ec2 instance and log in using your key-pair or ssh keys. Make sure you store the key.pem file when you create the instance.

$ ssh -i key.pem ubuntu@52.43.196.102  # (Your public IP)

If you get an error like:

Permissions 0644 for ‘Downloads/ishaan-key.pem’ are too open.

It is required that your private key files are NOT accessible by others.

This private key will be ignored.

Then change your permission on your key.pem file

$ chmod 700 key.pem

 

2. Before you can install python specific packages if you are using MYsql yo need to install mysql.(Ubuntu 16.04)

  • $ sudo apt-get update
  • $ sudo apt-get install mysql-server
  • $ sudo mysql_secure_installation

3.

Once you are logged in clone your code repository from Github.

$ git clone https://github.com/username/repo_name.git

Now once you have the code install pip (For ubuntu)

sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get install python-pip

Now that you have installed pip use your requirements.txt file to install all the python packages your flask app needs.

$ pip install -r requirements.txt

If you do have a requirements.txt you can create one from your local machine

$ pip freeze > requirements.txt

Install all the packges required by your flask app using

$ pip install -r requirements.txt

(If you get an error

EnvironmentError: mysql_config not found

Then run this command

sudo apt-get install libmysqlclient-dev

 

4.

Install gunicorn if you were not using it on your local machine

$ pip install gunicorn

We’ll need a web server installed on our instance, because we’re going to forward requests from port 80 to our Flask app running internally. It is best practice to have a web server handle our port 80 requests, because the Flask application server (Gunicorn) we are using is designed to serve Python code, but not ideal for handling port 80 requests from the Internet.

  • sudo apt-get update
  • sudo apt-get install nginx

Nginx Configaration:

$ sudo rm /etc/nginx/sites-enabled/default

$ sudo nano /etc/nginx/sites-available/flask_app

Paste in the below: Here We are passing on to port 8000 as gunicorn by default binds to 8000.

server {
    listen       80;
    server_name  your_public_dnsname_here;

    location / {
        proxy_pass http://127.0.0.1:8000;
    }
}

 

Now we can enable the site (by creating a symlink), test our configuration and restart to allow our changes to take place.

  
$ sudo ln -s /etc/nginx/sites-available/flask_app /etc/nginx/sites-enabled/
$ sudo nginx -t
$ sudo systemctl restart nginx 
  

And finally, let’s start our Gunicorn process to serve our Flask app:

$ gunicorn app:app 

This should be it. You can create a gunicorn service so that it starts up when the server boots.

Reset Mysql Root password

If you are getting this error:

Error: Access denied for user ‘root’@’localhost’ (using password: YES)

Basic commands to start/stop/restart mysql from Command line

On OS X to start/stop/restart MySQL pre 5.7  from the command line:

 sudo /usr/local/mysql/support-files/mysql.server start
 sudo /usr/local/mysql/support-files/mysql.server stop
 sudo /usr/local/mysql/support-files/mysql.server restart

These are the steps you need to follow:

  1. Stop Mysql server (
    sudo /usr/local/mysql/support-files/mysql.server stop
  2. Start mysql with the following option “/path/to/mysqld –skip-grant-tables ”
     sudo /usr/local/mysql/support-files/mysql.server start--skip-grant-tab
    les
  3. Connect to Mysql ($ mysql)
  4. Update the password

    update user set authentication_string=password(‘NEW PASSWORD’) where user=’root’;

    5. Restart mysql server