PHPMailer class to post mails from website.

PHPMailer To post mails from website phpmailer is much better and easy to use than using PHP mail() function.

Installation of phpmailer

Visit github and download the PHPMailer zip file, place them inside a folder say my_phpmailer. You have to include the file class.phpmailer.php
require_once('my_phpmailer/class.phpmailer.php');
We kept this file inside my_phpmailer folder, you can change the path as per your site condition.
This is an object oriented program so we will create the object first.
$email = new PHPMailer();
Most of our job is over now. Here is the full code to send your first mail using phpmailer.
<?php

$bodytext=' This is the body of the test mail ';
$subject= 'plus2 Message Subject'.date(" H:i:s", time());


require_once('my_phpmailer/class.phpmailer.php');
$email = new PHPMailer();
$email->From = 'userid@exmple.com';
$email->FromName = 'Your Name';
$email->Subject   = $subject;
$email->Body      = $bodytext;
$email->AddAddress('userid1@example.com');
$email->AddAddress('userid2@example.com');


if(!$email->send()) 
{
    echo "Mailer Error: " . $email->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}
?>

Sending HTML Email

After the $email->Body we will add this .
$email->Body      = $bodytext;
$email->IsHTML(true);// HTML email 

Adding CC & BCC

$email->AddCC('userid3@example.com);
$email->AddBCC('userid4@example.com');

Sending file as attachment with mail

$file_to_attach = 'your_dir/top.pdf'; // Path with file name
$email->AddAttachment( $file_to_attach);// File attached
The full code with file attachement is here.
<?php

$bodytext=' This is the body of the test mail ';
$subject= 'plus2 Message Subject'.date(" H:i:s", time());


require_once('my_phpmailer/class.phpmailer.php');
$email = new PHPMailer();
$email->From = 'userid@exmple.com';
$email->FromName = 'Your Name';
$email->Subject   = $subject;
$email->Body      = $bodytext;
$email->AddAddress('userid1@example.com');
$email->AddAddress('userid2@example.com');

$file_to_attach = 'your_dir/top.pdf'; // Path with file name
$email->AddAttachment( $file_to_attach);// File attached

if(!$email->send()) 
{
    echo "Mailer Error: " . $email->ErrorInfo;
} 
else 
{
    echo "Message has been sent successfully";
}
?>
Your Rating





Post your comments , suggestion , error , requirements etc here .




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer