Kamu menggunakan zimbra dan mau mengirimkan email menggunakan coding seperti PHP?
oke kali ini kita akan sharing bagaimana caranya dan bagaimana konfigurasinya.
Kirim email SMTP Relay menggunakan PHP Code.
-
Native.
Jika kalian menggunakan php native, kalian bisa menggunakan library PHP Mailer, untuk instalasi, kalian memerlukan composer, sematkan code ini didalam file composer.json
kalian.
"phpmailer/phpmailer": "^6.5"
atau kalian dapat menjalankan code ini:
composer require phpmailer/phpmailer
Setelah PHPMailer
terinstall, kalian dapat mencoba dengan contoh code PHP dibawah ini, disesuaikan saja sesuai kebutuhan kalian.
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'relay.excellent.co.id'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'relay.vavai@excellent.co.id'; //SMTP username
$mail->Password = 'StdPwdStrong2021!'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('marketing@company.com', 'Mailer');
$mail->addAddress('anton@gmail.com', 'Anton'); //Add a recipient
$mail->addAddress('doni@gmail.com'); //Name is optional
$mail->addReplyTo('noreply@company.com', 'Information');
$mail->addCC('marissa@yahoo.com');
$mail->addBCC('mira@outlook.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name
//Content
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
-
CodeIgniter.
Untuk framework Code Igniter, disini menggunakan codeigniter versi 3, pastikan sudah di install, lalu buat file di dalam folder aplication/controller
, file tersebut digunakan untuk konfigurasi kirim email menggunakan php, untuk menggunakannya kalian bisa menggunakan library kirim email yang sudah disediakan oleh Code Igniter, kalian dapat memanggil library tersebut menggunakan code $this->load->library('email')
.
<?php
if (!defined('BASEPATH'))exit('No direct script access allowed');
class Export extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('site');
}
public function index(){
$data['title'] = 'Create Excel | TechArise';
$data['result'] = $this->site->getProduct();
$this->load->view('index', $data);
}
public function sendEmail() {
$data['getInfo'] = $this->site->getProduct();
$htmlContent = $this->load->view('generatepdffile', $data, TRUE);
$config = array(
'protocol' => 'smtp', // 'mail', 'sendmail', or 'smtp'
'smtp_host' => 'relay.excellent.co.id',
'smtp_user' => 'relay.vavai@excellent.co.id',
'smtp_pass' => 'StdPwdStrong2021!',
'smtp_port' => 587, // u can use 465 or 25 or 587
'smtp_crypto' => 'tls', //can be 'ssl' or 'tls' for example
'mailtype' => 'html', //plaintext 'text' mails or 'html'
'charset' => 'utf-8',
'newline' => "\r\n"
);
$this->load->library('email', $config);
$from = 'marketing@company.com';
$to = 'marissa@gmail.com';
$subject = 'Example Subject';
$message = 'hello this is test message';
$this->email->set_newline("\r\n");
$this->email->from($from);
$this->email->to($to);
$this->email->subject($subject);
$this->email->message($htmlContent);
if ($this->email->send()) {
echo 'Your Email has successfully been sent.';
} else {
show_error($this->email->print_debugger());
}
}
}
?>
Selanjutnya buat file di folder aplication/views
yang akan digunakan sebagai landing page.
<div class="row">
<div class="col-lg-12">
<h2>Send Email using CodeIgniter Email Library</h2>
</div>
</div><!-- /.row -->
<div class="row">
<div class="col-lg-12">
<a href="<?php echo base_url();?>export/sendEmail" class="pull-right btn btn-primary btn-xs" style="margin: 2px;"><i class="fa fa-plus"></i> Send Email</a>
</div>
</div>
<hr>
<?php foreach($result as $detail){ ?>
<table border="0" width="80%" align="center">
<tr>
<td width="5%"><?php echo $detail['id']; ?></td>
<td width="12%"><img src="<?php echo base_url(); ?>assets/images/Penguins.jpg" height="85" width="75"></td>
<td width="10%"><b>Price:</b> <?php echo number_format($detail['price'], 2, '.', ''); ?></td>
<td width="30%"><b>Name:</b> <?php echo $detail['name']; ?></td>
<td width="43%"><b>Descriptipn:</b> <?php echo $detail['description']; ?></td>
</tr>
</table>
<hr>
<?php } ?>
Buat file 1 lagi di dalam folder aplication/views
yang akan digunakan untuk mengisi body email
<style>
h1{
font-size:25px;
color:blue;
}
table{
margin-top:20px;
}
</style>
<h1 align="center"> Products Report </h1><hr>
<br/><br/>
<?php foreach($getInfo as $detail){ ?>
<table border="0">
<tr>
<td width="5%"><?php echo $detail['id']; ?></td>
<td width="12%"><img src="<?php echo base_url(); ?>assets/images/Penguins.jpg" height="85" width="75"></td>
<td width="10%"><b>Price:</b> <?php echo number_format($detail['price'], 2, '.', ''); ?></td>
<td width="30%"><b>Name:</b> <?php echo $detail['name']; ?></td>
<td width="43%"><b>Descriptipn:</b> <?php echo $detail['description']; ?></td>
</tr>
</table>
<hr>
<?php } ?>
dan yang terakhir, buat file model di folder aplication/models
yang digunakan untuk menarik data dari database yang digunakan untuk mengisi body email.
<?php
class Site extends CI_Model{
public function getProduct($id = ''){
$this->db->select('*');
$this->db->from('products');
if($id != ''){
$this->db->where('id',$id);
}
return $this->db->get()->result_array();
}
}
?>
Selesai sudah untuk konfigurasi kirim email menggukanakan excellent SMTP Rely , selanjutnya kalian dapat mencoba nya melalui browser.
-
Yii
berikut langkah langkah untuk mengirim email menggunakan excellent smtp relay dengan framework yii:
- silahkan download extention path Yii Mail.
- Extract hasil download tadi didalam extension folder.
- Edit file konfigurasi main.php dan sesuaikan isinya sesuai dengan kebutuhan.
'mail' => array(
'class' => 'ext.yii-mail.YiiMail',
'transportType'=>'smtp',
'transportOptions'=>array(
'host'=>'relay.excellent.co.id',
'username'=>'relay.vavai@excellent.co.id',
'password'=>'StdPwdStrong2021!',
'port'=>'587', // u can use 465 or 587 or 25
),
'viewPath' => 'application.views.mail',
),
Note: File dapat ditemukan di ‘/protected/views/mail’.
- Selanjutnya import extension tadi didalam file main.php.
'ext.yii-mail.YiiMailMessage',
- fungsi Controller yang digunakan untuk konfigurasi kirim email.
public function SendMail()
{
$message = new YiiMailMessage;
//this points to the file test.php inside the view path
$message->view = "test";
$sid = 1;
$criteria = new CDbCriteria();
$criteria->condition = "studentID=".$sid."";
$studModel1 = Student::model()->findByPk($sid);
$params = array('myMail'=>$studModel1);
$message->subject = 'My TestSubject';
$message->setBody($params, 'text/html');
$message->addTo('marissa@gmail.com');
$message->from = 'markketing@company.com';
Yii::app()->mail->send($message);
}
selanjutnya buat file test.php untuk mengetes kirim email.
<html>
<head>
</head>
<body>
Dear <?php
echo $myMail->studentName;
?>
<br>This is a test mail.
</body>
</html>
-
SWAKS
Kamu juga dapat mengirim email menggunakan Excellent SMTP relay dengan SWAKS , dan berikut ini adalah contoh mengirim email menggukan SWAKS:
- Pastikan Email Delivery sudah di konfigurasi untuk mengirim email.
- Pastikan SWAKS sudah terinstall.Ensure Swaks is installed. Proses instalasi berbeda tergantung pada sistem operasi yang Anda gunakan. Misalnya, jalankan perintah berikut untuk menginstal Swaks di Oracle Linux:
sudo yum install swaks -y
- Jalankan command dibawah ini untuk mengirim email test menggunakan SWAKS:
swaks --pipeline -tls --server <smtp.region.oraclecloud.com> --port <587 or 25> --auth-user '<username OCID from SMTP credentials>' --auth-pass '<password>' --from '<sender email address>' --to '<recipient email address>' --data '<email message>'
Contoh Penggunaan:
swaks --pipeline -tls --server mail.hits.co.id --port 587 --auth-user 'hcis@hits.co.id' --auth-pass 'hits123456' --from 'sender@example.com' --to 'recipient@example.com' --data 'From: sender@example.com\nDate: Thu, 13 Sep 2019\nSubject: Test Send\n\nTest email'.
Leave a Reply