PHP form, not receiving email, SMTP authentication issue ??

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

rich1972

Thread Starter
Joined
Oct 21, 2016
Messages
11
Dear all,

I am trying to create a form which sends the info to my email. Before I begin to create the form, I have tried to practice on a simple feedback form first.

I have created the HTML page with the simple feedback form (using dreamweaver CS6), and have created a php page, and entered the code (from an adobe textbook – as I am quite new to all this).

Once ‘Submit’ is selected on the html page, the php page appears with the message (from the php page) about ‘thank you for your feedback etc’ appears, so I know the html is linking to the php page.

However, I never get the email.

I have tried so many times to modify the code but it never works.

I am pretty sure I am missing something quite simple (like i said, I am quite new to website design).

I already have a web host company and a domain name, so I am effectively testing this in a live environment (as couldn’t figure out how to set up a test server).

I have asked my web hosting company for advice on whether they support PHP, and they have stated:

“We do support this, but you would need to configure your script to use SMTP authentication to connect to your mailbox, and send the email rather than sending it directly from the web server.
We are a hosting company, not a web development company. So I am afraid all coding and scripting is fully out of remit of support. I do know that you would need to script a form so that it sends through the mail server using SMTP”



I don’t really understand what this means ? Configuring script to use smtp authentication ? Any ideas ?

I have also read that I need to use WAMP (or something similar) so I have downloaded that but have not real clue how to use it (or even if I need to).

I have enclosed the code here :


HTML page (with form):

<table width="100%" cellspacing="30">
<tr>
<td width="50%" valign="top">
<form id="feedbackform" method="post" action="email_feedback.php">
<fieldset>
<legend>Feedback Form</legend>
<table width="100%" border="1" cellpadding="6">
<tr>
<td width="26%">Name</td><td width="74%"><input name="Name" type="text" id="Name" size="30" /></td>
</tr>
<tr>
<td>Email</td><td><input name="Email" type="text" id="Email" size="30" /></td>
</tr>
<td>Comments</td>
<td><textarea name="Comments" id="Comments" cols="32" rows="4"></textarea></td>
</tr>
<tr>
<td width="26%"> <input type="submit" name="Submit Order" id="Submit" value="Submit" /></td>
<td width="74%"> </td>
</tr>
</table>
</fieldset>
</form>



PHP page (titled ‘email_feedback.php’

<?php
$to = "[email protected]";
$subject = "Feedback";
$message =
"Customer Name: " . $_POST['Name']. "\r\n" .
"Customer Email: " . $_POST['Email']. "\r\n" .
"Comments: " . $_POST['Comments'];
$from = $_POST['email'];
$headers = "From: $from" . "\r\n";
mail($to,$subject,$message,$headers);
?>

I have currently got it exactly as it looks above (haven’t edited for here).

I have tried everything on the above, from closing up the spacing, removing full stops, to entering my email into the from part and the headers part, but just cant get it to work.

Any advice would be massively welcome. As I said, I can create the site ok, but it’s these more advanced parts (well, to me anyway) that has me stumped.

Not sure what other info you would need, but I am running windows 10 and have Microsoft outlook as my email program, and my email account is set up as POP/SMTP. But please let me know if I can provide any more useful info.

Thanks,

Rich
 

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,398
Hi Rich;)
You're almost there(y) You just need to add these 2 lines to beef-up the headers ...
PHP:
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
... after your initial "$headers =" line.
This will enable the sending of the email from your web server - a perfectly normal approach (I don't know why your hosting service wants you to go the 'smtp - local email handler' route unless they want to cut down on their server work).
As regards your downloaded WAMP: look for a README file, which should give you installation instructions to follow.
It won't be of much use to you at present (It does not handle email sends). But as you start to use php more and more you'll find it a great local testing platform. (saves you constantly doing FTP uploads, to test your php).
 

rich1972

Thread Starter
Joined
Oct 21, 2016
Messages
11
Hi Jim,
Thanks so much for your reply, very much appreciated :)

I have added the two lines, so now my code looks like this (exactly like this):

