April 23, 2008

PHP E-mail with Pear

Filed under: The Internet Author: Cody

The php mail() function is wonderful in its simplicity but as anyone starting out in php soon discovers, that simplicity has a price - you have no ability to use more advanced functions such as SMTP. For anyone who sends out newsletters and gets half of them back from spam filters you’ll know this can be a problem.

This is where an email class such as Pear can help out; so on to the next problem. - What if you are building an application to put on a third party server where Pear mail isn’t installed? All of the tutorials on the Net tell you to go and install it and then re-boot your server; this is all well and good unless the on/off button is 6000 miles away on a shared host that is not particularly keen on turning it off for you.

Fortunately you don’t have to go to those lengths and you can incorporate all the Pear classes you need with a simple call to php ini_set() in your script.

The classes:

Mail:

This is the main mail class and it is needed for all the classes below. It can be downloaded at http://pear.php.net/package/Mail

Once downloaded extract it to temporary folder and rename the main folder to ‘mail’. Upload the folder and all the subfolders in it to the same directory on your server that contains your script for sending emails.

Net_SMTP:

This is the class you will need if you want to send e-mails via smtp, download at http://pear.php.net/package/Net_SMTP

Once downloaded and extracted you will need to change the main folder name to ‘Net’ (make sure you use a capital ‘N’.) Upload it, and put it inside the ../mail/Mail folder as shown in Fig. 1. Once again leaving all sub folders intact.

Path treeFIG. 1

Net_Socket:

This class is required to connect to smtp servers, download at; http://pear.php.net/package/Net_Socket

Once downloaded and extracted you will need to change the main folder name to ‘Net’ (make sure you use a capital ‘N’.) Upload it, and put it inside the ../mail/Mail/Net folder as shown in Fig. 2. Once again leaving all sub folders intact.

Path treeFIG. 2.

Now you have the basic classes in place to send smtp emails all you need now is the code to use it.

Create a new php file called ‘mail.inc.php’ and insert the following code;

<?php
ini_set(
“include_path”, (
“public_html/yourdomain.tld/mail/” .
PATH_SEPARATOR .
ini_get(”include_path”)
)
);

require_once ‘mail/Mail.php’;
require_once ‘mail/mime.php’;

$host = “mail.yourdomain.tld“;
$username = “youraddress@yourdomain.tld“;
$password = “yourpassword“;

?>

You will need to change the parts in red to the settings for the email account you wish to use. Save the file and upload it to the root of your server.

Finally, use the following code in your script that sends emails.

//send the email

require_once (’mail.inc.php’);

$message = new Mail_mime();
//Create a plain text part for the email

//Single new line character

$newline = “\n”;

/Double new line characters

$doubleNewline = “\n\n”;

$text = ‘Testing new email class‘.$doubleNewline.
This is some text to test the new classes‘.$doubleNewline.
Name: Mr Tester‘.$newline.
Hello! This is my test email‘.$doubleNewline;

//Create an html version of the email

$html = ‘<html>
<head>
<title>Testing new email class</title>
</head>
<body>
<h1><center>Testing new email class</center></h1>
<p>This is some text to test the new classes</p>
<p>Name: Mr Tester</p>
<p>Hello! This is my test email</p>

</body>
</html>’;

$message->setTXTBody($text);
$message->setHTMLBody($html);
$body = $message->get();
$from = “My Script<noreply@mydomain.tld>”;
$to = “Someone <address@someplace.com>”;
$subject = “Test Email“;

