DaisyLou has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perlmonks,

I have a file, sendMail.php, that contains one function sendMail ($fromName, $fromEmail, $to, $cc, $bcc, $subject, $messageHtml, $messageText).

I want to call the sendMail function from within Perl.

Why am I not using a Perl library you ask? Because (a) I want a single interface to email; and (b) getting Perl to play nicely with Amazon SES is proving nearly impossible.

Is there a way I can use my PHP sendMail() function to do what I way to do?

  • Comment on Calling a PHP function from within Perl

Replies are listed 'Best First'.
Re: Calling a PHP function from within Perl
by 1nickt (Canon) on Jul 28, 2019 at 00:38 UTC

      ++. There's even an actual Perl tab within the documentation.

      Thanks. I did try that, among many other things. I can't remember if the CPAN was failing for that one or if it was something else, but I couldn't get it to play nice with SES. :(

        I would try again, and capture your errors if there are any so others can help debug. It's certainly no problem using SES with Perl; in my experience the most frequent error when using an AWS API is connecting to the wrong region. What did you pass for host?


        The way forward always starts with a minimal test.
Re: Calling a PHP function from within Perl
by tobyink (Canon) on Jul 27, 2019 at 21:00 UTC

    Probably your best bet is to write a helper script called something like sendMail-cli.php something like this:

    <?php require_once 'sendMail.php'; extract(json_decode(file_get_contents('php://stdin'))); sendMail($fromName, $fromEmail, $to, $cc, $bcc, $subject, $messageHtml +, $messageText);

    And then from Perl, you can pass data to that:

    use JSON; my %mail = ( fromName => 'Alice', fromEmail => 'alice@example.com', to => 'bob@example.net', cc => 'carol@example.net', bcc => 'dave@example.org', subject => 'Test message', messageHtml => '<p>Test</p>', messageText => "Test\n", ); open my $sm, '|php sendMail-cli.php' or die 'could not open pipe to se +nd mail'; print {$sm} encode_json(\%mail); close $sm;

    There are probably nicer ways to do it, but that's quick and easy.

    Code is untested, but should give you a general idea.

    (For bonus points, make sure that sendMail-cli.php cannot be called directly via your webserver, to avoid creating a spam gateway.)

      Many thanks! I'll give it a try tomorrow!

        If you need help with avoiding creating a spam gateway, let me know, but that's probably a longer conversation.

Re: Calling a PHP function from within Perl
by Anonymous Monk on Jul 30, 2019 at 17:46 UTC
    "Don't use PHP. Figure out the problem you're having with Perl." It's that simple. Otherwise the people who hold your job after you will burn you in effigy.