Send Email using Python and 1and1

The code you use to send email depends on whether the code is running on 1and1.com or on an external machine.

If you are on an external machine like your desktop, you use an SMTP script to send through your 1and1 email account. The following script called sendmail.py sends an email with python.

#!/usr/bin/python
import smtplib
from email.mime.text import MIMEText
me = "steve@example.com"
you = "sam@comcast.net"
password = "mypassword"
msg = MIMEText("This is a test message body.")
msg['Subject'] = 'Test message'
msg['From'] = me
msg['To'] = you
session = smtplib.SMTP("smtp.1and1.com", 587)
session.login(me, password)
session.sendmail(me, you, msg.as_string())
session.quit()

Run it like this: python sendmail.py

On 1and1 you need a mailbox type email. These types of emails have a password that you use in the script. It is easy to create a mailbox, if you don’t have one, by by logging into 1and1 and using their web interface.

If you copy this script to your web space on 1and1 and run it there, it will fail. 1and1 does not allow SMTP within 1and1. To send an email when running on 1and1 use this script:

#!/usr/bin/python
import os
mail_to = "sam@comcast.net"
mail_from = "steve@example.com"
subject = "test message"
header = """From: {0}
To: {1}
Subject: {2}
""".format(mail_from, mail_to, subject)
msg = header + "a test from me"
sendmail = os.popen("/usr/lib/sendmail -t", "w")
sendmail.write(msg)
sendmail.close()

Just open sendmail and write to it!

Install WordPress on 1and1

I just set up WordPress and I figured my first post would be about installing it on 1and1.

WordPress blogging software is used by 15% of the top million websites, up about 6% this year! WordPress is free open source software and it has an active community of developers. You can select from hundreds of pre-made templates and selecting one for your site only takes a couple of clicks.

1and1 is a popular and cheap web hosting service. A basic plan is only $5.00 a month.

These instructions were tested on a local Ubuntu 10.4 system installing on 1and1 basic plan with Linux hosting. Similar steps apply if you are running a local Windows system. You can use the same steps for 1and1 advanced and business plans, or there are alternate steps, see below. If you have the Windows based package, 1and1 provides a way for you to switch.

Basically to install WordPress you download WordPress to your system, create a database on 1and1, edit one configuration file, then upload all the files to 1and1. Here are the details.

Download WordPress

The latest WordPress comes as a zip file from wordpress.org. To download WordPress open a terminal window and type the following:

cd
cd tmp
wget http://wordpress.org/latest.zip

Now unzip the file. It will create a folder called wordpress inside your tmp folder. Then delete the zip file to save space.

unzip latest.zip
rm latest.zip

Now rename the wordpress folder to “blog” (or whatever you want) and rename the wp-config-sample.php file to wp-config.php. The name you choose is the name that users will use to get to your blog. If you use “blog” for example, the url will be http://sample.com/blog/.

mv wordpress blog
cd blog
mv wp-config-sample.php wp-config.php

Find your 1and1 domain name and user name

Your domain name and user name are needed to copy files to 1and1.  To find out what they are follow these steps:

  • In your browser go to http://1and1.com/
  • Click on Customer Login (near the top right of the page) and enter your credentials.
  • Click on your package.
  • Find your .us domain name. It will be near the top and it looks something like: s123456789. onlinehome.us

Note: I could not figure out how to select the domain name for copy and paste. So instead, I selected everything on the page with Ctrl+A, then pasted that into a text editor. There I could select the domain name.

While you are still logged into 1and1 find your user name and set up a password for it.

  • Click on Domains & Web Space.
  • Click on FTP Account.
  • Find your user name. It will look something like: u12345678.
  • Edit the entry and add a password, if not already done.

Note: Don’t add a password if you share the account with someone else and it has already has been set up, because they will no longer be able to ftp. Instead ask the other user for the ftp password.

Note: The password for SSH and the password for FTP are identical. If you change one, it changes the other.  The password to log into your 1and1.com account is different than the SSH and FTP one.

Create WordPress Database

