<img src="https://d5nxst8fruw4z.cloudfront.net/atrk.gif?account=lYCzn1QolK10N8" style="display:none" height="1" width="1" alt="">

PHPMailer Raw Injection Example

Below is an example of using the Raw Injection Method with PHPMailer. See the Raw Injection Method Headers page for information on what the headers shown in this document represent.

<?php

// phpmailer-raw-injection-example.php
// Inject a test message into GreenArrow Engine using PHPMailer and Raw Injection

// Create the X-Mailer-Info header
function create_feedback_header ($listid, $sendid, $recipient)
{
    $data_to_encode = strtolower($listid . "," . $recipient . "," . $sendid);
    return "X-Mailer-Info: ".
        str_rot13(str_replace(array('+','/','=',"
"),array('-','_'),
        base64_encode(str_rot13(strrev($data_to_encode)))));
}

// Generate bounce address aka Return-Path
function create_bounce_address ($listid, $sendid, $recipient, $bounce_domain)
{
    $verp = str_replace("@", "=", $recipient);
    return "return-$listid-$sendid-$verp@$bounce_domain";
}

// Use PHPMailer
require_once('/usr/share/php/libphp-phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP(); // tell the class to use SMTP
//$mail->SMTPDebug = 2; // uncomment to print debugging info

// Timezone
date_default_timezone_set('America/Chicago');

// GreenArrow Engine installation settings
$mail->Host = "mta.example.com"; // Connect to this GreenArrow server
$mail->SMTPAuth = true; // enables SMTP authentication. Set to false for IP-based authentication
$mail->Port = 587; // SMTP submission port to inject mail into. Usually port 587 or 25
$mail->Username = "smtpauth@example.com"; // SMTP username
$mail->Password = "password"; // SMTP password

// Mailing list settings
$mtaid = "mail"; // VirtualMTA to send campaign out on
$listid = "t99"; // A unique identifier for the list being sent to
$bounce_domain = "mta.example.com"; // Fully qualified domain name used in the bounce address. Your GreenArrow server should be the MX record for this domain

// Campaign Settings
$sendid = "t110630"; // A unique identifier for the send. For example, a list prefix, plus yymmdd
$mail->SetFrom("test@example.com", "From Name");
$mail->Subject = "Raw Injection PHPMailer Example";
$mail->MsgHTML("HTML body");
$mail->AltBody = "Text body";

// Individual Recipient Settings
$recipient = "recipient@example.com";
$recipient_name = "Recipient Name";

// Generate headers
$bounce_address = create_bounce_address ($listid, $sendid, $recipient, $bounce_domain);
$mail->Sender = $bounce_address; // Sets the bounce address
$mail->AddAddress($recipient, $recipient_name); // To: header
$mail->addCustomHeader("X-GreenArrow-MtaID: $mtaid");
$mail->addCustomHeader("X-GreenArrow-ListID: $listid");
$mail->addCustomHeader("X-GreenArrow-SendID: $sendid");
$x_mailer_info = create_feedback_header($listid, $sendid, $recipient);
$mail->addCustomHeader("$x_mailer_info");

// Send the campaign 
if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo . "
";
} else {
    echo "Message sent!
";
}