When sending mail in PHP its always best to use an SMTP server rather than the mail() function, and the ideal candidate for the job is PHPMailer.

Assuming you have an SMTP server (usually mail.yourdomain.com) and a username and password (to authenticate as some servers won't allow you to send via SMTP without it) sending mail with PHPMailer is easy as pie.

For websites that send lots of emails with different information it's a good idea to setup some email templates.

In this example we will create a folder called "email_templates" and will create a file called "register.html" which will contain the login information when someone registers to our site:

<h1>Account Details</h1>
<p>Thank you for registering on our site, your account details are as follows:<br>
Username: %username%<br>
Password: %password%

Now here is the code to send this as an email, assuming you have the username and password of the user signed up.

// Include the PHPMailer class

// Retrieve the email template required
$message = file_get_contents('email_templates/register.html');

// Replace the % with the actual information
$message = str_replace('%username%', $username, $message);
$message = str_replace('%password%', $password, $message);

// Setup PHPMailer
$mail = new PHPMailer();

// This is the SMTP mail server
$mail->Host = 'mail.yourdomain.com';

// Remove these next 3 lines if you dont need SMTP authentication
$mail->SMTPAuth = true;
$mail->Username = 'admin@yourdomain.com';
$mail->Password = 'blah';

// Set who the email is coming from
$mail->SetFrom('admin@yourdomain.com', 'Website Admin');

// Set who the email is sending to

// Set the subject
$mail->Subject = 'Your account information';

//Set the message

// Send the email
if(!$mail->Send()) {
 echo "Mailer Error: " . $mail->ErrorInfo;