...

View Full Version : PHP Mailer Formatting Issues...



moktoman
06-29-2007, 07:03 AM
Ok, I will try to put this as simply as possible:

I have a mailer script I got from a website somewhere (I don't recall where exactly) that is successfully sending emails to a specific email account, the content of which is given from an HTML feedback form on my server.

Here is the script:



<?php

$To = 'webtester800@mywebserver.com';
$SmtpServer = 'smtp.mywebserver.com';
$SmtpUserid = 'webtester800@mywebserver.com';
$SmtpPassword = '******';
$NextPage = 'fbgood.html';
$Debug = 0;


function ReceiveOnSocket($Socket,$Ok,$Debug) {
$Line = '';
do {
$Result = fgets($Socket,10000);
if ($Result == FALSE) return('Error reading from socket');
$Line = $Line . $Result;
if ($Debug > 0) echo '<<< ' . htmlentities(trim($Result)) . "\n";
} while ($Result[3] == '-');
if (intval($Line) != $Ok) return($Line);
return('');
}


function SendOnSocket($Socket,$Message,$Debug) {
if ($Debug > 0) echo '>>> ' . htmlentities(trim($Message)) . "\n";
if (fputs($Socket,$Message) == FALSE) return('Error sending: ' . $Message);
return('');
}


function SendEmail(
$SmtpServer,
$SmtpPort,
$Userid,
$Password,
$To,
$From,
$ReplyTo,
$Subject,
$Body,
$Debug) {


if ($SmtpServer == '') return('No SmtpServer specified');
if ($SmtpPort == '') return('No SmtpPort specified');
if ($To == '') return('No TO address specified');
if ($From == '') return('No FROM address specified');
if ($ReplyTo == '') return('No REPLYTO address specified');
if ($Subject == '') return('No SUBJECT specified');
if ($Body == '') return('No BODY specified');


$Socket = fsockopen($SmtpServer,$SmtpPort,&$errno,&$errstr);
if ($Socket == FALSE) return('Cannot connect to mailserver');


$Status = '';
if ($Status == '') $Status = ReceiveOnSocket($Socket,220,$Debug);
if ($Status == '') $Status = SendOnSocket($Socket,'HELO ' . $SmtpServer . "\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,250,$Debug);

if (($Userid != '') && ($Password != '')) {
if ($Status == '') $Status = SendOnSocket($Socket,"AUTH LOGIN\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,334,$Debug);
if ($Status == '') $Status = SendOnSocket($Socket,base64_encode($Userid) . "\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,334,$Debug);
if ($Status == '') $Status = SendOnSocket($Socket,base64_encode($Password) . "\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,235,$Debug);
}

if ($Status == '') $Status = SendOnSocket($Socket,'MAIL FROM:<' . $From . '>' . "\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,250,$Debug);
$a = explode(',',$To);
for ($i = 0; $i < count($a); $i++) {
if (trim($a[$i]) == '') continue;
if ($Status == '') $Status = SendOnSocket($Socket,'RCPT TO:<' . trim($a[$i]) . '>' . "\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,250,$Debug);
}
if ($Status == '') $Status = SendOnSocket($Socket,"DATA\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,354,$Debug);
if ($Status == '') $Status = SendOnSocket($Socket,
'To: ' . $To . "\r\n" .
'From: ' . $From . "\r\n" .
'Reply-To: ' . $ReplyTo . "\r\n" .
'Subject: ' . $Subject . "\r\n" .
'Date: ' . date('D, d M Y H:i:s ') . "\r\n" .
"\r\n" .
trim($Body) .
"\r\n.\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,250,$Debug);
if ($Status == '') $Status = SendOnSocket($Socket,"QUIT\r\n",$Debug);
if ($Status == '') $Status = ReceiveOnSocket($Socket,221,$Debug);


if ($Socket) fclose($Socket);


return ($Status);
}



if (isset($HTTP_GET_VARS)) {
while ($a = each($HTTP_GET_VARS)) {
if ($a[0] == 'From') continue;
if ($a[0] == 'Subject') continue;
if ($a[0] == 'Submit') continue;
$Body .= $a[0] . "\t= " . $a[1] . "\r\n";
}
}
if (isset($HTTP_POST_VARS)) {
while ($a = each($HTTP_POST_VARS)) {
if ($a[0] == 'From') continue;
if ($a[0] == 'Subject') continue;
if ($a[0] == 'Submit') continue;
$Body .= $a[0] . "\t= " . $a[1] . "\r\n";
}
}


$Subject = ereg_replace("[^-[:alnum:]_.!@=* ]","",$Subject);
$From = ereg_replace("[^-[:alnum:]_.!@=* ]","",$From);



if ($Debug != 0) echo '<pre>';
$Status = SendEmail($SmtpServer,25,$SmtpUserid,$SmtpPassword,
$To,$From,$From,$Subject,$Body,$Debug);
if ($Debug != 0) echo '</pre>';
if ($Status != '') {
if ($Debug == 0) echo '<html><body>' . "\n";
echo '<b>An error has occurred while sending the email.' . "\n";
echo 'The status message is:</b>' . "\n";
echo '<p>' . "\n";
echo htmlentities($Status,ENT_QUOTES) . "\n";
echo '</body></html>' . "\n";
exit;
}



if ($Debug == 0) header('location: ' . $NextPage);
?>


Now, my issue is that the emails that this script is sending has the content formatted something like this:



Great websiteName = John
Body = Great website
Submit_x = 22
Submit_y = 4


Optimistically, I would like the emails to not have the "Name =","Body =","Submit =" etc., plus the message or body content repeats twice. Once at beginning and once after "Body ="...

I am not very knowledgeable in PHP scripting, so I am not sure what part of the script is formatting the emails, and what needs to be changed to remedy the situation...?

Thanks in advance for any help you guys can give me!!
- Michael

Fumigator
06-29-2007, 07:25 AM
It looks like someone copy/pasted this bit of code twice on accident:



if (isset($HTTP_GET_VARS)) {
while ($a = each($HTTP_GET_VARS)) {
if ($a[0] == 'From') continue;
if ($a[0] == 'Subject') continue;
if ($a[0] == 'Submit') continue;
$Body .= $a[0] . "\t= " . $a[1] . "\r\n";
}
}
if (isset($HTTP_POST_VARS)) {
while ($a = each($HTTP_POST_VARS)) {
if ($a[0] == 'From') continue;
if ($a[0] == 'Subject') continue;
if ($a[0] == 'Submit') continue;
$Body .= $a[0] . "\t= " . $a[1] . "\r\n";
}
}


All you need to do is remove all that code and replace it with


$Body = $_POST['Body'];

(Assuming your form's element is named Body as is implied but not shown)

moktoman
06-29-2007, 11:59 AM
Thanks, Fumigator.

However, I need to make sure the "Name" string is in the body of the email message as well (The name is "John" in the example from my previous post).

For example, with the code you gave me, I just get an email that says "Great website" and nothing else.

What do I need to add in order to get it looking like "John, Great website" (this comes from the Name field in the form which has the same string name in the PHP script respectively) or "John," and then "Great website" on the next line ?

In other words, how do I get the input from the Name field and Body fields of the feedback form to both go into the message body of the email being sent?

- Michael

Fumigator
06-29-2007, 06:33 PM
I just noticed the code I thought was duplicate code is actually checking both the POST and GET arrays so it's not duplicated, just flexible. Ooops...

Anyways, you can build the body of the email however you want it to look by using simple variable assignments. $Body is the name of the variable that ends up in the body of your email. The form input comes into the script via the $_POST or $_GET arrays, using the names of the form elements. So if you have a form element <input type="text" name="fuzzydice"> and submit the form with action="post", then the $_POST array will contain an index "fuzzydice": $_POST['fuzzydice'].

To "glue" strings together, use the period, like this: $Body = 'hi' . ' ' . 'there'; You can also imbed a variable within a string, like this (you must use double-quotes when imbedding): $Body = "You entered {$_POST['Name']} as the name.";

moktoman
06-29-2007, 11:52 PM
No big deal.
That original code wasn't working right anyway, so I'm glad to be rid of it.

I'll go ahead and try your suggestions, and see if I can't get it looking nice and proper...

Thanks again.
-Michael

moktoman
06-30-2007, 01:59 AM
Okay, one last thing.

The code looks like this now:



$Body = "Name = {$_POST['Name']} Message = {$_POST['Body']}";

Of course, this works, but then everything is all displayed on one line in the resulting email.

Is there a way to tell the script to add a line break in the email before the "Message =" portion?

- Michael



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum