PHPMailer Digest

PHPMailer is a PHP class for PHP that provides a package of functions to send email. The two primary features are sending HTML Email and e-mails with attachments. PHPMailer supports nearly all possiblities to send email: mail(), Sendmail, qmail & direct to SMTP server. You can use any feature of SMTP-based e-mail, multiple recepients via to, CC, BCC, etc. In short: PHPMailer is an efficient way to send e-mail within PHP.

Download Link: http://phpmailer.worxware.com/

============

<?php
require("class.phpmailer.php");

$mail = new PHPMailer();
$mail->IsMail();
$mail->From = "info@from.net";
$mail->FromName = "sender name";
$mail->AddAddress("receiver email");
$mail->AddReplyTo("info@from.net","sender name");
$mail->IsHTML(true);
$mail->CharSet="utf-8";
           $mail->Subject ="Subject"
           $mail->Body = "<p>Hello!</p>";
           if(!$mail->Send()) {
               echo 'Message was not sent.';
               echo 'Mailer error: ' . $mail->ErrorInfo;
           } else {
               echo 'Message has been sent.';
           }
?>

Updated Methods for PHPMailer v5.0.0

Methods (v5.0.0) Type Default Description
set($name, $value) string $name
string $value
Method provides ability for user to create own custom pseudo-properties (like X-Headers, for example). Example use:
$mail->set(‘X-MSMail-Priority’, ‘Normal’);
addCustomHeader($value) string $value Method provides ability for user to create own custom headers (like X-Priority, for example). Example use:
$mail->addCustomHeader(“X-Priority: 3”);
MsgHTML($message) Evaluates the message and returns modifications for inline images and backgrounds. Sets the IsHTML() method to true, initializes AltBody() to either a text version of the message or default text.
IsMail() boolean true Sets Mailer to send message using PHP mail() function. (true, false or blank)
IsSMTP() boolean Sets Mailer to send message using SMTP. If set to true, other options are also available. (true, false or blank)
IsSendmail() boolean Sets Mailer to send message using the Sendmail program. (true, false or blank)
IsQmail() boolean Sets Mailer to send message using the qmail MTA. (true, false or blank)
SetFrom($address, $name = “”) string $address
string $name
Adds a “From” address.
AddAddress($address, $name = “”) string $address
string $name
Adds a “To” address.
AddCC($address, $name = “”) string $address
string $name
Adds a “Cc” address. Note: this function works with the SMTP mailer on win32, not with the “mail” mailer.
AddBCC($address, $name = “”) string $address
string $name
Adds a “Bcc” address. Note: this function works with the SMTP mailer on win32, not with the “mail” mailer.
AddReplyTo($address, $name = “”) string $address
string $name
Adds a “Reply-to” address.
Send() Creates message and assigns Mailer. If the message is not sent successfully then it returns false. Use the ErrorInfo variable to view description of the error. Returns true on success, false on failure.
AddAttachment($path, $name = “”, $encoding = “base64”,
$type = “application/octet-stream”)
string $path
string $name
string $encoding
string $type
Adds an attachment from a path on the filesystem. Returns false if the file could not be found or accessed.
AddEmbeddedImage($path, $cid, $name = “”, $encoding = “base64”,
$type = “application/octet-stream”)
string $path
string $cid
string $name
string $encoding
string $type
Adds an embedded attachment. This can include images, sounds, and just about any other document. Make sure to set the $type to an image type. For JPEG images use “image/jpeg” and for GIF images use “image/gif”. If you use the MsgHTML() method, there is no need to use AddEmbeddedImage() method.
ClearAddresses() Clears all recipients assigned in the TO array. Returns void.
ClearCCs() Clears all recipients assigned in the CC array. Returns void.
ClearBCCs() Clears all recipients assigned in the BCC array. Returns void.
ClearReplyTos() Clears all recipients assigned in the ReplyTo array. Returns void.
ClearAllRecipients() Clears all recipients assigned in the TO, CC and BCC array. Returns void.
ClearAttachments() Clears all previously set filesystem, string, and binary attachments. Returns void.
ClearCustomHeaders() Clears all custom headers. Returns void.

============================

Database Example (for PHPMailer v5.0.0)

This demonstrates sending out multiple email messages with binary attachments from a MySQL database with multipart/alternative support.

require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail                = new PHPMailer();

$body                = file_get_contents('contents.html');
$body                = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host          = "smtp1.site.com;smtp2.site.com";
$mail->SMTPAuth      = true;                  // enable SMTP authentication
$mail->SMTPKeepAlive = true;                  // SMTP connection will not close after each email sent
$mail->Host          = "mail.yourdomain.com"; // sets the SMTP server
$mail->Port          = 26;                    // set the SMTP port for the GMAIL server
$mail->Username      = "yourname@yourdomain"; // SMTP account username
$mail->Password      = "yourpassword";        // SMTP account password
$mail->SetFrom('list@mydomain.com', 'List manager');
$mail->AddReplyTo('list@mydomain.com', 'List manager');

$mail->Subject       = "PHPMailer Test Subject via smtp, basic with authentication";

@MYSQL_CONNECT("localhost","root","password");
@mysql_select_db("my_company");
$query  = "SELECT full_name, email, photo FROM employee WHERE id=$id";
$result = @MYSQL_QUERY($query);

while ($row = mysql_fetch_array ($result)) {
  $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  $mail->MsgHTML($body);
  $mail->AddAddress($row["email"], $row["full_name"]);
  $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");

  if(!$mail->Send()) {
    echo "Mailer Error (" . str_replace("@", "@", $row["email"]) . ') ' . $mail->ErrorInfo . '<br>';
  } else {
    echo "Message sent to :" . $row["full_name"] . ' (' . str_replace("@", "@", $row["email"]) . ')<br>';
  }
  // Clear all addresses and attachments for next loop
  $mail->ClearAddresses();
  $mail->ClearAttachments();
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *