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@example.com';
$email->From = AddReplyTo( 'userid@example.com', 'Contact Admin' );
$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@example.com';
$email->From = AddReplyTo( 'userid@example.com', 'Contact Admin' );
$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";
}
?>

Questions

PHP Mail PHPMailer class using SMTP Sending Multiple Mails
Feedback form to post visitor details to site admin by email PHP Ajax feedback form
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    06-04-2021

    Just downloaded PHPMailer 6.4.0 but it does not contain the class.phpmailer.php file?

    Post your comments , suggestion , error , requirements etc here





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