Swift Persistance.

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
dphoebus
Forum Newbie
Posts: 1
Joined: Thu Sep 27, 2007 9:37 pm

Swift Persistance.

Post by dphoebus »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to tackle making a SwiftMailer Component for Cake 1.2 using SwiftMailer 3.3.  My question is this:  When a connection is made (in this case $smtp =& new Swift_Connection_SMTP()), can you store that connection for later use?

Using the same idea as the 2.2 component, not code due to incompatibility, I want to create a Swift Object from a connection set by the user.  The user can specify native,smtp, or sendmail and the connection is made.  From that connection a Swift Object is created.  Then I want to store that object in a public variable for later use.  Here is where I run into trouble.  I can make the connection and create the Swift object, but when I try to make the Swift Message in a later function, $message =$ new Swift_Message('subject') fails.  $message is blank.

Code: Select all

public function addBody($subject, $body, $type) {
    vendor("Swift");
    // Create Swift Message.
    try {
	$this->body = new Swift_Message($subject, $body, $type);
    } catch (Swift_Message_MimeException $e) {
  	echo "There was an unexpected problem building the email:" . $e->getMessage();
	exit();
    }
}
$this->body is a public variable in the component. But when I try to send(), $this->body is empty causing a failure.

HOWEVER.... if I group everything together like this:

Code: Select all

vendor("Swift");
vendor('Swift'.DS.'Connection'.DS.'SMTP');
				
$smtp =& new Swift_Connection_SMTP("HOST", 25, ENC_TLS);
$smtp->setUsername('****');
$smtp->setPassword('****');
				
$swift =& new Swift($smtp); 
// Build Message
$message =& new Swift_Message("SUBJECT", "BODY", "text/html");
// Build Recipients
$recipients =& new Swift_RecipientList();
$recipients->addTo("TO", "ME");
// Build From
$from =& new Swift_Address("FROM",'FROM NAME');
 
if ($swift->send($message, $recipients, $from)) {
	$this->Session->setFlash('<div class="msg_report"><p>Message sent.</p></div>');
} else {
        $this->Session->setFlash('<div class="error_report"><p>Message failed to send</p></div>');
}
This works. Can you not save the Swift Object? Can you only make a Swift_Message at the same time the Swift object is instantiated? Am I doing something wrong?

Sorry if I am rambling... I have been going round and round with this for hours and my eyes and mind are blury. I appreciate any help and thank you very much.

Daniel


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply