All posts by Bruno Amaral

Send e-mail with inline image attachment

Since I start coding in PHP that one of the key feature were send and simple e-mail by a form. This can be done with many API’s that exist to work with the e-mail system. With the evolution of the Internet, sending e-mails with a design and not only with text has become to be normal and widely used. I’ve used many solutions like php mailer, xpert mailer, version 2 and 4, code igniter e-mail class, but still using the version 2 of xpert mailer, also known as XPM2.This older version, already with lack of support by its developers is simpler and easier to include in one project and do everything I need to send e-mail, from normal text based e-mail to rich content e-mail in html and attachments.

There are many ways for doing that by using html e-mails. This task isn’t as simple as it looks like, because html e-mail has some limitations and some rules that we can’t break if we want to see our e-mail well formed. One of the principal limitations is use a table based layout instead of a div one.

While using rich e-mails we face some difficulties and issues. The way we put images on the html e-mail is important. We’ve to choices to doing that.

  1. The first one is use full path to an image that is store in a web server accessible everywhere. This has some problems because we may not have space on a web server to put the files. Another problem is the limitation of the domain. If we want to change our domain or use our script in another one, we’ve to change all image and file path.
  2. Another way to put files on the e-mail, especially images, is putting them inline of the e-mail like an embed attachment. This solves the problems described but is much hard to use and to understand. Some API’s are simplifying the programmers work by offering a simpler to use Mime content inside an e-mail. The XPM2 have this feature and turns it simply to use when we do it right.

The code shown below is simple and sends an example e-mail with an inline image.

// XPM2 with attach inline example
require(“xpm2-0.1/smtp.php”);

// Create XPM instance
$mail = new smtp();

// Select delivery mode
$mail->Delivery(‘local’);

// Set timeout
$mail->TimeOut(10);

// Set e-mail priority
$mail->Priority(‘Normal’);

// Set from e-mail
$mail->From(“test@ws4.org”,”Test”);

// Set to e-mail
$mail->addTo(“protech@ws4.org”,”ProTech”);

// Set body content has HTML dining is encoding
$mail->Html(‘<html><head><title>Test</title></head><body>Test<br /><img src=”image.jpg” alt=”Inline image” /></body></html>’,’ISO-8859-1′, ‘base64’);

// Attach file to the e-mail
$mail->AttachFile(‘image.jpg’, false, ‘autodetect’, ‘inline’ );

// Send e-mail and set the subject and it’s encoding
$sent = $mail->Send(‘Hello World!’,’ISO-8859-1′, ‘base64’);

// Check the status and see the result if error
echo $sent ? ‘Success’ : $mail->result;

The method attach file is used to add files to the e-mail, either as a normal attachment and an inline one. If we see the parameters we find a disposition that allows us to define the type of attachment. Using the second parameter we can set a name or use the full file name. Is this example, I attach an image as inline content and use it in html. It’s so easy and clean.

This API also allow you to choose the better way to send your e-mails using local, relay and client letting you use your servers e-mail layer or connecting to an SMTP server. Other feature is the possibility to connect to a throw POP3.

See the documentation about XPM2

I haven’t use the XPM4 because I had some header encoding issues and haven’t tried to fix the problem yet. Besides, I still use XPM2 because allows sending e-mail the way I like and is too small and easy to deploy and integrate with some frameworks, like Code Igniter.

Detect cookie support with JavaScript

Cookies, also known as web cookies, HTTP Cookies or just cookies, are a mechanism that allow the store of that in the client’s browser. This information is sent by a server to the web browser and then sent back without change each time the browser access that server. Cookies are used for several purposes such authentication, tracking or just to hold specific information, such users preferences in a web site. The idea and name was inspired in the well-known concept “magic cookie” from UNIX.

This mechanism is so common between developers that most of the times we forget to check the client browser support for them. When we develop an application that uses cookies we must check their support to avoid some errors. This can be done in several ways, such with server side scripting. The code bellow shows a simple way to check cookies support with JavaScript.

<script type=”text/javascript”>
var cookiesON = false;
if (typeof navigator.cookieEnabled==”undefined”)
{
//if other than IE4+ and NS6+
var date = new Date();
document.cookie = “checkCookie=”+date.getTime();
cookiesON = (document.cookie.indexOf(“checkCookie”)!=-1)? true : false
}
else
{
//if IE4+ or NS6+
cookiesON = (navigator.cookieEnabled)? true : false;
}

//Do something
if (cookiesON)
alert(‘Cookies ON’);
else
alert(‘Cookies OFF’);
</script>

