Page 1 of 1

Having a PHP script conect to sendmail

Posted: Sat May 25, 2002 3:04 pm
by robert4442
I am a newbe to php and need some very discritpive help.
I am using a tellafriend script on a unix server but I don't know how to get the php script to use my sendmail. The script seems to work with reguard to the form and the redirect but it does not send the email. My hosting company tells me it has to link with cgi. My sendmail is in /usr/sbin/sendmail. Can anyone help me get this script going? I will supply any further info that is needed to get this going.
thanks

Posted: Sat May 25, 2002 4:14 pm
by NetDragonX
Hi,

You're not using your own sendmail program are you? If not, just use PHPs built-in mail function (the hosting company should have already set this up properly).

In a PHP file, you can send mail to any address by using the following setup:

Code: Select all

<?php
mail("myemail@domain.com","Subject","The entire message...");
?>
When you use forms to send mail, you can use the same type of thing:

Code: Select all

<?php
$subject = htmlspecialchars(nl2br($subject));
$body = htmlspecialchars(nl2br($body));
mail("techsupport@yoursite.com",$subject,$body);
?>
Also you can add additional headers to the email with the 4th field:

mail($email, $subject, $body, $more_headers);

Hope this helps.

Posted: Sat May 25, 2002 4:52 pm
by robert4442
Thanks Dragon. I will try what you said. If I don't get it workingI hope you will allow me to come back to you.

Posted: Sat May 25, 2002 5:06 pm
by NetDragonX
No problem, Robert. You can always check PHP.Net for the rest of the things you can do with the mail function.

See ya

Posted: Sun May 26, 2002 9:12 pm
by robert4442
I got the mail function working but have another problem.