$extraheaders = array(’From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$headers = $message->headers($extraheaders);

$smtp = Mail::factory(’smtp’,
array (’host’ => $host,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo(”<p>” . $mail->getMessage() . “</p>”);
} else {
echo(”<p>Message successfully sent!</p>”);
}

Once again the code in red should be configured to suit your purposes.

And that’s it!

November 10, 2007

CSS An Introduction

Filed under: The Internet Author: Cody

So you’ve heard all about CSS and the wonderful things it can do and you fancy giving it a whirl. What you will need first is a basic understanding of what it is and how to use it.

CSS is a way to control the styles on your website and to keep content separate from layout as much as possible. So how do we use CSS?

There are three ways to incorporate CSS into your web page; you can either put the code inside the <head> section; you can create a separate page containing only the CSS and link to it from the <head> section of your pages or you can place it in the middle of your HTML as and when necessary.

To declare inside your web page you should use the following inside the <head> section;

<style type="text/css">
<!--

Place your CSS here

-->
</style>

The above is all well and good for a small website with only a few pages but for anything larger you should consider creating an external style sheet (a separate document that contains your CSS). All you need to do then is to link to it from each of your pages. The code to placed inside your head section for doing that is;

<LINK href="style.css" rel="stylesheet" type="text/css">

The final way, which is useful if you have a ‘one off’ CSS instruction to use is to place it inside your HTML; this is known as an inline style.

The best way to visualise these three methods of incorporating your style sheet is with a few examples.

First the declaration inside the <head> section of each of your web pages;

<html>
<head>
<title>Foo Widgets</title>

<style type="text/css">
<!--
img {
border: none;
}
h1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}

->
</style>

</head>

As you can see from the above you are declaring the styles of certain aspects of your web page. These will act as a kind of default throughout the page, for example everywhere you use the <h1> tag the text will appear in bold size 14 Verdana.

With an external stylesheet you would simply put your CSS in a document and save it with a relevant name such as style.css, then link to it from each of your web pages with the code used earlier. For example, if your external stylesheet contained the following;

p {
font-family: Verdana, Tahoma, Arial, sans-serif;
font-size: 7pt;
color: #ffffff;
margin:5px;
}

All of the text within <p> tags on your website would have the above style associated with them.

Finally an example of inline styles;

<h1 style="margin-top:5px; margin-bottom:5px">A Header</h1>


I hope this brief introduction to CSS has wet your appetite and sparked your imagination to the possibilities; if you find it useful let me know.

October 3, 2007

Saving The Universe With CSS

Filed under: The Internet Author: Cody

CSS Tips And TricksI’ve thought to myself 800 billion times today, I must stop exaggerating, but what is all this kerfuffle about CSS and why are there 3 different ways to spell kerfuffle?

There seems to be some very big claims about CSS, I’ve heard people refer to it as a language and a replacement for HTML, I’ve heard others say that tables are dead and that CSS is the future of the internet. Let’s first of all deal with what it is and it isn’t and then maybe more people will embrace it and reap the benefits.

CSS is not a language nor is it a replacement for HTML; it’s merely a style sheet. Tables are unbeatable for displaying data in rows and columns but are poorly suited for designing the layout of a page and CSS is not the future - but it’s a good start.

The fundamentals: (not a pop band from the 60’s)

Many people resist CSS; - “what’s the point when I can make nice websites the way I always have?” - Well, here’s the deal; CSS helps you to create nice clean web designs where content is largely separate from the code. Why is that important? Well, pages load faster, things appear where you intended them to appear, development is much quicker and if you ever need to add something to your page you won’t spend 4 years scrolling through endless tr, td, th or font tags to find the place to put it.

The bits and bobs: (not a pop band from the 70’s)

How does it save you time you ask? It saves you time because you build a style sheet that can be used over and over again, change the odd colour here, the odd font size there and voilĂ , use it on your next project. I will write a short tutorial for beginners and when it’s done I’ll put a link HERE. (If that’s not a link it’s because I haven’t done it yet), (Also, if you read this in a months time and it’s still not a link it’s probably just that I forgot, so leave a comment to remind me), (if it is a link then ignore the last two bracketed comments and go directly to ‘Finally;’).

Finally;

I have no idea why there are 3 different ways to spell kerfuffle but I’m pretty sure I will soon be top of Google for the search term - ‘why are there 3 different ways to spell kerfuffle?’ :)

better order some more bandwidth !

Older Posts »

Powered by WordPress