-1

I am using Swiftmailer to send an email and I currently have a try/catch block to see if the Email Addresses are valid based on the Swift_RfcComplianceException. I currently catch the error and output the $e->getMessage() error on the screen. On the form submission for the email I have a variable set up to spit out a success message if the email is sent successfully.

QUESTION: How do I use the same flash message area to output an error message based on whether or not my Swiftmail file has caught an exception. And how do I make this occur on the same page? I am not using AJAX but rather Page Post Back. Right now all I get is a white screen with text, I want the error message to be where the form is submitted from. Thank you.

HTML

<?php if(isset($_SESSION['message'])):?>
            <div id="success-alert" class="alert alert-success text-center">
                <?php
                    echo $_SESSION['message'];
                    unset($_SESSION['message']);
                ?>
            </div>

        <?php elseif(isset($_SESSION['email_error'])) :?>
            <div id="success-alert" class="alert alert-success text-center">
                <?php
                    echo $_SESSION['email_error'];
                    unset($_SESSION['email_error']);
                ?>
            </div>
        <?php endif; ?>

Swiftmailer

try {
    // Create a message
    $message = (new Swift_Message($_POST['subject']))
        ->setFrom('ME@YOU.COM')
        ->setTo($finalEmailList) //Array of email address
        ->setBody($_POST['message'], 'text/html')
        ->setReplyTo('you@me.com');
} catch (Swift_RfcComplianceException $e) {
    print($e->getMessage());
}

PHP

if (isset($_POST['submit_email'])) {
        require_once 'views/Swiftmail.php';
        $_SESSION['message']= "Email Sent Successfully";
        $_SESSION['email_error'] = "Invalid Email Address Entered";
}
CodeConnoisseur
  • 1,452
  • 4
  • 21
  • 49
  • What exactly is stopping you from setting the message the same way? – Patrick Q Dec 18 '19 at 21:59
  • The success message happens if an email goes through successfully so it redirects back to the same page then a message is spit out. If the email fails to send, the exception stops the form submission, and basically dies on the exception catch block and just spits out the error message without continuing back to the same page from which the form was submitted – CodeConnoisseur Dec 18 '19 at 22:01
  • Echo the desired element in the exception block..? – Libra Dec 18 '19 at 22:02
  • @Laif, you mean the html div (error) from above? – CodeConnoisseur Dec 18 '19 at 22:03
  • 1
    "the exception stops the form submission" No, it doesn't. The form has already been submitted at that point. Everything after the `catch` will still happen (unless you explicitly `die` or `exit` in the `catch`, which your code above doesn't show you doing). So just set your error message in the `catch`. – Patrick Q Dec 18 '19 at 22:03
  • So instead of having "catch (Swift_RfcComplianceException $e) { print($e->getMessage()); }" what do I put in this catch block? A
    flash message?
    – CodeConnoisseur Dec 18 '19 at 22:04

1 Answers1

1

You just move the error message setting lines from your "main" PHP code ...

session_start();
if (isset($_POST['submit_email'])) {
    require_once 'views/Swiftmail.php';
}

to your Swiftmail.php file ...

try {
    // Create a message
    $message = (new Swift_Message($_POST['subject']))
        ->setFrom('ME@YOU.COM')
        ->setTo($finalEmailList) //Array of email address
        ->setBody($_POST['message'], 'text/html')
        ->setReplyTo('you@me.com');
    $_SESSION['message']= "Email Sent Successfully";
} catch (Swift_RfcComplianceException $e) {
    $_SESSION['email_error'] = "Invalid Email Address Entered";
}

FYI, most modern PHP frameworks have flash message components built in.

Patrick Q
  • 6,373
  • 2
  • 25
  • 34
  • So in my "main PHP" Do I just remove the 2 $_SESSION['message'] and $_SESSION['email_error'] but keep the if (isset($_POST['submit_email'])) { require_once 'views/Swiftmail.php'; } in main? – CodeConnoisseur Dec 18 '19 at 22:10
  • Now I am getting this error: "Undefined variable: message on line 54" which is this line: " $result = $mailer->send($message);" – CodeConnoisseur Dec 18 '19 at 22:14
  • Well, that's not in your code above and sounds like a different question/problem. But you seem to need to back up and learn some basic debugging skills. "undefined variable" errors are exactly what they say they are; you are trying to use a variable that hasn't been defined (at least not within the [scope](https://www.php.net/manual/en/language.variables.scope.php) that you're trying to use it). Questions about errors like that are almost always closed as duplicates of [this](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined). – Patrick Q Dec 18 '19 at 22:17
  • 1
    You're welcome. Do note the edit to my above comment though. – Patrick Q Dec 18 '19 at 22:20