Web Hosting Talk







View Full Version : Beginner question: how to make an email server?


vk101
02-17-2005, 11:49 PM
Thanks for reading this post!

I am wondering how sites go about automatically sending you an email message. It's automated for sure, because it happens when you do things like if you haven't logged on in a certain number of days, if it's a deadline to submit an online payment (like they do at your online bank), or just after you've registered.

Not only can these emails be sent automatically, but the content is specific to a particular user's account.

How do sites do this? What programs, programming languages, databases, etc do you need to be able to do this? What is the conceptual way in which this works?

What things do you need besides what I've described above? Let's say you have the whole code written, what is the process by which it is sent out, like don't you need a POP or IMAP server - how exactly do you 'get' one?

I hope you can tell by reading this message that I have no idea what I'm talking about, and that I basically want to know how to go from my current state to having the ability to send out my first email from my own server?

Thanks so much in advance!

Carp
02-18-2005, 12:10 AM
I'm not quite sure what you're talking about but here I go....

Say you have an online bank account, Your balance is below $20 and they automatically send you an email. They probably run a CRON job which is a script that is run every x minutes or days or whatever they set it to.

The script will go through the database and look for accounts with >$20 and email them.

If it's a big company, they probably have their own mail server. There are many mail servers you can downlaod and run on your computer. Try a Google search for "Mail Server" or goto www.downloads.com

vk101
02-18-2005, 12:45 AM
Thanks for your reply!

So can I simply download a mail server program and start sending mail? Are these mail-server programs basically something that you install on your site's server and are able to start sending things out right away, or what?

In general terms, how would you go about integrating this mail server with the rest of your site to be ready to send out email?

Is a mail server a ready-to-send mail program or is it just a programming environment that you have to set up and configure, or what is it? When you want to send mail would you do the programming (including the CRON script you talked about) inside this mail server program or in your PHP or in your Apache or your Linux or what?

I guess the specifics would be beyond my understanding at this point, but I'm hoping somebody could describe in conceptual terms for a beginner how somebody would go about setting up their systems to send mail.

I know my question probably sounds really dumb, but I'm on a whole different universe in terms of how little I know about all this web stuff... I don't really understand what exactly a mail server is and how one would go about getting it ready to send mail.

Thanks a million in advance!

VolkNet
02-18-2005, 12:51 AM
Also if you go to hotscripts.com or something like that and look under email management scrripts you can find something like what you're looking for. :) (hopefully) maybe not tho/.

giraffedata
03-05-2005, 08:07 PM
This is a pretty open-ended question. You say you're new to web stuff, but how about computer programming in general? Could you make a computer display a certain message on the screen when certain things happen (maybe when a certain day of the month arrives)? If not, that would be the first thing to learn, and sending mail would be a further step. In the example you gave of a bank, it isn't a web server that sends those emails. It's a different kind of computer.

I own a computer that sends me mail at various times for various reasons. And it's got nothing to do with the web.

You don't need a mail server to send mail. A mail server is what _receives_ mail. And sometimes it relays outbound mail, but I don't think you want to get that complex right now.

What you need instead is a mail client, which is technically called an SMTP client. Any Unix system comes with several of them; the simplest one is called "mail" Another is called "sendmail". You can invoke these from a PHP script. But assuming the PHP script runs only when someone visits it with a browser, that wouldn't be much of an automated mailer.

"cron" is the name of a subsystem that's present on virtually every Unix system, including those whose primary purpose is to be a web server. But it's totally separate from the web server. Cron's purpose is to run certain programs at certain identified clock times, e.g. every Wednesday at 3:00. If the program is a mail client (e.g. 'mail') or a program that invokes a mail client, then cron will send an email.

Just how you tell Cron what to run when varies from one system to another.
There are various versions of Cron and various front ends that pass schedule information to Cron.

To get a grip on the problem, so you at least can formulate a more specific question, I would recommend reading a book on Linux, paying particular attention to the parts on email, cron, and web serving.