While you are still connected to 1and1 create the database for WordPress. Write down the database information for use when editing wp-config.php later. Here are the steps:

  • Click on MySQL Database Set Up and Configuration.
  • Click on New Database .
  • Write down the database name, host name, user name and database password. They should look similar to:
  1. db123456789 — database name
  2. db123456789.db. 1and1.com — host name
  3. dbo123456789 — user name
  4. mydbpassword – database password

You are done with the 1and1 web interface.

Edit wp-config.php

Edit wp-config.php using your favorite text editor changing the database name, user name, remote host and database password to the names you just wrote down in the last step. Also change the salt values to unique values.

gedit wp-config.php

The lines should look something like this:

define(‘DB_NAME’, ‘db123456789’);
define(‘DB_USER’, ‘dbo123456789’);
define(‘DB_PASSWORD’, ‘mydbpassword’);
define(‘DB_HOST’, ‘db123456789.db. 1and1.com’);

For the salt values change the strings “put your unique phrase here” to something else.

define(‘AUTH_KEY’, ‘12345 zzxcv asdf qwerty zxcv’);
define(‘SECURE_AUTH_KEY’, ‘put 1234 unique phrase 1235’);

Upload Files to 1and1

Now all the files are configured and ready to upload to 1and1.

The 1and1 basic plan supports sftp but not ssh or scp. Using the command line with sftp is too tedious. Instead upload the blog folder to 1and1 using Filezilla. Filezilla is a free GUI based program for ftp. For Windows go to http://filezilla-project.org/ and download the Filezilla client, for Ubuntu, Filezilla can be downloaded and installed with:

 sudo apt-get install filezilla

To run Filezilla type:

filezilla

Once Filezilla is running enter the connection information to your 1and1 account. Select the menu File > Site Manager and enter the following information:

  • Host (domain name) = s123456789.onlinehome.us
  • Server type = SFTP
  • Logon type = Normal
  • User = u12345678
  • FTP Password = myftppassword
  • Comments = this is my account on 1and1

Then press the Connect button.

While still in Filezilla, transfer the blog folder to your account by following these steps:

  1. In the left local site window select the folder to transfer from your local machine, ~/tmp//blog.
  2. In the right Remote site window select the destination,  /.
  3. Right click the source folder and select upload. The files will be uploaded.

Almost done. Run the Install Script by switching to your browser and entering the address to the install script, something like this:

http://sample.com/blog/wp-admin/install.php

Write down your WordPress password.

Now WordPress is installed!

Visit your blog at the URL:

http://sample.com/blog/

Alternate Steps for Advance and Business Packages

The 1and1 advanced and business packages support ssh which makes installing a little easier (but the basic steps work too).

Instead of downloading to your local machine, configuring then uploading, you login to 1and1 using ssh, download directly to your 1and1 account, then configure in place, no need to upload.

To start, open a terminal window and log into your 1and1 account. If you run Windows locally, you need to set up Putty first. Get your user name and host names by logging into 1and1 as noted above under Find your 1and1 domain name and user name.

ssh u12345678@ s123456789.onlinehome.us

Download the latest version of WordPress to your 1and1 account and unzip it. This will create the folder called wordpress with all the files inside it. Then delete the unneeded zip file.

wget http://wordpress.org/latest.zip
unzip latest.zip
rm latest.zip

Rename the wordpress folder to “blog” (or whatever you want) and rename the wp-config-sample.php file to wp-config.php.

mv wordpress blog
cd blog
mv wp-config-sample.php wp-config.php

Log into your main 1and1.com account and create a database. Follow the same steps as above under Create WordPress Database.

Switch back to the terminal window and edit wp-config.php changing the database name, user name, remote host and database password to the names you just wrote down in the last step.

nano wp-config.php

Run the Install Script by switching to your browser and entering the address to the install script, something like this:

http://sample.com/blog/wp-admin/install.php

Write down your WordPress password.

Now WordPress is installed!

Visit your blog at the URL:

http://sample.com/blog/