Jump to content

Using php and curl to post data to a form handler


BobSwede

Recommended Posts

Hi, my first post on this forum...

I have a form on a public webhost that has stopped working correctly.
The handler is written using php and it is supposed to send an email to me upon posting the form.
It was originally created many years ago and has worked well for all the time since.
But as of April 2021 it does not generate any emails anymore...
It used the simple mail() command to send the emails and I suspect that the webhosting company has blocked this simple mailing.

I have tested it the last few days trying to implement PHPmail instead but with no success, probably that is also blocked.
In order to get PHPmailer work at all (without php errors) I had to modify the webhost php version from 5.3 to 7.
But still it will not output the emails on the public server.

So now I want to test a workaround where I will move the form handler to my own webserver, which does work to send email using the PHPmailer package.
The workaround would be this:

1- I replace the mail() call in the original handler with a call to a new curl based function, which in turn posts to the form handler on my own webserver.

2- On my own webserver I create a new php handler accepting the data as the original handler does but it sends the email using PHPmail from my own server.

To do this I need to know how to handle the data on the first webserver concerning items containing both spaces and linefeeds.

I have looked at a number of examples for this and I think I get the gist of it except how to format the data to be sent out.
Ideally I should create the exact same POST data package as the source form on the first server uses and just forward it all to the other server's php handler.

So how can I get the full set of POST data into a variable which I can POST to the new "remote" handler?

Say I create a function CurlPost($targeturl, $data) where $data is an array of name-value pairs where the values may contain both speces and linefeeds, how would this handle the argument $data to put it into a curl call?

Here is the SendMessage function used on both the webservers which does not send mail on the public server but works ok on my own server
 

<?php
ini_set('display_errors', '1');    //Uncomment this if there are problems...

include 'logindata.php'; //Secret mail information stored here
include_once 'logdebug.php'; //Used to log data to a log file on the server

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

function SendMessage($subject, $sender, $sendername, $recipient, $copyto, $bccto, $body)
{
    global $mailserver, $mailsender, $mailpasswd; //From logindata.php
    
    $ret = FALSE;
    LogDeb("SendMessage called, will use these parameters for sending:\nMail server: " . $mailserver . "\nMail sender: " . $mailsender . "\nMailpwd: " . $mailpasswd);
    
    $mail = new phpmailer();

    $mail->isSMTP();
    $mail->Host = $mailserver;
    $mail->SMTPAuth = true;
    $mail->Username = $mailsender; // SMTP username
    $mail->Password = $mailpasswd; // SMTP password
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    $mail->CharSet = 'UTF-8';
    $mail->ContentType = 'text/plain';
    $mail->IsHTML(false);
    
    $mail->setFrom($sender, $sendername);
    $mail->addAddress($recipient); // Add a recipient, Name is optional
    $mail->addCC($copyto);
    $mail->addBCC($bccto);
    $mail->addReplyTo($sender, $sendername);

    $mail->Subject = $subject;
    $mail->Body = $body;
    LogDeb("Ready to send email now!"); //To check that I always get here without errors
    if(!$mail->send()) //Seems like the system hangs here
    {
        LogDeb("Message could not be sent.");  //Not logged....
        LogDeb("Mailer Error:\n" . $mail->ErrorInfo);
        //echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else
    {
        LogDeb('Message has been sent');
        $ret = TRUE;
    }
    return $ret;
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.