Sending mail is very necessary & required feature in almost every website/web application. There are two ways to send mail using PHP. After reading this article you will be able to send mail using both methods.
- Built-in method
- Third party library like phpmailer (recommended)
I will recommend you to use phpmailer instead of built-in method. You can say, phpmailer is extended version of built-in method.
Built-in Method
mail() is built-in method which is very easy and straight forward.
mail ( $receiver_email , $subject , $message);
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php //Receiver Email $receiver_email = "abc@domain.com"; //Mail Subject $subject = "Test Mail"; //your message body $message = "Body of Your Message Here"; if(mail($receiver_email, $subject, $message)) { echo "Mail Sent"; } else { echo "Unable to send mail"; } ?> |
You can also add header information to mail.
mail ( $receiver_email , $subject , $message, $header_data);
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php //Receiver Email $receiver_email = "abc@domain.com"; //Sender Email $sender_email = "sender@email.com"; //Mail Subject $subject = "Test Mail"; //your message body $message = "Body of Your Message Here"; //Mail Header Information $headers = "MIME-Version: 1.0"; $headers.= "Content-type: text/plain; charset=iso-8859-1"; $headers.= "From: {$sender_email }"; $headers.= "Reply-To: {$sender_email }"; $headers.= "Subject: {$subject}"; $headers.= "X-Mailer: PHP/".phpversion(); if(mail($receiver_email, $subject, $message , $headers)) { echo "Mail Sent"; } else { echo "Unable to send mail"; } ?> |
Send Mail using PHPMailer
First you need to download phpmailer library from https://github.com/Synchro/PHPMailer
To use phpmailer, First you need to include class.phpmailer.php file to your PHP file and then create PHPMailer class object, and set some required values (sender email, receiver email, subject, message etc.) and that’s it.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php //include phpmailer class require_once('class.phpmailer.php'); // create mail object" $mail = new PHPMailer(); $mail->IsSendmail(); //telling the class to use SendMail transport $mail->ContentType = 'text/plain'; //Your Message Text $mail->Body = "Your Message Body here"; //reply to address $mail->AddReplyTo("sender_email@yourdomain.com","First Last"); //Sender Email $mail->SetFrom('sender_email@yourdomain.com', 'First Last'); //Receiver Email $address = "receiver@email.com"; $mail->AddAddress($address, "FirstName LastName"); //mail Subject $mail->Subject = "Your Subject Here"; if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent!"; } ?> |
Attach Document (Attach any file in mail)
If you want to attach any document or file in email you just need to add
1 |
$mail->AddAttachment('folder_name/filename.jpg', 'filename.jpg'); |
HTML Formatted Mail
If you want to send mail in HTML format you need to set IsHTML to true
1 |
$mail->IsHTML(true); |
Add BCC/CC
1 2 3 |
$mail->AddCC("CC@mail.com", "First LastName"); $mail->AddBCC("receiver3@email.com", "Receiver Name"); |
That’s It, Any Question?