<?php
$to = "[email protected]";
$subject = "Feedback";
$message =
"Customer Name: " . $_POST['Name']. "\r\n" .
"Customer Email: " . $_POST['Email']. "\r\n" .
"Comments: " . $_POST['Comments'];
$from = $_POST['email'];
$headers = "From: $from" . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);
?>

Unfortunately, I still don't get any emails :-( Have I put the two lines in the correct place? I noticed that you had a full stop in your headers line (before the = sign) that I didnt - dont know if this made a difference, so tried both (ie - from line without full stop, and with) but sadly didnt make any difference.

I am sure it is something simple I am just missing somewhere. Any other ideas ?

From reading online guides etc, I have also checked my spam/junk folder (nothing there).

I haven't touched WAMP, as by the sounds of it, for what I'm doing at the moment, doesnt sound as if I need it ? Which sounds good, one less thing :whistle:
 

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,398
“We do support this, but you would need to configure your script to use SMTP authentication to connect to your mailbox, and send the email rather than sending it directly from the web server.
We are a hosting company, not a web development company. So I am afraid all coding and scripting is fully out of remit of support. I do know that you would need to script a form so that it sends through the mail server using SMTP”
From this we should perhaps surmise that your host doesn't just advise against using php's send, but actually have some sort of method whereby they can block it - I'd query them about this and see what turns up, because I can see no problems with your code.
 

rich1972

Thread Starter
Joined
Oct 21, 2016
Messages
11
Many thanks - I have done exactly that, and send them a reply to their ticket, with the above comments. Lets see what they come back with :)
Thank you for the clarification on the code though - at least I know that part is ok.
Cheers,
Rich
 

rich1972

Thread Starter
Joined
Oct 21, 2016
Messages
11
Hi Jim,
I have an update from Heart (my web hosting company), who are now advising (or passing the buck depending on your view), to do this:

Thanks for your reply.
Here's what we recommend doing for sending emails from our web servers:
https://www.lifewire.com/send-email-from-php-script-using-smtp-authentication-and-ssl-1171197
However, please don't use the SMTP and SSL method. Also, please make sure that you create the mailbox in the package as it would need to be checked by our servers and verified that it exists.

A seeminlgy simple task is now getting more complicated by the day it seems. What do you think, ever heard of pear ?
 

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,398
I noticed that you had a full stop in your headers line (before the = sign)
The . before =, is simply PHP shorthand for $headers = $headers."MIME-Version: 1.0\r\n"; - I forgot to explain that:(
Regarding Heart's reply:
I think they want you to use the first option on that page-link:
Sending Mail from PHP Using SMTP Authentication - Example
... definitely NOT the second (SSL) option.

So give it a try:
1. set up an email address via your Control Panel on your Heart login and make a note of the hostname, username and password you use.
2. get reassurance from them that PEAR is installed.
3. ask them where you can find Mail.php (require once at beginning of script) and what it's function is (so that you can point to it from or insert it into your website's root folder).
4. use the example code, edited to include your specifics.
... and give it a whirl;)
 

rich1972

Thread Starter
Joined
Oct 21, 2016
Messages
11
Hey Jim,

Once again, thank you so much for your advice. I have contacted them following this, and they have replied with :

Hi Richard,
1. set up the mailbox by logging into extend here:
Manage Web Hosting (including email and FTP)
and clicking on "mail boxes" follow the simple instructions to set up the mailbox

2. It certianly is, yes.