Burhan
03-06-2005, 02:14 AM
Here is how it works.

Generally, all user information is stored in a database somewhere. There are templates for each type of email (for example, 30 day expiration notice on your domain).

Each template is parsed and placeholders (such as {name}) are replaced with actual information from the database.

There are scripts that run at set intervals (also called cron jobs) that query the database for information, then send out emails if the user's account matches the criteria.

Almost any database server can be used to do such tasks, the same goes for email servers. As far as languages go, it really depends on your environment. For example, if your servers are all running Windows, you would be better off programming in ASP.NET, C#, or some other Windows-specific programming language (so that you can take full advantage of the architecture). For UNIX/Linux based servers, the choice really varies by programmer. Some choose PERL, others Python, yet some even use PHP.

The choice of language depends on the platform (as mentioned above) and how "fit" the language is for a certain task. If your chosen language doesn't have email functionality, then you will be hampered because you would have to deal with opening sockets and sending information to the server directly. If your chosen language cannot open sockets; then you can't use it for such an application.

To bring this whole concept full circle, here is an example PHP script that checks to see if a user's account has expired, then sends out a template email.

First, the template :

Hello {first_name} {last_name}:

This is to inform you that your account number {acct_number} has expired today!

Regards,
Billing Department


Now the PHP script:

<?php

// -- Set some configuration information

$email_from = "Reminder Services <reminders@company.com>";
$email_subj = "Your account has expired!";

// -- Connect to the database
$status = mysql_connect("localhost","supa-user","supa-pass");
if (!$status) { die(mysql_error()); }
$status = mysql_select_db("user-database");
if (!$status) { die(mysql_error()); }

//Check which users are expired. Users that are expired
//have their timestamp less than today

$query = "SELECT `fname`, `lname`, `acct`, `expires`, `email` FROM ";
$query .= "`users` WHERE `expires` < ".date();

$result = mysql_query($query);
if (!$result) { die(mysql_error()); }

//Did we get any users?
if (mysql_num_rows($result) > 0)
{
//Yes, we have results

//Load the template
$template = file("template.tmpl");

//What we will replace in the template
$targets = array("{first_name}","{last_name}","{acct_number}");

while ($row = mysql_fetch_assoc($result))
{
//Build our replacement list
$replacements = array($row['fname'],$row['lname'],$row['acct']);

//Parse our template
$body = str_replace($targets,$replacements,$template);

//Send our email
mail($row['email'],$email_subject,implode("\n",$body),"From: ".$email_from."\r\n");
}
}
?>


Finally, the entry in your crontab file. This entry is what tells the server to run the php script every day at midnight:


0 0 * * * php -q emailer.php


Hopefully this helps

vk101
03-06-2005, 07:23 PM
Thanks for the great response!

So in terms of actually getting the email sent out, I wouldn't need an email server? I could just program a script like you described and it would send out the email automatically?

Don't you need some kind of 'email account' to be able to send out email, or is it just an SMTP client like was described in a previous post?

Thanks a lot!

VolkNet
03-06-2005, 07:46 PM
If your host supports it, you should be able to mail it out using just the script.

the part of the code:
//Send our email
mail($row['email'],$email_subject,implode("\n",$body),"From: ".$email_from."\r\n");


will send it out.
:)

vk101
03-06-2005, 08:39 PM
Oh really? That's pretty neat actually.

What if you're on your own host? What kinds of things would you need to do to ensure you can support it?

Thanks.

Burhan
03-07-2005, 02:41 AM
Almost all webhosts will provide you an email account along with your webhosting space. This means that there is an email server running at your host that is configured for your domain.

To check if you are able to send emails via PHP, you can run this simple script:


<?php

$to = "me@mydomain.com";
$from = "testing@mydomain.com";
if (mail($to,"Testing","Testing","From: ".$from."\r\n"))
{
echo "Email was sent to the server successfully, check ".$to." for a message from ".$from."<br />";
} else {
echo "Something went wrong with the script, and it wasn't able to send an email";
}
?>