Advanced webcoding: PHP – Emails


Some time ago I got a question about PHP. More specific, on how to send a mail from a PHP script without the preconfigured e-mail client to pop up.
You may want to read my previous entries about PHP first, unless you have experience with PHP ;-).

This may be interesting for any type of online application you are developing, so let me just write it down in a few lines.
It’s remarkably simple.

First of all, I’ll explain this by using the WAMP tool. Since it’s the most simple, easy to set up, solution. And you should know, before you can actually use the PHP mail functionalities, you should configure your PHP server.

You do this as follows, locatie the PHP.ini file (click the wamp icon, navigate to PHP and select PHP.INI). Locate this part in the file:

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25

This is the default setup. Change following values:
SMTP = whateversmtpserver.smtp.com
smtp_port = 25 (or whichever port your SMTP server uses)

Once this is done, save the file and restart the wamp services.

Right, for the PHP mail functionalities. I’ll just quickly explain how this works, without going into detail. If someone requests it, I’ll be more specific in a later post.

Use the mail() function to send a mail. Take for example following script:

<?php
$message = “My god I love this blog!”;
$message = wordwrap($message, 70);
mail(‘sharp@example.com’, ‘Sharpsblogorama Rulez’, $message);
?>

The message contains… the.. message.. right.. okay. Moving on..
As you can see I also used the wordwrap function, this breaks our lines after 70 chars.
The function mail() requires 3 parameters, the first is the e-mail adress you’ll be sending the mail to, the second is the Subject and the third is the actual message.

Save this file and run it. The mail has now been send…

Who ever said PHP was hard?

Let me know if you have problems with this ;-)..

And for the love of god, Don’t mailbomb me.. :P

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: