0

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?

Alexa289
  • 8,089
  • 10
  • 74
  • 178
  • Stack Overflow is for *programming* questions. Voting to close as off-topic. Maybe try at our sister site [sf] but they (too) will probably want to see some logs. – tripleee Dec 31 '17 at 22:45
  • @tripleee https://serverfault.com/questions/890222/cant-send-email-anymore-using-gmail-and-postfix-from-localhost i have asked in serverfault, but they said it shall be asked in stackoverflow if the questions are about development, testing and development tools – Alexa289 Jan 01 '18 at 01:36
  • If the problem manifests without the PHP parts, it's not a programming issue. Maybe you confused them by posting code instead of useful diagnostics. – tripleee Jan 01 '18 at 06:20
  • Anyway, their closure message is a generic one. Their beef is probably more about "I'm a beginner... running on localhost". Perhaps then try [su] or [unix.se] but again, concentrate on the Gmail problem and useful logs (though perhaps link back to here for context, too). – tripleee Jan 01 '18 at 06:23
  • And I agree with their recommendation to use a mailing service if this is something you are at all serious about, and/or you are not specifically trying to build up your skills in administering an email service. Most email providers have very generous free tiers these days. – tripleee Jan 01 '18 at 06:26
  • Points for trying to find the right site, but if yur code worked and you haven't changed it, chances are your problem is not code-related. – tripleee Jan 01 '18 at 06:32
  • Having said that, repeatedly sending identical test messages has been known to trip Gmail's bulk rejection automations before. But again, without logs or error messages, we are in the dark here. – tripleee Jan 01 '18 at 06:34

0 Answers0