Page 3 of 6

Posted: Tue May 30, 2006 9:56 pm
by Weirdan
option two, definitely. perhaps something along the lines of:

Code: Select all

$html_part = '
Here is an inline image! <img src="'.$mailer->addImage('/path/to/beach.png').'"/>

do you like it?
';
could be even better, as it allows easy customization of img attributes.

Posted: Wed May 31, 2006 2:46 am
by Chris Corbyn
Weirdan wrote:option two, definitely. perhaps something along the lines of:

Code: Select all

$html_part = '
Here is an inline image! <img src="'.$mailer->addImage('/path/to/beach.png').'"/>

do you like it?
';
could be even better, as it allows easy customization of img attributes.
You got it buddy... as long as Swift loads in the attachment as inline data and passes back the correct identifier for it that will work nicely :)

Posted: Wed May 31, 2006 7:20 pm
by Chris Corbyn
Since it's been requested, here's a few side-by-side's against PHPMailer.

Some of these examples are only available in 1.1 due for release in a couple of days.

It's the first time I've done this actually so pretty interesting :) I'm aware of the fact that the Swift code looks tighter - I've just laid it out the way I code, copy and past and muck around if you want.

---------------------
Sending a plain email over SMTP:

In PHPMailer

Code: Select all

<?php

require('class.phpmailer.php');

$mail = new PHPMailer;

$mail->From = 'someone@tld';
$mail->FromName = 'Someone';

$mail->Host = 'smtp.host.tld';
$mail->Mailer = 'smtp';

$mail-Subject = 'Some subject';
$mail->Body = 'The email body';

$mail->AddAddress('recipient@tld', 'Recipient Name');

if (!$mail->Send()) echo 'Not sent!';

$mail->ClearAddresses();

?>
In Swift

Code: Select all

<?php

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

$mail = new Swift(new Swift_SMTP_Connection('smtp.host.com'));

if (!$mail->send(
	'"Recipient Name" <recipient@tld>',
	'"Someone" <someone@tld>',
	'Some subject',
	'This is the body')) echo 'Not sent!';

?>
-------------------------------

Sending a mail with a HTML part and a plain text part

PHPMailer

Code: Select all

<?php

require('class.phpmailer.php');

$mail = new PHPMailer;

$mail->From = 'someone@tld';
$mail->FromName = 'Someone';

$mail->Host = 'smtp.host.tld';
$mail->Mailer = 'smtp';

$mail->IsHTML();
$mail-Subject = 'Some subject';
$mail->Body = '<em>HTML</em> part';
$mail->AltBody = 'Plain part';

$mail->AddAddress('recipient@tld', 'Recipient Name');

if (!$mail->Send()) echo 'Not sent!';

$mail->ClearAddresses();

?>
Swift

Code: Select all

<?php

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

$mail = new Swift(new Swift_SMTP_Connection('smtp.host.com'));

$mail->addPart('Plain part');
$mail->addPart('<em>HTML</em> part', 'text/html');

if (!$mail->send(
	'"Recipient Name" <recipient@tld>',
	'"Someone" <someone@tld>',
	'Some subject')) echo 'Not sent!';

?>
----------------------------------------

Sending an email to 4 people independently:

In PHPMailer

Code: Select all

<?php

require('class.phpmailer.php');

$mail = new PHPMailer;

$recipients = array('one@tld', 'two@tld', 'three@tld', 'four@tld');

$mail->From = 'someone@tld';
$mail->FromName = 'Someone';

$mail->Host = 'smtp.host.tld';
$mail->Mailer = 'smtp';

$mail-Subject = 'Some subject';
$mail->Body = 'Body';

foreach ($recipients as $address)
{
	$mail->AddAddress($address);
	
	if (!$mail->Send()) echo 'Not sent!';
	
	$mail->ClearAddresses();
}

?>
Swift

Code: Select all

<?php

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

$mail = new Swift(new Swift_SMTP_Connection('smtp.host.com'));

$recipients = array('one@tld', 'two@tld', 'three@tld', 'four@tld');

if (!$mail->send(
	$recipients,
	'"Someone" <someone@tld>',
	'Some subject',
	'Body')) echo 'Not sent!';

?>
--------------------------------

Sending an attachment:

PHPMailer

Code: Select all

<?php

require('class.phpmailer.php');

$mail = new PHPMailer;