3. We run a multiple p[hp installs side by side so dependent on what php version you're using you'll need one of the following if you're referencing it directly for some reason:

/usr/share/pear/Mail/mail.php (php4)
/usr/share/pear5/Mail/mail.php
/usr/share/pear53/Mail/mail.php
/usr/share/pear54/Mail/mail.php
/usr/share/pear55/Mail/mail.php
/usr/share/pear56/Mail/mail.php
/usr/share/pear7/Mail/mail.php

However you won't need to normally because the each version of php is aware of the correct directory and you'll simply need to:
require_once "Mail.php";


So......I have set up a couple of mailboxes on heart - the enquiries one (which i think is already set up) and another one, just to check in case I have overlapped with the enquiries one somehow.

So, to the next point - "make a note of the hostname, username and password you use":
A password was provided with my email box whn i set it up so thats good. As for hostname and username, can you please clarify what that is ? - is the username just the email address, and the hostname the website name (for example, my website name is www.digimems.co.uk and the email address is [email protected], so are these the hostname and username ?)
 

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,398
A password was provided with my email box whn i set it up so thats good. As for hostname and username, can you please clarify what that is ? - is the username just the email address, and the hostname the website name (for example, my website name is www.digimems.co.uk and the email address is [email protected], so are these the hostname and username ?)
Hostname is normally the website name (without the www.) preceded by either 'smtp.' or 'mail.' and yes the username will be the email address.
Check where you set up or edit your emails in your hosting control panel and you should be able to spot it in 'settings' or something similar.
However you won't need to normally because the each version of php is aware of the correct directory and you'll simply need to:
require_once "Mail.php";
I wouldn't use a capital 'M' in your require_once statement either, as it is in the example code - the file has a small 'm' according to Heart ...
It would also appear that you don't have to give it an absolute pathname either - as Michael Caine might say "I didn't know that!";)
 

rich1972

Thread Starter
Joined
Oct 21, 2016
Messages
11
UPDATE - JIM, PLEASE READ THE POST BELOW BEFORE READING THE BELOW (IF THAT MAKES SENSE) AS I THINK IT NOW WORKS


Thanks Jim,

'I Didn't know that' :) - I'm feeling that on a daily basis :)

ok, so I have your info in above post - I also got another reply from heart also ref username and hostname, stating :

The hostname is actually the name of the mailserver. to avoid complication use: mail99.extendcp.co.uk
Username is full email address, password is associated password.


So, I now have:

Username - [email protected]
Password for this email box - (best not print it here i suppose but I do have it - for the purpose of displaying it here though, lets say the password is - MichaelCaine :)
Hostname - mail99.extendcp.co.uk

I have the feeling that I am almost there........almost.

I have put this in to the code, but still no luck - i have tried a few variants but so far no luck, but at least now im getting new errors, which means something is connecting - got a couple of smpt authentication failures messages.

I hate to ask, but wondered if you could have a look at the code and let me know where exactly I have to put the above hostname etc, as there appears to be various mentions of it in the code etc and i think im probably putting it in the wrong place. I have entered where I think things should go - can you please let me know if this is right.

here is the original code as provided on that website which Heart recommends etc (without any changes - ie - it is the example code provided)

<?php
require_once "Mail.php";

$from = "Sandra Sender <[email protected]>";
$to = "Ramona Recipient <[email protected]>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "mail.example.com";
$username = "smtp_username";
$password = "smtp_password";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>



From my web page, just to recap this is my HTML page:


HTML page (with form):

<table width="100%" cellspacing="30">
<tr>
<td width="50%" valign="top">
<form id="feedbackform" method="post" action="email_feedback.php">
<fieldset>
<legend>Feedback Form</legend>
<table width="100%" border="1" cellpadding="6">
<tr>
<td width="26%">Name</td><td width="74%"><input name="Name" type="text" id="Name" size="30" /></td>
</tr>
<tr>
<td>Email</td><td><input name="Email" type="text" id="Email" size="30" /></td>
</tr>
<td>Comments</td>
<td><textarea name="Comments" id="Comments" cols="32" rows="4"></textarea></td>
</tr>
<tr>
<td width="26%"> <input type="submit" name="Submit Order" id="Submit" value="Submit" /></td>
<td width="74%"> </td>
</tr>
</table>
</fieldset>
</form>


And this is the PHP page to go with it. - this is the example code above but with my added/edited changes, to reflect the fields in the feedback form (ie - the body of the email), and the hostname/password etc info I have now been given. To make it easier to look at, I have bolded all instances where I have edited with my info. I have also lower-cased the M :)


PHP Page


<?php
require_once "mail.php";

