Copy link to clipboard
Copied
I have routines to register a new user who can update data files displayed on the website and to long them in to access update routines. I now need to write a routine which allows a user to change his password. I've asked for the email account and then sent a message to the account containing a link to an update routine, but the email is unauthenticated and contains a "noname" document containing the message, which is
Click on the link to reset your password.
https://www.domain.com/reset.php
This is useless. When I follow routines to reset a password, the link is never this legible - it's always long in indeciperable. How do you do that? And what am I doing wrong?
My code:
if (!$suspect && !$errors) {
// send email with link to reset routine
$to = $email;
$subject = 'Change password';
$headers = "Content-Type: text/plain, charset=utf-8\r\n";
$headers .= "From: webmaster@domain.com";
$param = '-fwebmaster@domain.com';
$message = "Click on link to change your password.\r\n\r\n".'https://www.support.domain.com/reset.php';
$mailSent = mail($to, $subject, $message, $headers,$param);
if (!$mailSent) {
$errors[] = "Your mail could not be sent.<br>";
} else {
echo 'Mail sent OK<br>';
}
It also might be because you are sending the email as plain text rather than html, which the link is, try the below:
if(!$suspect && !$errors) {
$to = $email;
$subject = "Change Password";
$from = "-fwebmaster@domain.com";
// build message
$message = "<h2>Click on link to change your password</h2>
<a href='https://www.support.domain.com/reset.php'>Reset</a>";
// set mime version for html headers
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-Type:text/html;charset=UTF-8"."\r\n";
$
...
Copy link to clipboard
Copied
When I send the email, the email is going as a document containing the message and link. How do I get it to send the content AS the email? There's something I'm not seeing...
Copy link to clipboard
Copied
Must be something wrong with your server - the message comes through as an email if I test your code NOT as a document. Do you have another server to test on?
Its probably how you are passing the $email to the mail script? Test by hard-coding an email and see what happens:
$to = "blah@blah.com;
rather than:
$to = $email;
Copy link to clipboard
Copied
It also might be because you are sending the email as plain text rather than html, which the link is, try the below:
if(!$suspect && !$errors) {
$to = $email;
$subject = "Change Password";
$from = "-fwebmaster@domain.com";
// build message
$message = "<h2>Click on link to change your password</h2>
<a href='https://www.support.domain.com/reset.php'>Reset</a>";
// set mime version for html headers
$headers = "MIME-Version: 1.0"."\r\n";
$headers .= "Content-Type:text/html;charset=UTF-8"."\r\n";
$headers .= "From: $from\r\nReply-to: $email";
// send data
mail($to, $subject, $message, $headers, $from);
echo 'Mail sent OK';
}
else {
echo 'Your mail could not be sent.';
}
Copy link to clipboard
Copied
Thank you so much! One thing I had going wrong was that I was using Webmaster as the FROM when the default was sainttim. I changed that.
Then I replaced my headers code with the code you sent, and BINGO! It worked! Now - can/will you tell me if it was the MIME addition, replacing type text/plain with text/html, adding from & Reply-to, or ALL of the above that made it work?
Again - thank you for the help. I've been battling this for a couple of days.
Copy link to clipboard
Copied
Now - can/will you tell me if it was the MIME addition, replacing type text/plain with text/html, adding from & Reply-to, or ALL of the above that made it work?
I think the problem was probably resolved by the addition of the correct MIME-version and Content-type
Copy link to clipboard
Copied