$file = '../files/myimage.jpg';
$filename = 'My Image.jpg';

$mail->From = 'someone@tld';
$mail->FromName = 'Someone';

$mail->Host = 'smtp.host.tld';
$mail->Mailer = 'smtp';

$mail-Subject = 'Some subject';
$mail->Body = 'Body';

$mail->AddAddress('recipient@tld', 'Recipient Name');

$mail->AddAttachment($file, $filename);

if (!$mail->Send()) echo 'Not sent!';

$mail->ClearAddresses();
$mail->ClearAttachments();

?>
Swift

Code: Select all

<?php

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

$mail = new Swift(new Swift_SMTP_Connection('smtp.host.com'));

$file = '../files/myimage.jpg';
$filename = 'My Image.jpg';

$mail->addPart('Body');

$mail->addAttachment(file_get_contents($file), $filename);

if (!$mail->send(
	'"Recipient Name" <recipient@tld>',
	'"Someone" <someone@tld>',
	'Some subject')) echo 'Not sent!';

?>
---------------------------------------------

Sending mail with an inline image:

PHPMailer

Code: Select all

<?php

require('class.phpmailer.php');

$mail = new PHPMailer;

$cid = 'abcdefg123456';
$image = '../files/myimage.jpg';

$mail->IsHTML();

$mail->From = 'someone@tld';
$mail->FromName = 'Someone';

$mail->Host = 'smtp.host.tld';
$mail->Mailer = 'smtp';

$mail-Subject = 'Some subject';
$mail->Body = '<strong>So here is where we went!</strong><br />
<img src="cid:'.$cid.'" alt="Somewhere" /><br />
Pretty nice eh?';

$mail->AddEmmbeddedImage($file, $cid);

$mail->AddAddress('recipient@tld', 'Recipient Name');

if (!$mail->Send()) echo 'Not sent!';

$mail->ClearAddresses();
$mail->ClearAttachments();

?>
Swift

Code: Select all

<?php

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

$mail = new Swift(new Swift_SMTP_Connection('smtp.host.com'));

$image = '../files/myimage.jpg';

$mail->addPart('<strong>So here is where we went!</strong><br />
<img src="'.$mail->addImage($image).'" alt="Somewhere" /><br />
Pretty nice eh?', 'text/html');

if (!$mail->send(
	'"Recipient Name" <recipient@tld>',
	'"Someone" <someone@tld>',
	'Some subject')) echo 'Not sent!';

?>
-----------------------------------------

Relaying mail via Gmail servers (TLS encryption)

PHPMailer

Code: Select all

<?php

/** Forget it - TLS is not supported **/

?>
Swift

Code: Select all

<?php

require('Swift.php');
require('Swift/Swift_SMTP_Connection.php');

//I had this automated but it was flaky - I'll try to remove the need to do this again
$mail = new Swift(new Swift_SMTP_Connection('smtp.gmail.com', false, SWIFT_TLS));

$mail->authenticate('user@gmail.com', 'pass');

if (!$mail->send(
	'"Recipient Name" <recipient@tld>',
	'"Someone" <someone@tld>',
	'Some subject',
	'The body')) echo 'Not sent!';

?>
Haven't had time to benchmark yet... was this what you were looking for though?

Posted: Wed May 31, 2006 9:32 pm
by Roja
d11wtq wrote:Haven't had time to benchmark yet... was this what you were looking for though?
Yes. Thats just what I was looking for.

Looking forward to the benchmark results. :)

Posted: Thu Jun 01, 2006 1:24 am
by Chris Corbyn
Roja wrote:
d11wtq wrote:Haven't had time to benchmark yet... was this what you were looking for though?
Yes. Thats just what I was looking for.

Looking forward to the benchmark results. :)
By the way, the only sort of benchmarking I've ever done is speed tests.... I'll do those. But how would I most accurately measure memory/cpu usage? I use linux which I'm guessing helps :)

Posted: Thu Jun 01, 2006 2:37 am
by s.dot
Hmm, I remembered searching php.net for some windows statistics on server load and found some good functions. Searching again I can't seem to find what I was looking for

but maybe memory_get_usage() and getrusage() could be useful

Posted: Thu Jun 01, 2006 3:52 am
by Weirdan
But how would I most accurately measure memory/cpu usage?
I believe xdebug could give you some ideas on memory usage...