$from = "Your Web Site Email <[email protected]>";
$to = "Rich Hardiman <[email protected]>";
$subject = "Feedback Form";
$body =
"Customer Name: " . $_POST['Name']. "\r\n" .
"Customer Email: " . $_POST['Email']. "\r\n" .
"Comments: " . $_POST['Comments'];


$host = "mail.mail99.extendcp.co.uk";
$username = "smtp_[email protected]";
$password = "smtp_MichaelCaine";

$headers = array ('[email protected]' => $from,
'[email protected]' => $to,
'Feedback Form' => $subject);
$smtp = Mail::factory('smtp',
array ('mail99.extendcp.co.uk' => $host,
'auth' => true,
'[email protected]' => $username,
'MichaelCaine' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>


Hows it looking - i do wonder whether they should be an entry in this line?
$smtp = Mail::factory('smtp',

Also wondering whether :
$host = "mail.mail99.extendcp.co.uk";
Should just be
$host = "mail99.extendcp.co.uk";

But, have tried both variations with no luck

But, again, any advice massively appreciated. i know i've already taken up a lot of your time, and you may well be inclined to just yell "Give it up man - you're never gonna do it" !! and look at a far more interesting forum :)

Thank you,

Rich
 
Last edited:

rich1972

Thread Starter
Joined
Oct 21, 2016
Messages
11
OK - HANG FIRE !!!!! :)

I think.......I think, it may have worked. I combined the original code that I sent you, and the one form the pear site etc, so had this -

<?php
$to = "[email protected]";
$subject = "Digital Memories Feedback";
$message =
"Customer Name: " . $_POST['Name']. "\r\n" .
"Customer Email: " . $_POST['Email']. "\r\n" .
"Service: " . $_POST['Service']. "\r\n" .
"Comments: " . $_POST['Comments'];

$host = "mail.mail99.extendcp.co.uk";
$username = "[email protected]";
$password = "smtp_MichaelCaine";

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);
?>

And it sort of worked. Wierdly, i forgot to remove the 'Michael Caine' part (ie - didnt put in my correct password for [email protected], and it still worked. - Go figure :)

So, please ignore above for now, and i will experiment on this.


UPDATE - IT NOW WORKS WITHOUT PUTTING IN ANY HOSTNAME/PASSWORD (ie - the very first code i tried now works) - as per below.
I am guessing that somewhere along the line, the smtp etc got authenticated, and this now works going forward without having to put in all the info.

<?php
$to = "[email protected]";
$subject = "Digital Memories Feedback";
$message =
"Customer Name: " . $_POST['Name']. "\r\n" .
"Customer Email: " . $_POST['Email']. "\r\n" .
"Service: " . $_POST['Service']. "\r\n" .
"Comments: " . $_POST['Comments'];

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to,$subject,$message,$headers);
?>
 
Last edited:

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,398
I am guessing that somewhere along the line, the smtp etc got authenticated, and this now works going forward without having to put in all the info.
This whole smtp thingy is foreign to me. I just use the additional header information and it has always worked for me - just as you have it now. But I'm glad it's sorted:D
Also wondering whether :
$host = "mail.mail99.extendcp.co.uk";
Should just be
$host = "mail99.extendcp.co.uk";
Yup - the 2nd one ...
 

rich1972

Thread Starter
Joined
Oct 21, 2016
Messages
11
Thanks Jim - yep, all foreign to me too. It is strange that after a couple of weeks, the original small code that I first tried now works. I will never make sense of it. But anyway, it works. I cant thank you enough for all your help, I really appreciate it :)

Now.....onto the next part - creating a php page for a proper form, with radio buttons and drop down lists. Or dare I venture into the database route (me thinks probably not - know your limits as my missus keeps telling me :) - Already realising that this is not quite as easy as a simple feedback form. But......i will persevere :)

(expect another thread soon of me whining cause I cant get the form to work)

Rich
 

JiminSA

Jim
Joined
Dec 15, 2011
Messages
3,398
It's cool that you are venturing forward with designing your own form(y)
May I recommend that you go the html5 route - this doctype enables much more powerful form management and is particularly mobile friendly as described here ... just remember to make your html's first line ...
HTML:
<!DOCTYPE html>
... and away you go;)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top