0

I am trying to send emails using PHPMailer, but I'm not being able to. Actually I'm working on existing system already built by other senior developers. Here's what I tried :

$mail = new PHPMailer(true);
    try {
        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->isHTML(true);
        $mail->addAddress("rmali@growbydata.com");
        $mail->addAddress("pshrestha@growbydata.com");
        $mail->addAddress($userEmail);

        $mail->Host = $f3->get('GBD.smtp.host');   // Specify main and backup SMTP servers
        $mail->From = $user['email'];
        $userFullName = trim(ucfirst($user['firstname'])) . " " . trim(ucfirst($user['lastname']));
        $mail->FromName = $userFullName;
        $mail->Body =  "<br><br>".$f3->get('message');
        $mail->Body .="<br>". $mailBody;
        $mail->AltBody = 'Employee has requested for assistance. Please comply.
        Issued Date:' . $f3->get('issuedDate') . '<br>Issued Selection:' . $f3->get('issueSelection') . '<br>Issue Priority:' . $f3->get('issuePriority') . '<br>Issue Description:' . $f3->get('issueDescription');
        $mail->Subject = 'Updates on issue report';

        $mailStatus = (boolean)$mail->send();

        if ($mailStatus === true) {
            return $mail; //object is being sent here.. anyone know this?
        }
    } catch (phpmailerException $e) {
        $response = array(
            'status'=>'error',
            'message'=>'Got some error while sending emails',
            'exceptions'=>$e->getMessage()
        );
        return $response;

    }
}

Here's how I am trying to send mail from the controller:

$emailStatus = EmailHelper::issueMail();        //send email
          if($emailStatus==TRUE){
              echo 'this works though';
              exit;
          }

$mail PHPMailer instance is being sent instead of boolean 'emailStatus' but it seems to be working, and I don't know why. But I am not receiving any emails. Also my SMTP configuration in config.ini file is as follows:

GBD.smtp.host = smtp.wlink.com.np

; Will receive all exported logs as an email
;GBD.smtp.exportemail = "trourk@growbydata.com"


GBD.smtp.email[0] = "malakar.rakesh1993@gmail.com"
;GBD.smtp.email[1] = "pramitshrest@gmail.com"


GBD.smtp.password = ""
GBD.smtp.from = "noreply@growbydata.com"
GBD.smtp.fromName = "NoReply GrowByData"
GBD.smtp.subject = "Leave Request Application"
GBD.smtp.email_template = ""

I checked everything but no luck. Please help me with this.

sascha
  • 4,671
  • 3
  • 36
  • 54
Azima
  • 3,835
  • 15
  • 49
  • 95
  • You're returning `$mail` instead of `$mailStatus` which is why you're getting the PHPMailer instance instead of a boolean. As for the email, I can't see where you're setting the username and password - most mail providers don't allow unauthenticated emails to be sent for obvious reasons. – crazyloonybin Oct 16 '17 at 11:05
  • can object be treated and compared as a boolean? – Azima Oct 16 '17 at 11:08
  • You can, but it will always evaluate to true (so long as the object has been instantiated to a non-falsey value). See [this answer](https://stackoverflow.com/a/5572895/1561469) for details. – crazyloonybin Oct 16 '17 at 11:11
  • ohh.. that's why it is always being TRUE... thanks for this help :) – Azima Oct 16 '17 at 11:13
  • Ohh.. but again.. for that to be sent.. $mailStatus = (boolean)$mail->send(); this has to be true.. which means mail has been sent... but I'm not getting any... hahah... its driving me crazy all day. – Azima Oct 16 '17 at 11:15
  • No problem. Like I said in the first comment though, I can't see where you're setting the username and password for sending the email, which may be why you're not receiving the email. – crazyloonybin Oct 16 '17 at 11:16
  • Have you tried any [debugging](https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging) to see if the email is being sent successfully without errors? – crazyloonybin Oct 16 '17 at 11:34
  • 1
    I finally made it with SMTP gmail configurations.. thanks for your help :) – Azima Oct 16 '17 at 11:46
  • Glad you got it sorted in the end :) – crazyloonybin Oct 16 '17 at 11:47
  • Doing `$mail->From = $user['email'];` is probably wrong - if these addresses are not on your own domain, it's forgery, and you're likely to get bounced by SPF checks. – Synchro Oct 16 '17 at 11:51

0 Answers0