Thursday, December 23

Mailing with Joomla: HTML and Text

Joomla has its own built in version of PHPMailer called JMail. Hey, we set all these email configurations in the setup, might as well use those.

$mail =& JFactory::getMailer();
$config =& JFactory::getConfig(); //this is where we grab info from the config file
$mail->useSMTP(
$config->getValue('config.smtpauth'),
$config->getValue('config.smtphost'),
$config->getValue('config.smtpuser'),
$config->getValue('config.smtppass')
);
Now we can define more stuff for useSMTP, but unless your server settings aren't standard, you don't really need to. But it's all in the config file too, easy way to see it all is just to print_r($config).

Now, I want to send this email as html because it looks nicer, but I have to make sure to set a text only one for those people reading the emails on their phones, etc.
$HTMLmsg = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Message title</title>
</head>
<body>
<p>Here is some text. It looks nice.</p>
</body></html>';
$textmsg = "Here is some text. It looks ok.";
$mail->setBody(&HTMLmsg);
$mail->isHTML(true);
$mail->AltBody=&textmsg;
AltBody is a variable, not a function. It was carried over from PHPMailer. Setting this automatically sets the email to multipart/alternative. It is read by clients that don't have HTML email (like my phone), but clients that can read HTML will see the normal body.

Notice I used double quotes on $textmsg. This is in case I want to use newlines, php will escape them (I mentioned this earlier).

We're almost done. Just need a recipient, sender and subject. We can use another Joomla config value to set who the email is sent from.
$mail->addRecipient('example@example.com');
$mail->setSender($config->getValue('config.mailfrom'));
$mail->setSubject('Test email');
Well this seems good right? I spent a few minutes with this all set up, testing, but no email was sent. Oh yeah, forgot one very important line.
$mail->send();
Resources:

Friday, December 10

Ajax Username Check

I don't know about you, but I love cool ajaxy stuff. A lot of times, it's really simple to add this to a form. All I'm doing is checking to see if the requested username is in use or not.

So first, the html is the simple part. Just give your input box and id and add a section after it to hold the results:
<input type="text" name="uname" id="uname"><span id="checkuname">
Then I make a little php file with the to check for the username.
$u = $_GET['u'];
require_once('db.php');
if($u <> ""){ //we don't want to display anything if the field is blank
$checkname = query("SELECT * FROM table WHERE username = '".$u."'");
if(num_rows($checkname) <> 0){
$desc = 'Already in use';
}else{
$desc = 'Ok to use!';
}
}
echo $desc;
I style $desc to be something nice looking, or use an image of a checkbox or something, depending on how the rest of the form looks.

So then in the scripts, I look in the uname id and display in the checkuname id.
$('#uname').keyup(function(){
$('checkuname').load('checkuser.php?u='+$(this).val(), function(response, status, xhr){
if(status == "error"){
var msg = "There was an error: ";
$('#checkuname').html(msg + xhr.status + " " + xhr.statusText);
}
});
});

Thursday, December 9

Style a button as a link

I have an invisible form that I want the user to submit with a link, keeping the illusion that it's not a form. I know you could submit the form with javascript (onclick="document.formName.submit();), but then if the user doesn't have javascript enabled, I'd still have to use a noscript tag and they'd see it as a form button. The solution is to style the button with CSS. something like:

.sublink {
color: #00f;
background-color: transparent;
text-decoration: underline;
border: none;
cursor: pointer;
}
Then the html would be:
<input type="submit" class="sublink" value="Click this">