How to send an email using PHP?

  • Added:
  • |
  • In: Basic PHP

I am using PHP on a website and I want to add emailing functionality.

I have WAMPSERVER installed.

How do I send an email using PHP?

This Question Has 9 Answeres | Orginal Question | user590849

For most projects, I use Swift mailer these days. It's a very flexible and elegant object-oriented approach to sending emails, created by the same people who gave us the popular Symfony framework and Twig template engine.

Basic usage :

require 'mail/swift_required.php'; $message = Swift_Message::newInstance() // The subject of your email ->setSubject('Jane Doe sends you a message') // The from address(es) ->setFrom(array('' => 'Jane Doe')) // The to address(es) ->setTo(array('' => 'Frank Stevens')) // Here, you put the content of your email ->setBody('<h3>New message</h3><p>Here goes the rest of my message</p>', 'text/html'); if (Swift_Mailer::newInstance(Swift_MailTransport::newInstance())->send($message)) { echo json_encode([ "status" => "OK", "message" => 'Your message has been sent!' ], JSON_PRETTY_PRINT); } else { echo json_encode([ "status" => "error", "message" => 'Oops! Something went wrong!' ], JSON_PRETTY_PRINT); } 

See the official documentation for more info on how to use Swift mailer.

Full code example..

Try it once..

<?php // Multiple recipients $to = ','; // note the comma // Subject $subject = 'Birthday Reminders for August'; // Message $message = ' <html> <head> <title>Birthday Reminders for August</title> </head> <body> <p>Here are the birthdays upcoming in August!</p> <table> <tr> <th>Person</th><th>Day</th><th>Month</th><th>Year</th> </tr> <tr> <td>Johny</td><td>10th</td><td>August</td><td>1970</td> </tr> <tr> <td>Sally</td><td>17th</td><td>August</td><td>1973</td> </tr> </table> </body> </html> '; // To send HTML mail, the Content-type header must be set $headers[] = 'MIME-Version: 1.0'; $headers[] = 'Content-type: text/html; charset=iso-8859-1'; // Additional headers $headers[] = 'To: Mary <>, Kelly <>'; $headers[] = 'From: Birthday Reminder <>'; $headers[] = 'Cc:'; $headers[] = 'Bcc:'; // Mail it mail($to, $subject, $message, implode("\r\n", $headers)); ?> 

If you are interested in html formatted email, make sure to pass Content-type: text/html; in the header. Example:

// multiple recipients $to = '' . ', '; // note the comma $to .= ''; // subject $subject = 'Birthday Reminders for August'; // message $message = ' <html> <head> <title>Birthday Reminders for August</title> </head> <body> <p>Here are the birthdays upcoming in August!</p> <table> <tr> <th>Person</th><th>Day</th><th>Month</th><th>Year</th> </tr> <tr> <td>Joe</td><td>3rd</td><td>August</td><td>1970</td> </tr> <tr> <td>Sally</td><td>17th</td><td>August</td><td>1973</td> </tr> </table> </body> </html> '; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: Mary <>, Kelly <>' . "\r\n"; $headers .= 'From: Birthday Reminder <>' . "\r\n"; $headers .= 'Cc:' . "\r\n"; $headers .= 'Bcc:' . "\r\n"; // Mail it mail($to, $subject, $message, $headers); 

For more details, check php mail function.

You can use a mail web service such as Postmark, Sendgrid etc.

Sendgrid vs Postmark vs Amazon SES and other email/SMTP API providers?

Edit: I just use the Google Gmail API now. I had trouble sending reminder email to my employer's organization due to strict filters. But Gmail works as long as you don't spam people.

Also look into the PEAR mail package Pear Mail Page

It seems to be a little more robust than the standard mail() function that is built in (if the standard function isn't adequate).

Here is an excerpt from this page showing how it is used. PEAR Mail send() usage

<?php include('Mail.php'); $recipients = ''; $headers['From'] = ''; $headers['To'] = ''; $headers['Subject'] = 'Test message'; $body = 'Test message'; $smtpinfo["host"] = ""; $smtpinfo["port"] = "25"; $smtpinfo["auth"] = true; $smtpinfo["username"] = "smtp_user"; $smtpinfo["password"] = "smtp_password"; // Create the mail object using the Mail::factory method $mail_object =& Mail::factory("smtp", $smtpinfo); $mail_object->send($recipients, $headers, $body); ?> 

Using PHP's mail() function it's possible. Remember mail function will not work in Local server.

<?php $to = ''; $subject = 'the subject'; $message = 'hello'; $headers = 'From:' . "\r\n" . 'Reply-To:' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); ?> 


this is very basic method to send plain text email using mail function.

<?php $to = ''; $subject = 'This is subject'; $message = 'This is body of email'; $from = "From: FirstName LastName <>"; mail($to,$subject,$message,$from); 

Try this:

<?php $to = ""; $subject = "My subject"; $txt = "Hello world!"; $headers = "From:" . "\r\n" . "CC:"; mail($to,$subject,$txt,$headers); ?> 

You could also use PHPMailer class at .

It allows you to use the mail function or use an smtp server transparently. It also handles HTML based emails and attachments so you don't have to write your own implementation.

The class is stable and it is used by many other projects like Drupal, SugarCRM, Yii, and Joomla!

Here is an example from the page above:

<?php require 'PHPMailerAutoload.php'; $mail = new PHPMailer; $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = ';'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = ''; // SMTP username $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted $mail->From = ''; $mail->FromName = 'Mailer'; $mail->addAddress('', 'Joe User'); // Add a recipient $mail->addAddress(''); // Name is optional $mail->addReplyTo('', 'Information'); $mail->addCC(''); $mail->addBCC(''); $mail->WordWrap = 50; // Set word wrap to 50 characters $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; } 

I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.

Connect Social With PHPAns