I have a script that recommends a site on a web page. It uses two files refer6.html and refer6.php. (see below for the script of both files.

The script allows someone to recommend up to four people. It stores the results in a txt file and then it emails the recommendations and out puts to the browser.

It does all this with some problems. It does not send all four emails. It also repeats a message in the browser for each recommended person. I will like this not to repeat itself, but list only the emails to and form.

I tried also to see if I could get the script to redirect back to the website, nut I keep getting the following message:

Warning: Cannot add header information - headers already sent by (output started at /home/lowcostc/public_html/refer7.php:2) in /home/lowcostc/public_html/refer7.php on line 13
E-mail sent to robert4442@comcast.net from robert4442@comcast.net, they now know how to save up to 70% on their prescription drugs from lowcostcanadianrx.com

I tried moving the hearder script to just before the echo so it is called before the output to the browser., but it does not work.

I also have tried to define more variables like:
$subject=”Put subject Txt here”;
$body=”put your message here”;

I wanted to put $subject in the line -- mail("$email[$numitems]", "Save up to 70% on your prescription drugs!", "Hi – to replace save up to 70% on your prescription drugs!
And to I want to put $body in place of the text that starts with – Hi $friendsfirstname[$numitems]: I recently found a way to save up to 70% on my prescription drugs. I thought you would like to know how also. Please visit $siteaddress.
$sendersfirstname $senderslastname

This is my first try at a script this involved (for me anyway). Can someone help me?

Refer6.html:

<form action="refer6.php" method="post">
<table width="81%" border="0" cellpadding="5" align="center">
<tr>
<td><b><font size="5">*</font>Required </b></td>
</tr>
</table>
<table width="82%" border="0" cellpadding="5" align="center" bgcolor="#00CCFF">
<tr>
<td width="24%">
<div align="right"><b>You</b></div>
</td>
<td width="40%">
<div align="right">First Name<b><font size="5">*</font></b>:</div>
</td>
<td width="36%">
<input type="text" name="sendersfirstname">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">Last Name<b><font size="5">*</font></b>: </div>
</td>
<td width="36%">
<input type="text" name="senderslastname">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">E-mail<b><font size="5">*</font></b>:</div>
</td>
<td width="36%">
<input type="text" name="senderemail">
</td>
</tr>
<tr>
<td width="24%">
<div align="right"><b>Friend #1</b></div>
</td>
<td width="40%">
<div align="right">First Name<b><font size="5">*</font></b>:</div>
</td>
<td width="36%">
<input type="text" name="friendsfirstname[1]">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">Last Name<b><font size="5">*</font></b>:</div>
</td>
<td width="36%">
<input type="text" name="friendslastname[1]">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">E-mail<b><font size="5">*</font></b>:</div>
</td>
<td width="36%">
<input type="text" name="email[1]">
</td>
</tr>
<tr>
<td width="24%">
<div align="right"><b>Friend #2</b></div>
</td>
<td width="40%">
<div align="right"> First Name:</div>
</td>
<td width="36%">
<input type="text" name="friendsfirstname[2]">
</td>
</tr>
<tr>
<td width="24%" height="39">&nbsp;</td>
<td width="40%" height="39">
<div align="right">Last Name:</div>
</td>
<td width="36%" height="39">
<input type="text" name="friendslastname[2]">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">E-mail:</div>
</td>
<td width="36%">
<input type="text" name="email[2]">
</td>
</tr>
<tr>
<td width="24%">
<div align="right"><b>Friend #3</b> </div>
</td>
<td width="40%">
<div align="right"> First Name:</div>
</td>
<td width="36%">
<input type="text" name="friendsfirstname[3]">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">Last Name:</div>
</td>
<td width="36%">
<input type="text" name="friendslastname[3]">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">E-mail:</div>
</td>
<td width="36%">
<input type="text" name="email[3]">
</td>
</tr>
<tr>
<td width="24%">
<div align="right"><b>Friend #4</b></div>
</td>
<td width="40%">
<div align="right"> First Name:</div>
</td>
<td width="36%">
<input type="text" name="friendsfirstname[4]">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">Last Name:</div>
</td>
<td width="36%">
<input type="text" name="friendslastname[4]">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">E-mail:</div>
</td>
<td width="36%">
<input type="text" name="email[4]">
</td>
</tr>
<tr>
<td width="24%">&nbsp;</td>
<td width="40%">
<div align="right">
<input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="Reset">
</div>
</td>
</tr>
</table>
</form>

refer6.php:

<!---Begin refer.php--->
<?
//set variables
$sitename="lowcostcanadianrx.com";
$siteaddress="http://lowcostcanadianrx.com";
$filename="url-log.txt";
//cycle through the email address and names
$items="5";
$tally="";
for($numitems=0;$numitems<$items;$numitems++){
if(($friendsfirstname[$numitems]!="")&&($friendslastname[$numitems]!="")&&($email[$numitems]!="")){
//php mail function
mail("$email[$numitems]", "Save up to 70% on your prescription drugs!", "Hi $friendsfirstname[$numitems]: I recently found a way to save up to 70% on my prescription drugs. I thought you would like to know how also. Please visit $siteaddress.
$sendersfirstname $senderslastname", "From: \"$sendersfirstname\" $senderslastname \"$senderemail\n");
echo "E-mail sent to $email[$numitems] from $senderemail, they now know how to save up to 70% on their prescription drugs from $sitename";
//Logging to a file
//Be sure to upload the blank test file that the data will write to. I this case it is called url-log.txt
$fl=fopen($filename,"a+");
fwrite($fl,"'$sitename'|'$friendsfirstname[$numitems]'|'$friendslastname[$numitems]'|'$email[$numitems]'|'$sendersfirstname'|'$senderslastname'|'$senderemail'\n");
fclose($fl);
$tally="$friendsfirstname[$numitems],";
}
}

?>
<!---End refer.php--->

the header section is:

header("Location: http://lowcostcanadianrx.com/");

where do I put this to work. It most likely will do away with the echo script section.

Posted: Mon May 27, 2002 3:03 am
by NetDragonX
I'm just going to rewrite your refer6.php file instead of going through the steps and telling you 'where to put it'. You'll probably find it easier that way. First I'll tell you why you were having the problems you were.

Q "It also repeats a message in the browser for each person."
A You have the echo statement within the loop so PHP naturally thinks you want it to display it 4 times.

Q "I tried also to see if I could get the script to redirect back to the website, but I keep getting error messages."
A You can't place a header within a loop and expect it to know what's going on... and you also can't place it after anything has been sent to output. So if you want to automatically redirect, place it at the top of the script and it will forward when the page finishes loading. In this case though, since you want a list of the emails sent to be visible, maybe you should use a META redirect and have it redirect to the main site after a few seconds ( <META HTTP-EQUIV="refresh" CONTENT="5;http://www.domain.com"> ).

Q "I wanted to use $subject and $body in the mail function.
A Easy. You can change the mail function to look like mail($email, $subject, $body, $other); and it will work fine. You'll notice the change below.

Code: Select all

&lt;?php
// Setup header
$url = "http://www.lowcostcanadianrx.com";
Header("Location: $url");

// Variables
$sitename="lowcostcanadianrx.com"; 
$siteaddress="http://lowcostcanadianrx.com"; 
$filename="url-log.txt"; 
$items = "5"; 
$tally = "";
$subject = "Save up to 70% on your prescription drugs!";

echo "&lt;p&gt;These people now know how to save up to 70% on their prescription drugs from $sitename:&lt;br&gt;";

// Begin reiterations
for( $i=0; $i &lt; $items; $i++ )
{ 

   if ( $friendsfirstname&#1111;$i] AND $friendslastname&#1111;$i] AND $email&#1111;$i] )
   { 
      // Body and mail_header variables
     $body = "Hi $friendsfirstname&#1111;$i]: Message here.";
     $mail_headers = "From: $senderemail"); 

      // Mail function 
      mail($email&#1111;$i], $subject, $body, $mail_headers);
      echo $email&#1111;$i]."&lt;br&gt;";

      //Logging to a file 
      $fl=fopen($filename,"a+"); 
      fwrite($fl,"'$sitename'|'$friendsfirstname&#1111;$i]'|'$friendslastname&#1111;$i]'|'$email&#1111;$i]'|'$sendersfirstname'|'$senderslastname'|'$senderemail'\n"); 
      fclose($fl); 
      $tally="$friendsfirstname&#1111;$i],"; 
   } 
} 
?&gt;
Try that. Reply back if the mail still doesn't get sent.

Posted: Mon May 27, 2002 2:01 pm
by robert4442
Thanks for your help.

Iput the script in and I am getting the following.

Parse error: parse error in /home/lowcostc/public_html/refer7.php on line 24

I saw a period after echo $email[$i]."<br>";
I tried changing it to a "," but that did not change anything.

When you get a parse error on line xx. do you count the spaces and the comments lines?

Posted: Mon May 27, 2002 3:22 pm
by volka
a) echo $var."<br>" is the concatenation of the strings, ie: $var = "nothing" -> "nothing<br>"

b) yes, you do count it.
I now use PHPEdit. It shows the line numbers (ok,ok not that special ;) ) , has php-syntaxhighlighting and (best of all) highlights corresponding braces :D