One problem that comes with the web development is the need to develop cross-browser applications. This way, in the above code you see two different ways of check cookie support in JavaScript.

  • The first one checks cookies on browsers different from IE4+ and NS6+ by trying to put a dummy value in the cookie and read it. If we could read it, is because cookies are enabled. There is no problem by storing the dummy value because this one is removed when the browser is closed. This is the behavior when isn’t specified a expire date.
  • We could use the same verification for all browsers. However, browsers like IE4+ or NS6+ have the cookie support information built-in with a navigator property “cookieEnabled”.

Play MOD movies made with a JVC camcorder

Some time ago I bought a JVC camcorder that writes in an HDD or an SD card. The camcorder is great and has a magnificent image and sound quality, even if compared with some 5Mb digital cameras.

However when we try to see the movies that it produces face a problem. The camcorder record movies in a MOD file that is, in fact, a MPEG2 encoding. This way we can rename the MOD file to MPG and get the movie playing. The rename action couldn’t be the solution. The problem is that MPEG2 is a format that is not supported by default by some operating systems. For example, i have a Windows XP with the software that the camcorder bring, the Cyberling Power Director, and i can manage free my movies but when I’ve only my laptop with Windows Vista with me i couldn’t see the recorded movies. The solution is to install a package with the correct codec in Vista. I installed a Vista Codec Package and it starts working ok.

When you have the software (Cyberlink) that read MOD files you only have to used it and convert the movies to a more supported format. This software allows the user to make a complete movie with the recorded scenes. This can be done by getting all MOD files, select those that we want and produce a movie in a AVI or MPEG1 formats.

When you don’t have this software you can rename the MOD file to MPG and see it. Don’t forget to install the correct codec to play it. In some cases, the file doesn’t play at all or it can only be without sound.

This is not proved and it’s my own experience trying to play and produce my videos.

WordPress blogs SEO

Today there are so many blogs that if you want to let people know that you have one, you must try to optimize it. Sometimes this optimization is right in front of our eyes and small changes can make a difference. In this post i’ll give some advice to improve your wordpress blogs index position in search engines. However, this are not proved actions to improve blog indexing, but are some SEO improvements discussed in SEO world.

Permalinks

Permalink is the permanent URL of your blog pages. This is the URL that visitors will see and that other web bloggers can use to refer a post of your blog. WordPress allow the configuration of the URL format. It offers different options, some of then very ugly and not well accepted by search engines. However, you can define your own format. This way you can either have a good and simple URL and a format that the spiders (search engines bots) like and extract your keywords from it.

The format that i choose to my blog and that i recommend is the “/%category%/%postname%”. The result is a simple and friendly URL that is parsed by the spiders. This bots get the keywords in the URL using them to define your pages value. If you’ve a URL with the date of the post or with a code what the meaning of the URL?

Visit WordPress permalink page

Post and category slug

To complete the benefit of permalinks, the WordPress allows you to choose the name that appear in the URL for each post or category. With post and category slugs you can define a much friendly URL. By default, the WordPress put your post title separated with ‘-‘. Some keywords are not necessary and can be removed. This way you can define your category and post parts of the URL format defined above.

Meta Tag

Another step to improve your blog value to the spiders, is defining some meta tags. Beside the definition of the page title, one of the most important information, we must define the description, the keywords, and other tags.

Bold fields

Some SEO professionals says that we must put strong tags on the keywords that we want to highlight either to the users an to the spiders.

WordPress Slug fields and permalinks

The search engines are the primary source of visitor to a web site. This engines have some crawlers that analyse our blog frequently and check our content. However, the content is not everything. One thing that matter to the spiders (name called to the engines that analyse our site) is the web site url’s. Like i said in WordPress blogs SEO, this engines don’t like query strings with many parameters and give more importance to first keywords in url.

WordPress have some fields that must be used to provide a crawler and user friendly urls. The called post slug and category slug can be filled with url friendly names that are well parsed by the crawlers. For example, if we create a post with the title like “WordPress Slug fields and permalinks”, wordpress will transform in a postname for url “wordpress-slug-fields-and-permalinks”, this if we have our permalink well optimized. With this slug fields we can set the best keywords reducing the url size and rising the crawlers position. In this post i set my post slug to “slug-permalink”, making the url with the keywords wanted with wordpress (category), slug and permalink.

Using this fields and with a optimized permalink strcture and a good category organization we can rise our index pontuation and, who knows, get more visitors.