anyone use swiftmailer?

Discussion in 'PHP' started by ansi, Sep 18, 2008.

  1. #1
    if so...

    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.

    
    <?
    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->disconnect();
    include("footer.html"); 
    ?>
    
    PHP:

     
    ansi, Sep 18, 2008 IP