I am new to PHP, but have a decent grasp of things (have not learned classes yet).
The question:
Which to choose? PHPMailer or mail() for my
PHPMailer is my choice because it allows me to send SMTP e-mails to google without installing any libraries or configuring a mailserver, that way I don't have to worry about spam-related problems.
As Pascal MARTIN mentioned, sending an email isn't as straight forward and easy as some people just assume it is. To answer your questions directly. Yes PHPMailer does do some validation, but it's not super-advanced, but should be enough for your uses. And PHPMailer will save you some time depending on what custom functions you will need. Some things to consider though:
In addition to just PHPMailer Zend_Mail is a good one to check out to (it's part of the Zend Framework). However that may be a bit much for a simple contact form.
This will maybe not really answer all your questions, but it won't hurt either, I guess...
Whatever you want to do, I would not go with mail()
: sending a mail is not such an easy task, and using an existing library/framework will always be a good idea : it will solve many problems you probably have not even thought about -- even if you don't need to send lots of mails.
About your specific questions, maybe other answers will say something else and/or get your more informations, but any "good" library created to send mails should deal with those kind of problems... Else, you should probably search for another library ^^
Still, testing a couple of dumb non-addresses will allow you to be 100% sure ;-)
Another solution to be quite sure is to check the source of the library ;-)
In the source of version 2.2.1, you'll find stuff like this :
class.phpmailer.php
, function AddAnAddress
, line 413, you'll see this :
if (!self::ValidateAddress($address)) {
$this->SetError($this->Lang('invalid_address').': '. $address);
if ($this->exceptions) {
throw new phpmailerException($this->Lang('invalid_address').': '.$address);
}
echo $this->Lang('invalid_address').': '.$address;
return false;
}
And it seems this function is used by the other functions that add an address... So, I suppose there's some kind of email-addresses validation ;-)
That'll answer at least one of your questions ^^
PHPMailer is not the only solution that exists, btw ; there are plenty of others, like, for instance :
Here is all I could think of in one sitting, forgive me if there are any glaring omissions.
Advantages to using PHP's built-in mail function, no external library/wrapper:
Advantages to using an external library/wrapper:
$message->addAttachment($file);
without having to play around with headers, etc.If I can think of anything else, I'll be sure to add it.