Page 1 of 1

Using Mailgate from another php script

Posted: Tue Sep 11, 2007 9:30 am
by pedaloz
I recently changed web hosting companies and the new one (Optus) will only let my website send an email from a form using mailgate (http://mailgate.server-mail.com/cgi-bin/mailgate).

The standard "mail" function I have been using won't work with Optus.

The mailgate script allows me to send one string as the email message to an email account that I have pre-registered.

My website uses an online booking form that has many many fields and my existing script formats these nicely into an email that looks like my normal booking form.

I would like to keep my original php script to read the form and format the message body, then swap the "mail" command to call the mailgate script that I have to use.

I am trying...

Code: Select all

$redurl='http://pedaloz.com.au';
	$rep='info@pedaloz.com.au';
	$from=... read from form
	$name=... read from form
	$sub='Online Booking';
	$msg= ... lots of code to format all the form fields into my desired email body ...

header("Location: http://mailgate.server-mail.com/cgi-bin ... ct=$redurl");
	exit();
But this returns an error from the mailgate site saying I have not supplied a recipient.

Is the header (Location ...) the most appropriate command to use?

Any clues? All feedback much appreciated.

:cry:

Posted: Tue Sep 11, 2007 10:57 am
by Begby
You need to properly escape your parameters before passing them to the URL. For instance http:// needs to be escaped along with the @ symbol.

See http://us3.php.net/manual/en/function.urlencode.php

Posted: Tue Sep 11, 2007 10:58 am
by Begby
Also, this will redirect them to the mailgate page. However, you can use curl to send the request without redirecting the user, and also use curl to 'post' the parameters instead of sending them on the URL. I am not sure how the mailgate script works, but there is a chance that it might not check the URL.

Posted: Tue Sep 11, 2007 10:15 pm
by pedaloz
Thanks so much for the help. I've never used curl before but it works! Excellent!

The only problem I have, as the second post predicted, the redirecting. The mailgate script is supposed to redirect to my supplied url, but first displays a page saying my url has moved. It provides a link that works, but its not professional.

I tried overriding the curl location commands but as I have it in my code (commented out below) it doesn't work. If I don't supply mailgate with the redirect field, it takes redirects to its own page.

Here is my code now...

Code: Select all

<?
function do_formmail(){
	$recipient=urlencode('info@pedaloz.com.au');
	$subject=urlencode('test email from mailgate');
	$email=urlencode('bakerj29@hotmail.com');
	$realname=urlencode('jo test');
	$subject=urlencode('test email from mailgate');
	$comments=urlencode('message content');
	$redirect=rawurlencode('http://www.pedaloz.com.au/Pedal_OZ_Response.html');

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL,"http://mailgate.server-mail.com/cgi-bin/mailgate");
	//curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
	//curl_setopt($curl, CURLOPT_MAXREDIRS, 0);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, "recipient=$recipient&subject=$subject&email=$email&realname=$realname&comments=$comments&redirect=$redirect");

	curl_exec ($curl);
	curl_close ($curl);	

	//$redirect_url='http://www.pedaloz.com.au/Pedal_OZ_Response.html';	
	//global $redirect_url;
    //header("Location: $redirect_url");
    exit();
}


do_formmail();
?>
Any more ideas?