Posted: Thu Jun 01, 2006 8:21 am
by Chris Corbyn
I shall have a look cheers :) Something outside of PHP (such as xdebug as you say) is going to be better because to get calls to those functions in the right place I'll need to modify the code in both Swift and PHPMailer.

Posted: Thu Jun 01, 2006 10:34 am
by Maugrim_The_Reaper
Xdebug Profiling in combination with KCacheGrind (there's one for Windows similar to KDE's) does a fair job on breaking down execution time by function/method. memory_get_usage() will get overall memory use on Linux (won't work on Windows without implementing a PHP alternative).

I just started looking through the code...very nice.

Posted: Fri Jun 02, 2006 10:50 am
by JayBird
Trying to send attachments with this now and when i recieve the email i get this in the message body!

Code: Select all

--
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit

Have a look at durhaml!!


__________ NOD32 1.1454 (20060321) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com


--
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit

Have a look at durham <strong>NOW</strong>!!
----


--swift-8EF4BA73109BE3CC234E467848266FFC
Content-Type: application/vnd.ms-word; name="how_can_we_help_you.web_page9.doc";
Content-Transfer-Encoding: base64
Content-Description: how_can_we_help_you.web_page9.doc
Content-Disposition: attachment; filename="how_can_we_help_you.web_page9.doc"

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAKQAAAAAAAAAA
EAAAKwAAAAEAAAD+////AAAAACgAAAD/////////////////////////////////////////
EAAAKwAAAAEAAAD+////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////s
[*snip*]
any ideas?

Posted: Fri Jun 02, 2006 11:07 am
by Chris Corbyn
:? The only thing Swift does with the data you pass it is to base64 encode it, so you can have a look yourself if you like and try just:

Code: Select all

echo chunk_split(base64_encode(file_get_contents($file)));
What PHP version/Swift version do you have?

Posted: Fri Jun 02, 2006 11:09 am
by Chris Corbyn
FYI. After a flurry of emails from the website regarding the GPL license I can confirm I'll be swicthing to LGPL. If you have an earlier version of the Swift Library I'm not going to take action if you choose to use it in your commercial products although it's probably a good idea to upgrade to 1.1.1 which I'm adding later today (same as 1.1.0 but license changed).

Posted: Fri Jun 02, 2006 11:14 am
by JayBird
I am using Swift 1.1.0

PHP Version 4.4.2

Running the snippet above you just gave me outputs:

Code: Select all

0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAAKQAAAAAAAAAA EAAAKwAAAAEAAAD+////AAAAACgAAAD///////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////s pcEATSAJBAAA8BK/AAAAAA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AAAAAAAfxIAAAAAAACYAwAAAAAAAIwDAAAMAAAAMCJ2769N xgE0AQAAQAIAAHQDAAAAAAAAogMAACIAAAD7EQAADAAAAAAAAAAAAAAAfxIAAAAAAADQEgAAMAAA AAATAAAAAAAABxIAAHgAAADGFgAAAAAAAMQDAAAAAAAAxhYAAAAAAAB/EgAAAAAAAMQDAAAAAAAA NAEAAAAAAAA0AQAAAAAAACABAAAAAAAAIAEAAAAAAAAgAQAAAAAAACABAAAAAAAAAgDZAAAASG93 IENhbiBXZSBIZWxwIFlvdT8NDVdlIGJlbGlldmUgdGhhdCBnb29kIEhSIG1hbmFnZW1lbnQgaXMg YW4gaW50ZWdyYWwgcGFydCBvZiBhbnkgc3VjY2Vzc2Z1bCBidXNpbmVzcyBpcnJlc3BlY3RpdmUg b2Ygc2l6ZS4gV29ya2luZyBpbiBwYXJ0bmVyc2hpcCwgb3VyIGluLWRlcHRoIGtub3dsZWRnZSBh
[*snip*]

Posted: Fri Jun 02, 2006 11:57 am
by Chris Corbyn
I'm getting confused... what's the problem? Can you correctly base64_decode() the data again?

Posted: Fri Jun 02, 2006 12:26 pm
by Chris Corbyn
Bugfix: 1.1.0 --> 1.1.1

$alternative_boundary declared in a condition not satisfied if embedded images aren't there. Now fixed in 1.1.1, 1.1.0 no longer available over HTTP (still in FTP if anybody wants to see the bug :P)