[SOLVED] custom header?

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
dillion
Forum Commoner
Posts: 56
Joined: Thu Feb 15, 2007 8:32 am

[SOLVED] custom header?

Post by dillion »

Code: Select all

Return-Path: <me@domain.com>
Delivered-To: to@hotmail.com
Received: from www.domain.com (server12-345-678-910.servers.net [12.345.678.910])
	by localhost.domain.com (Postfix) with ESMTP id EA58E1215D6
	for <to@hotmail.com>; Thu,  5 Apr 2007 15:56:48 +0100 (BST)
To: to@hotmail.com
From: me@domain.com
Reply-To: me@domain.com
Subject: Welcome to subject
Date: Thu, 05 Apr 2007 15:56:48 +0100
X-Mailer: Swift 3.1.2
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="_=_swift-172760851546150e30eb0a49.73991995_=_"
Content-Transfer-Encoding: 7bit
Message-Id: <20070405145648.EA58E1215D6@localhost.domain.com>
Basic code used

Code: Select all

$swift =& new Swift(new Swift_Connection_SMTP("mail.domain.com"));
	
	$swift->attachPlugin(new Swift_Plugin_AntiFlood(100, 10), "anti-flood");
	 
	//Create the message
	$message =& new Swift_Message("Welcome to subject");
	
	//Add some "parts"
	$message->attach(new Swift_Message_Part("Part 1 of message"));
	$message->attach(new Swift_Message_Part("Part <strong>2</strong> of message", "text/html"));
	 
	$from = "me@domain.com";

	//Send messages
	if($swift->send($message, "me@hotmail.com", $from)) echo "Sent";
		
	$swift->disconnect();
I am wondering if there is a way to append some extra header info and also how do I set reply-to and sender to have two different email addresses? e.g. me@domain.com for people to reply email to, and basic@domain.com to receive any bounced emails. So basically, the header becomes (see the ------- line):

Code: Select all

Return-Path: <me@domain.com>
Delivered-To: to@hotmail.com
Received: from www.domain.com (server12-345-678-910.servers.net [12.345.678.910])
	by localhost.domain.com (Postfix) with ESMTP id EA58E1215D6
	for <to@hotmail.com>; Thu,  5 Apr 2007 15:56:48 +0100 (BST)
To: to@hotmail.com
From: basic@domain.com   Modified <---------------------------------------------------------------------
Reply-To: me@domain.com
Subject: Welcome to subject
Date: Thu, 05 Apr 2007 15:56:48 +0100
X-Mailer: Swift 3.1.2
X-Subscriber: 12    Added <---------------------------------------------------------------------
X-somethingElse: 123456   Added <---------------------------------------------------------------------
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="_=_swift-172760851546150e30eb0a49.73991995_=_"
Content-Transfer-Encoding: 7bit
Message-Id: <20070405145648.EA58E1215D6@localhost.domain.com>
Many thanks in advance! :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

http://www.swiftmailer.org/wikidocs/v3/ ... on/headers


Reply-to address is set in the $message:

Code: Select all

$message->setReplyTo("foo@bar.com");
//or even
$message->setReplyTo(new Swift_Address("foo@bar.com", "Foobar"));
I'm re-doing all the docs pretty extensively but it's gonna take me a while so I do apologies for the lack of actual examples. The API can still be found in all completeness though:

http://www.swiftmailer.org/api/php5/

EDIT | As per the first link I just posted, you could also set the reply to the long way around:

Code: Select all

$message->headers->set("Reply-To", "foo@bar.com");
//or
$addr = new Swift_Address("foo@bar.com", "Foobar");
$message->headers->set("Reply-To", $addr->build());
dillion
Forum Commoner
Posts: 56
Joined: Thu Feb 15, 2007 8:32 am

Post by dillion »

thanks mate. So I could do the following, right?

Code: Select all

$message->headers->set("X-Subscriber", "foo@bar.com");
$message->headers->set("X-Campaign", "Football news - 06/04/2007");
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I guess you've sussed it by now :)

Yes, that's how it works. MIME parts and attachments have their own little set of headers too so you could feasibly also do:

Code: Select all

$attachment->headers->set("X-OriginalFilename", "somefile.ext");
dillion
Forum Commoner
Posts: 56
Joined: Thu Feb 15, 2007 8:32 am

Post by dillion »

sorry mate - was away visiting my parents! :)

But I have just given this a try and I can confirm it works!

Once again, many thanks for your help. 8)
Post Reply