To send email from your php script either you can use PHP’s built-in mail function or third party library like phpmailer or swiftmailer.
In this article you will find both methods to send email.
Send Mail using php mail function
<?php $to = "[email protected]"; //mail to $from = "[email protected]"; //your email $subject = "This test mail sent using php mail function"; //mail Subject $msg = "Your email body will go here"; //your message body $headers = "MIME-Version: 1.0"; $headers.= "Content-type: text/plain; charset=iso-8859-1"; $headers.= "From: {$from}"; $headers.= "Reply-To: {$from}"; $headers.= "Subject: {$subject}"; $headers.= "X-Mailer: PHP/".phpversion(); if(mail($to, $subject, $msg, $headers)) { echo "Mail Sent"; } else { echo "Unable to send mail"; } ?>
Send mail using phpmailer (recommended method)
You can download phpmailer script from http://phpmailer.worxware.com/
<?
require_once('../class.phpmailer.php');//include phpmailer class
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSendmail(); // telling the class to use SendMail transport
$mail->ContentType = 'text/plain';
$mail->Body = "Your email body will go here"; //your message body
$mail->AddReplyTo("[email protected]","First Last"); //reply to address
$mail->SetFrom('[email protected]', 'First Last'); //from address
$address = "[email protected]"; //to address
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic"; //mail Subject
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
phpmailer library is easy to use and highly configurable. Unlike php built-in mail function we can easily send html mail or email with attachments without having to play around with headers.
