Pengiriman Email Pada PHP Menggunakan Swift Mailer


Dalam dunia internet pada saat ini, email bukanlah suatu hal yang sulit didapatkan. Bahkan email dijadikan suatu identitas yang dibutuhkan dalam proses registrasi. Pada pemrograman PHP, disediakan fungsi mail() yang berfungsi untuk melakukan pengiriman email ke suatu atau beberapa alamat email. Dengan menggunakan Swift Mailer, yang merupakan API untuk pengiriman email di PHP, code PHP yang dibuat akan semakin mudah.

Berikut cara pengiriman email menggunakan Swift Mailer:

1. Pengiriman Email Standard

<?php
//Load in the files we'll need
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

//Start Swift
$swift =& new Swift(new Swift_Connection_SMTP("smtp.your-host.tld"));

//Create the message
$message =& new Swift_Message("My subject", "My body");

//Now check if Swift actually sends it
if ($swift->send($message, "foo@bar.tld", "me@mydomain.com")) echo "Sent";
else echo "Failed";
?>

2. Pengiriman Email Berbentuk HTML

<?php
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

$swift =& new Swift(new Swift_Connection_SMTP("server.tld", 25));

$message =& new Swift_Message("Some subject", "Your message <u>here</u>", "text/html");

if ($swift->send($message, "recipient@domain.tld", "you@home.tld"))
{
    echo "Message sent";
}
else
{
    echo "Message failed to send";
}

//It's polite to do this when you're finished
$swift->disconnect();
?>

3. Penanganan Kesalahan Dalam Pengiriman

<?php
//Load in the files we'll need
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

try {
  //Start Swift
  $swift = new Swift(new Swift_Connection_SMTP("smtp.your-host.tld"));

  //Create the message
  $message = new Swift_Message("My subject", "My body");

  //Now check if Swift actually sends it
  $swift->send($message, "foo@bar.tld", "me@mydomain.com");
  echo "Sent";
} catch (Swift_Connection_Exception $e) {
  echo "There was a problem communicating with SMTP: " . $e->getMessage();
} catch (Swift_Message_MimeException $e) {
  echo "There was an unexpected problem building the email:" . $e->getMessage();
}
?>

4. Pengiriman Attachment

<?php
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/NativeMail.php"; //There are various connections to use

$swift =& new Swift(new Swift_Connection_NativeMail());

$message =& new Swift_Message("My subject");
$message->attach(new Swift_Message_Part("I have attached a file to this message!"));

//Use the Swift_File class
$message->attach(new Swift_Message_Attachment(
  new Swift_File("foo.pdf"), "foo.pdf", "application/pdf"));

$swift->send($message, "my-friend@host.tld", "me@my-domain.tld");
?>

5. Menambahkan Gambar pada Email Berbentuk HTML

<?php
require_once "lib/Swift.php";
require_once "lib/Swift/Connection/SMTP.php";

$smtp =& new Swift_Connection_SMTP("your.smtp.tld", 25);

$swift =& new Swift($smtp);

$message =& new Swift_Message("Your subject");

$img =& new Swift_Message_Image(new Swift_File("/path/to/image.jpg"));

$src = $message->attach($img);

$body =& new Swift_Message_Part("This email contains this image:<br />
<img src=\"" . $src . "\" /><br />
which is embedded", "text/html");

$message->attach($body);

if ($swift->send($message, new Swift_Address("joe@bloggs.com", "Joe"), new Swift_Address("system@domain.tld", "System")))
{
    echo "Message sent";
}
else
{
    echo "Sending failed";
}

//recommended to do this
$swift->disconnect();
?>

Selain menggunakan Swift Mailer, masih banyak API untuk pengiriman email lainnya di lingkungan PHP, antara lain PHPMailer, Zend_Mail, PEAR Mail, dll. Selamat mencoba!

3 thoughts on “Pengiriman Email Pada PHP Menggunakan Swift Mailer

Leave a comment