Jump to content

Basic Advice On How To Send Email Via Php


Xyth

Recommended Posts

Hello,

 

I posted a topic herebut is not getting much response so am posting a similar question here instead...

 

Basically, I have essentially no experience with or knowledge about PHP - that said I am trying to create a simple form which, when uploaded to a website I am designing, will send an email to me with the contents of the form.

 

Shouldn't be too difficult right? I am currently working the best I can from various pieces of example code I've found online.

 

I have come up with 2 files - form.php and senddetails.php, the code of which I have below:

 

 

<html></html><form enctype="multipart/form-data" method="post" action="senddetails.php">
<div style="text-align: center">
<input type="hidden" name="required_vars" id="required_vars" value="name,email">
</div>

<table cellspacing="5" cellpadding="5" border="0" align="center">
<tr>
<td valign="top" style="text-align: center">
<strong>Your Name:</strong>
</td>
<td valign="top">
<input type="text" name="name" id="name" size="40" value="">

</td>
</tr>

<tr>
<td valign="top" style="text-align: center">
<strong>Address line 1:</strong>
</td>
<td valign="top">
<input type="text" name="address1" id="address1" size="40" value="">

</td>
</tr>

<tr>
<td valign="top" style="text-align: center">
<strong>Address line 2:</strong>
</td>
<td valign="top">
<input type="text" name="address2" id="address2" size="40" value="">
</td>
</tr>

<tr>
<td valign="top" style="text-align: center">
<strong>Town:</strong>
</td>
<td valign="top">
<input type="text" name="town" id="town" size="40" value="">

</td>
</tr>

<tr>
<td valign="top" style="text-align: center">
<strong>County or State:</strong>
</td>
<td valign="top">
<input type="text" name="countystate" id="countystate" size="40" value="">
</td>
</tr>

<tr>
<td valign="top" style="text-align: center">
<strong>Postcode:</strong>
</td>
<td valign="top">
<input type="text" name="postcode" id="postcode" size="40" value="">
</td>
</tr>

<tr>
<td valign="top" style="text-align: center">
<strong>Message:</strong>
</td>
<td valign="top">
<textarea name="persmes" id="persmes" rows="6" cols="40"></textarea>
</td>
</tr>

<tr>
<td valign="top" style="text-align: center">
<strong>Your email address:</strong>
</td>
<td valign="top">
<input type="text" name="email" id="email" size="40" value="">
</td>
</tr>

<tr>
<td colspan="2" style="text-align: center">
<input type="submit" value=" Submit Form ">
</td>
</tr>
</table>
</form></HTML>

 

 

<?php
$ToEmail = 'postmaster@localhost';
$EmailSubject = 'Details';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY = "Address Line 1: ".$_POST["address1"]."";
$MESSAGE_BODY = "Address Line 2: ".$_POST["address2"]."";
$MESSAGE_BODY = "Town name: ".$_POST["town"]."";
$MESSAGE_BODY = "County/state: ".$_POST["countystate"]."";
$MESSAGE_BODY = "Postcode: ".$_POST["postcode"]."";
$MESSAGE_BODY = "Message: ".$_POST["persmes"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>

 

 

OK, so the code above should work right?? Is there anything obvious wrong with it?

 

I have been trying to test it out using XAMPP and Thunderbird to send an email to myself locally but am starting to realise this may not be possible. Would it be a simpler test just to upload it a suitable domain? Or, can I just test it out with a gmail account, for example?

 

If anyone can point me in the direction of any help with setting up Mercury, or perhaps just explain it to me in simple terms, that would be very much appreciated.

 

I realise this question extends a little outside the scope of PHP coding alone but nonetheless I hope some of you will have enough experience doing what I'm trying to do to help me out.

 

 

 

One final question also - is there a simple way to redirect the user to another page confirming that their details have been sent? I'm guessing this is probably much simpler to answer than the above questions.

Edited by Xyth
Link to comment
Share on other sites

Testing locally requires a mailserver setting up which is out of the scope of this post, though there are examples online.

 

I find it a pain to setup correctly so instead I test emails on my live production server.

 

I can't see any problems with the above code so it should work ok.

Link to comment
Share on other sites

One final question also - is there a simple way to redirect the user to another page confirming that their details have been sent? I'm guessing this is probably much simpler to answer than the above questions

 

Sure you can do a redirect by

 

$ToEmail = 'postmaster@localhost';
$EmailSubject = 'Details';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."";
$MESSAGE_BODY = "Address Line 1: ".$_POST["address1"]."";
$MESSAGE_BODY = "Address Line 2: ".$_POST["address2"]."";
$MESSAGE_BODY = "Town name: ".$_POST["town"]."";
$MESSAGE_BODY = "County/state: ".$_POST["countystate"]."";
$MESSAGE_BODY = "Postcode: ".$_POST["postcode"]."";
$MESSAGE_BODY = "Message: ".$_POST["persmes"]."";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."";
if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
{
header("Location: replaceWithFilename.php");
}

 

Alternatively you could just keep the success / fail msg in the same page.

 

if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
{
  echo "Success";
}
else
{
  echo "Failure";
}

Edited by berridgeab
Link to comment
Share on other sites

Thanks for the response! Actually I seem to have managed to set up a local mail server after much headache - I am using XAMPP and ArGosoft Mail Server, and having configured Mozilla Thunderbird with my postmaster@localhost account, I seem to be able to send an email to myself, which I wasn't able to before, so I assume this means it is working...

 

However, my form still doesn't seem to do anything. I click on Submit Form and just get a blank page thrown up at me, with no email message in my inbox... is there anything else I could be missing here?

Link to comment
Share on other sites

FWIW I think I have configured both php.ini and sendmail.ini properly...

 

In php.ini, I have set:

 

 

SMTP = localhost

smtp_port = 25

 

...and in sendmail.ini:

 

smtp_server=localhost

smtp_port=25

 

 

auth_username=postmaster

auth_password=mypassword

 

hostname=localhost

 

 

I don't think I've made any other changes.

 

Also, I must confess that since, as I said, I've basically been working from example code I've found online, I'm not entirely sure what this

mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>

or this

if(mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader))
{
echo "Success";
}
else
{
echo "Failure";
}

...is supposed to do. Am I supposed to be getting some sort of confirmation message on screen when I press "Submit"? Because as I said, I'm not getting anything, although senddetails.php seems to be loading, all I see is a blank page.

Edited by Xyth
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.