Swift Mailer Support Thread (Split from Source)

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

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

But how would I most accurately measure memory/cpu usage?
I believe xdebug could give you some ideas on memory usage...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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/AAAAAAAAEAAAAAAABAAACAkAAA4AYmpiauI94j0AAAAAAAAAAAAAAAAAAAAA AAAJBBYALhYAAIBXAACAVwAACAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAA AAAAAAD//w8AAAAAAAAAAAD//w8AAAAAAAAAAAAAAAAAAAAAAGwAAAAAACABAAAAAAAAIAEAACAB AAAAAAAAIAEAAAAAAAAgAQAAAAAAACABAAAAAAAAIAEAABQAAAAAAAAAAAAAADQBAAAAAAAAdAMA AAAAAAB0AwAAAAAAAHQDAAAAAAAAdAMA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UgdGhhdCBnb29kIEhSIG1hbmFnZW1lbnQgaXMg YW4gaW50ZWdyYWwgcGFydCBvZiBhbnkgc3VjY2Vzc2Z1bCBidXNpbmVzcyBpcnJlc3BlY3RpdmUg b2Ygc2l6ZS4gV29ya2luZyBpbiBwYXJ0bmVyc2hpcCwgb3VyIGluLWRlcHRoIGtub3dsZWRnZSBh
[*snip*]
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I'm getting confused... what's the problem? Can you correctly base64_decode() the data again?
Last edited by Chris Corbyn on Fri Jun 02, 2006 4:34 pm, edited 1 time in total.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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)
Last edited by Chris Corbyn on Fri Jun 02, 2006 4:25 pm, edited 1 time in total.
Locked