Page 1 of 1

Swift Mailer Question

Posted: Thu Aug 06, 2009 10:27 am
by wedmonds
Hello again,

I had a quick look at SwiftMailer and confused myself entirely as I am not 100% if it would be the best solution for me.
I have some silly questions which I think would give me a better idea.

1. Will Swiftmailer provide me with the ability to create e-mails and send them out so that people using old mail clients/web mail programs that will only accept text and people using slightly newer mail clients using HTML?. I found the following example for use with PHP but I could not get it working correctly. I received an email but i was expecting one line of "Hello World!!! This is simple text email message." but i ended up receiving both.
Will Swiftmailer make this any easier without causing me more problem further down the line?

2. Is it a fairly straight forward process to install this onto a hosted server via Cpanel?

Thank you everyone.

wedmonds

Code: Select all

<?php
 
//define the receiver of the email
$to = 'example@example.com';
//define the subject of the email
$subject = 'Multipart E-Mail Test';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: wayneedmonds@btinternet.com\r\n Reply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
Hello World!!! 
This is simple text email message. 
 
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
 
--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
 

Re: Swift Mailer Question

Posted: Fri Aug 21, 2009 9:47 am
by andyb
1. It is possible to send an email using SWIFT that contains plain text and HTML parts. Using the correct commands in the right order will prevent you from receiving receiving both. Swiftmailer is very easy to use but like anything you have to sit there and use it for a while to get the best out of it.

2. To install it you put it onto the server using FTP. Put it into the same place as your PHP programs live. Then just require() the libary and off you go!