I am a beginner in PHP development, and I am following a tutorial to send an email from localhost using xampp
. initially I can send an email using postfix
and gmail, but after 2 weeks, it doesn't work anymore.
I configure the postfix
mta
by modifying main.cf
file using this line of code:
in main.cf file
# Gmail SMTP
relayhost=smtp.gmail.com:587
# Enable SASL authentification in the Postfix SMTP
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/
sasl_passwd
smtp_sasl_security_options=noanonymous
smtp_sasl_mechanism_filter=plain
# Enable TLS i.e. SSL
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom
I have set Allow less secure apps to be enable on gmail. I also make sasl_passwd
file that contains email account and its password
smtp.gmail.com:587 fake@gmail.com:password123456
It could work perfectly for around 1 week, and then it stops working, I got an email from gmail about Critical security alert, but after giving permission, it can work normally again.
But now, 2 weeks after that, I can't send an email again using my postfix
. and there is no notification again from gmail. In terminal / command line I have been trying to send and email using this command:
sudo postfix stop
sudo postfix start
sudo postfix reload
date | mail -s testing someemail@gmail.com
but it can't send an email.
here is my PHP code when sending an email
<?php
class email {
// membuat token untuk konfirmasi email
function generateToken ($length) {
$characters = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
// mendapatkan panjang karakter
$charsLength = strlen(($characters));
$token = "";
for($i = 0; $i < $length; $i++) {
$token .= $characters[rand(0,$charsLength-1)];
}
return $token;
}
// membuka template isi email dari html file untuk konfirmasi registrasi
function confirmationTemplate() {
$file = fopen("templates/confirmationTemplate.html", "r") or die ("unable to open file");
$template = fread($file, filesize("templates/confirmationTemplate.html"));
fclose($file);
return $template;
}
// membuka template isi email dari html file untuk melakukan reset password
function resetPasswordTemplate() {
$file = fopen("templates/resetPasswordTemplate.html", "r") or die ("unable to open file");
$template = fread($file, filesize("templates/resetPasswordTemplate.html"));
fclose($file);
return $template;
}
// mengirim email dengan PHP
function sendEmail($details) {
// isi informasi email
$subject = $details["subject"];
$to = $details["to"];
$fromName = $details["fromName"];
$fromEmail = $details["fromEmail"];
$body = $details["body"];
//email header
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-type:text/html;content=UTF-8"."\r\n";
$headers .= "From: ".$fromName."<".$fromEmail.">"."\r\n";
// PHP function send email
mail($to,$subject,$body,$headers);
}
}
?>
What went wrong in here?