Bagaimana mengirim email dengan phpmailer pada web yang telah dihosting?

Halo kak..
Saya mau bertanya bagaimana script untuk mengirim email yang benar? 
saya sudah coba untuk website saya yang dihosting, email tidak terkirim tapi masuk database. Sedangkan dengan localhost laptop saya email terkirim.

<?php
session_start();
include "../koneksi.php";
if (isset($_POST['daftar'])) {

    $id  = $_POST['id_pelanggan'];
    $nama  = $_POST['nama'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = md5($_POST['password']);

    $token = hash('sha256', md5(date('Y-m-d')));

    $sql_cek = mysqli_query($koneksi, "SELECT * FROM pelanggan WHERE email='" . $email . "'");
    $r_cek = mysqli_num_rows($sql_cek);
    if ($r_cek > 0) {
        header("Location: ../daftar_pel.php?mes=gagal");
    } else {

        $insert = mysqli_query($koneksi, "INSERT INTO pelanggan(pelanggan_id,nama,email,username,password,token,aktif) 
        VALUES('" . $id . "','" . $nama . "','" . $email . "','" . $username . "','" . $password . "','" . $token . "','0')");

        include("../mail_pel.php");

        if ($insert) {
            header("Location: ../daftar_pel.php?mes=berhasiltambah");
 
        }
    }
}

avatar arunoktavia

@arunoktavia

6 Kontribusi 1 Poin


Jawaban

Ini script mail_pel.php

<?php

include "koneksi.php";
if (isset($_POST['daftar'])) {

    $nama  = $_POST['nama'];
    $email = $_POST['email'];
}

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;


require '/home/marsdoor/public_html/vendor/vendor/phpmailer/phpmailer/src/Exception.php';
require '/home/marsdoor/public_html/vendor/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require '/home/marsdoor/public_html/vendor/vendor/phpmailer/phpmailer/src/SMTP.php';


$mail = new PHPMailer(true);


$email_pengirim = 'mu*****@gmail.com'; 
$nama_pengirim = 'M****'; 
$email_penerima = $email; 
$nama_penerima = $nama;

try {
    //Server settings
    $mail->SMTPDebug = 3;                      
    $mail->isSMTP();                                            
    $mail->Host       = 'smtp.gmail.com';                    
    $mail->SMTPAuth   = true;                                   
    $mail->Username   = $email_pengirim;                     
    $mail->Password   = '*******';                               
    $mail->SMTPSecure = 'tls';         
    $mail->Port       = 587;
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );                                  

    $mail->setFrom($email_pengirim, $nama_pengirim);
    $mail->addAddress($email_penerima, $nama);

    $mail->isHTML(true);
    $mail->Subject = "Aktivasi pendaftaran Member";
    $mail->Body = "Selamat, anda berhasil membuat akun. Untuk mengaktifkan akun anda silahkan klik link dibawah ini.";
    $mail->send();

    echo "oke";
} catch (Exception $e) {
    // header("Location: ../daftar_pel.php?mes=gagal");
    echo "gagal";
}

avatar arunoktavia

@arunoktavia

6 Kontribusi 1 Poin


Login untuk gabung berdiskusi