help with swiftmailer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ansi,
Forum Newbie
Posts: 3
Joined: Thu Sep 18, 2008 11:03 am

help with swiftmailer

Post by ansi, »

i was wandering if i could get some help with my code for swiftmailer.

i have a database that contains email addresses, names, and a email key. what i need to do is send an email to each person in the database with a link that uses the email key.

here is the code that i have now but this is my first time using swiftmailer so im not sure if i am doing it right or how to show errors if there are any or if there is a way to show errors.

Code: Select all

 
<?
ob_start();
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link href="inc/css/style.css" rel="stylesheet" type="text/css" />
        <title>MyDomain</title>
    </head>
 
<body>
 
<?php include("header.php"); 
 
$dbtable = "eml_campaign_test";
 
require_once ("includes/phpscripts/conn.php");
require_once "swift/lib/Swift.php";
require_once "swift/lib/Swift/Connection/SMTP.php";
require_once "swift/lib/Swift/Plugin/Decorator.php";
 
//Instantiate Swift as usual
$swift =& new Swift(new Swift_Connection_SMTP("mail.domain.com"));
 
//Create the message, using some unique variables to search for
$message =& new Swift_Message("Your registration with MyDomain is almost complete!","Hello {name}, your account is almost setup at domain.com! all you need to do is click this link: <a href='http://www.domain.com/signup.php?k={emlkey}'>http://www.domain.com/signup.php?k={emlkey}</a> and complete the registration process.", "text/html");
 
$recipients =& new Swift_RecipientList();
 
$er = mysql_query("select email from {$dbtable}");
while ( $row = mysql_fetch_array( $er, MYSQL_ASSOC ) )
    $recipients->addTo( $row['email'] );
 
 
//Extend the replacements class
class Replacements extends Swift_Plugin_Decorator_Replacements {
    function getReplacementsFor($address) {
        $address = mysql_real_escape_string($address);
        $query = "select
        f_name as `{name}`, emlkey as `{emlkey}`
        from `eml_campaign_test`
        where email = '{$address}'";
        //die($query);
        $result = mysql_query($query) or die( mysql_error() );
        if (mysql_num_rows($result) > 0)
        {
            return mysql_fetch_assoc($result);
        }
    }
}
 
//Load the plugin with the extended replacements class
$swift->attachPlugin(new Swift_Plugin_Decorator(new Replacements()), "decorator");
 
//Send messages
if ( $swift->send($message, $recipients, "register@domain.com") )
    echo "mail sent";
else
    echo "error: ".$swift->error();
 
$swift->disconnect();
include("footer.html"); 
?>
 
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: help with swiftmailer

Post by Jade »

I've never used swiftmailer before but I can help you with the idea behind it.

1) Connect to the database
2) Query to select all email address, names and keys
3) While you have results from that query, generate the text/info for the email
4) Send the email
5) End the loop
ansi,
Forum Newbie
Posts: 3
Joined: Thu Sep 18, 2008 11:03 am

Re: help with swiftmailer

Post by ansi, »

that's basically what it's attempting to do. it selects all of the email addresses and then adds them to the queue then in the Replacements class it replaces {name} and {emlkey} with the name and key before trying to send. some sort of plugin type deal for swiftmailer from the docs from what i can see.

http://www.swiftmailer.org/wikidocs/v3/plugin_decorator
Post Reply