Saturday, April 11, 2015

[PHP] Simple Email dengan PHP Mailer

11 April 2015

Intro::

Simple send email menggunakan PHPMAILER dengan akun Gmail.

Steps::

Download Script PHPMAILER
1. https://github.com/PHPMailer/PHPMailer
2. install & extract localhost/twitplay/lib/phpmailer/

Simple Parsing html
1. copas script dibawah ini dan jangan lupa mengganti username_gmail, password_gmail, name, to_email

<?php
require 'lib/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;

//from username
$mail->Username = 'username_gmail';
$mail->Password = 'password_gmail';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->From = 'username_gmail@gmail.com';
$mail->FromName = 'name';

//to email
$mail->addAddress('to_email@example.com');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//attachment
//$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('img/yupi.jpg');
$mail->isHTML(true);

//body
$mail->Subject = 'Hello World';
$mail->Body    = 'Yupi was Here.';
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

?>

2. simpan email.php dan panggil di browser

Catatan::

attachment yupi.jpg jangan lupa dimasukan kedalam folder /img/

No comments:

Post a Comment