php mailer using standard email form

Issues related to forms.
Post Reply
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

php mailer using standard email form

Post by rogerl »

I have the php mailer form working well but would like to update the functionality a bit so that when I receive the email from my server the "reply to" is defined as the email address input by the user in the edit field named "email"
Currently the system defaults to the "From" address which is my server's default email address so when I click on reply I end up sending an email to myself.
I did some Google research and found a command:
$mail->AddReplyTo = "$email";
This looks what I might be needing but how do I apply this and where is the php mailer script so I can edit it please.
Regards
Roger
User avatar
Pablo
 
Posts: 21574
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: php mailer using standard email form

Post by Pablo »

If you do not specify the 'from' address in the form's properties then the 'email' input field will be used as from address.
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

Yes, I had that setup previously but changed it as part of fix for gmail / yahoo issues where gmail rejected emails where the originator / user was yahoo address but the email was from my server and that broke their rules. I was forced to correctly identify from address and host added some code on their side as well.
User avatar
Pablo
 
Posts: 21574
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: php mailer using standard email form

Post by Pablo »

In that case, you cannot use the input field as the form address. This is a limitation of the web host.
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

After further checking on php mailer queries on Google, this very subject is discussed and the suggestion is to use;
$mail->addReplyTo($_POST['email'], $_POST['name'])
Comments relating to my earlier message with the gmail / yahoo issue.
//**DO NOT** use the submitter's address here as it will be forgery
//and will cause your messages to fail SPF checks
If I amend your php code as displayed in the page header and replace:
$mail->AddReplyTo($mailfrom);
with
$mail->addReplyTo($_POST['email'], $_POST['name']);
The issue appears to be solved and I don't appear to be breaking gmail's rules.
Your thoughts please.
User avatar
Pablo
 
Posts: 21574
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: php mailer using standard email form

Post by Pablo »

I'm sorry, this option is currently not available.
But you can add custom code via the form's HTML properties.
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

Okay, have edited the object html, custom form processing and added the following:
$mail->clearReplyTos();
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->FromName = 'my email name';
This achieves the desired result.
The clear command is necessary otherwise your code generates 2 reply to addresses.
Regards
Roger
Last edited by rogerl on Sun Apr 28, 2019 4:01 am, edited 1 time in total.
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

While sorting this I note the "FromName" field has also been fudged instead of allowing input in the properties of a meaningful name.
User avatar
Pablo
 
Posts: 21574
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: php mailer using standard email form

Post by Pablo »

While sorting this I note the "FromName" field has also been fudged instead of allowing input in the properties of a meaningful name.
What exactly do you mean by this?
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

Instead of a proper value being defined the code just gives it the value of the from email address so you end up with;
from:
johndoe@yahoo.com <johndoe@yahoo.com>
The php code in the header is:
$mail->FromName = $mailfrom;
$mailfrom has already been defined as the "from" email address from the properties input field.
GrahamW
 
 
Posts: 240
Joined: Sat Jul 08, 2017 5:02 am

Re: php mailer using standard email form

Post by GrahamW »

Hi Roger,

this may help you out with your issue. Its a html email extension I wrote and it will do as you require
here is a demo link and purchase link
https://shop.grahamscomputers.net/webshop.html

in the extension when you design your form lets say you call the email field email if you use the same name in repl;y-to of the extension it will reply to that email address that the sender uses

hope this is what your after
Graham
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

Thanks Graham, I'll pass on that for now as my custom code does what I need. I enjoy sorting these little issues it just annoys me when I investigate and find the basics are there already but have been passed over for a less than professional end result. But hey, the fun and learning is in the journey.
Bloodhound2
 
 
Posts: 13
Joined: Thu Aug 25, 2016 6:44 am

Re: php mailer using standard email form

Post by Bloodhound2 »

rogerl wrote: Thu Apr 25, 2019 1:44 pm Okay, have edited the object html, custom form processing and added the following:
$mail->clearReplyTos();
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->FromName = 'my email name';
Roger
Dear Roger,
I have exactly the same problem and requirement so I was delighted to see your solution and immediately placed your three lines in the form's Custom Form Processing box. Unfortunately my result on posting a message through the form is:
"Fatal error: Call to a member function clearReplyTos() on null in /home/yourtota/public_html/Contact/Contact-Us.php on line 103"
Is the "s" after clearReplyTo correct?
I have tried using just your original single line but that was also met with a Fatal error message.

I wonder if you are able to throw any light on what I may have done wrong please?

Regards,
Mike
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

Hi Mike,
Works fine on mine and tested out on all 6 of our Breeders on site.
clearReplyTos() is correct - you can Google the command and see it on php sites.
I'm no php expert just tenacious in hunting down results I'm after.
Can you make your code available for people to look at?
Things that come to mind:
  • If you right click on the actual fields on the form that collect the person's name and email address then select object properties, are those fields actually called name and email. If not then change the code to reflect the actual names in the form.
  • my email name is a meaningful name to identify your "from" email address - in our case 'White Shepherd Web Site'.
  • Have you actually identified your from email address in the form? This is found by right clicking on a blank part of the form and selecting object properties then clicking on the More button. Insert the actual from address that shows up when you receive the email from the site. In our case it was the default address for our site allocated by the server which we had added to our list of valid emails for the site. With the amount of email verification that goes on now it pays to be thorough.
  • Have you selected - Use built-in PHP form processor script.This is found by right clicking on a blank part of the form and selecting object properties .
  • Have you selected - Advanced, Mail engine, Use PHPMailer instead of standard PHP mail, also Mail Engine, mail. This is found by right clicking on a blank part of the form and selecting object properties then clicking on the Advanced button.
Since the error talks about a Null field, one of those named fields probably has no value allocated and now you are telling it to do something with a null value. Well that's my logic. Seeing line 103 might help.
Regards
Roger
Bloodhound2
 
 
Posts: 13
Joined: Thu Aug 25, 2016 6:44 am

Re: php mailer using standard email form

Post by Bloodhound2 »

Hello Roger,
I am very sorry I have not been back earlier; I did not receive an email notification of a reply so assumed not but today decided to have a look. Alas I find I had not ticked the notification button!!

Thank you for all your suggestions of where to check, very thorough but everything looks to me to be in order:

My code is as below with email addresses altered, the error line is where your three lines start:

<?php
if (session_id() == "")
{
session_start();
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['formid'] == 'layoutgrid4')
{
if (isset($_POST['captcha_code'],$_SESSION['random_txt']) && md5($_POST['captcha_code']) == $_SESSION['random_txt'])
{
unset($_POST['captcha_code'],$_SESSION['random_txt']);
}
else
{
$errorcode = file_get_contents('./problem.htm');
$replace = "##error##";
$errorcode = str_replace($replace, 'The entered code was wrong.', $errorcode);
echo $errorcode;
exit;
}
}
?>
<?php
function ValidateEmail($email)
{
$pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
return preg_match($pattern, $email);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['formid']) && $_POST['formid'] == 'layoutgrid4')
{
$mailto = 'contact@ouraddress.com';
$mailfrom = 'mike@ouraddress.com';
$subject = 'Website form';
$message = 'Enquiry submitted from YTE form:';
$success_url = './success.htm';
$error_url = './problem.htm';
$eol = "\n";
$error = '';
$internalfields = array ("submit", "reset", "send", "filesize", "formid", "captcha_code", "recaptcha_challenge_field", "recaptcha_response_field", "g-recaptcha-response");
$boundary = md5(uniqid(time()));
$header = 'From: '.$mailfrom.$eol;
$header .= 'Reply-To: '.$mailfrom.$eol;
$header .= 'MIME-Version: 1.0'.$eol;
$header .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"'.$eol;
$header .= 'X-Mailer: PHP v'.phpversion().$eol;
try
{
if (!ValidateEmail($mailfrom))
{
$error .= "The specified email address is invalid!\n<br>";
throw new Exception($error);
}

if (!empty($_POST['email']) && !preg_match("/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i", $_POST['email']))
{
$error .= "requires a correct email address\n<br>";
}
$message .= $eol;
$message .= "IP Address : ";
$message .= $_SERVER['REMOTE_ADDR'];
$message .= $eol;
foreach ($_POST as $key => $value)
{
if (!in_array(strtolower($key), $internalfields))
{
if (!is_array($value))
{
$message .= ucwords(str_replace("_", " ", $key)) . " : " . $value . $eol;
}
else
{
$message .= ucwords(str_replace("_", " ", $key)) . " : " . implode(",", $value) . $eol;
}
}
}
$body = 'This is a multi-part message in MIME format.'.$eol.$eol;
$body .= '--'.$boundary.$eol;
$body .= 'Content-Type: text/plain; charset=ISO-8859-1'.$eol;
$body .= 'Content-Transfer-Encoding: 8bit'.$eol;
$body .= $eol.stripslashes($message).$eol;
if (!empty($_FILES))
{
foreach ($_FILES as $key => $value)
{
if ($_FILES[$key]['error'] == 0)
{
$body .= '--'.$boundary.$eol;
$body .= 'Content-Type: '.$_FILES[$key]['type'].'; name='.$_FILES[$key]['name'].$eol;
$body .= 'Content-Transfer-Encoding: base64'.$eol;
$body .= 'Content-Disposition: attachment; filename='.$_FILES[$key]['name'].$eol;
$body .= $eol.chunk_split(base64_encode(file_get_contents($_FILES[$key]['tmp_name']))).$eol;
}
}
}
$body .= '--'.$boundary.'--'.$eol;
$mail->clearReplyTos();
$mail->addReplyTo($_POST['email'], $_POST['name']);
$mail->FromName = 'my email name';
if ($mailto != '')
{
mail($mailto, $subject, $body, $header);
}
header('Location: '.$success_url);
}
catch (Exception $e)
{
$errorcode = file_get_contents($error_url);
$replace = "##error##";
$errorcode = str_replace($replace, $e->getMessage(), $errorcode);
echo $errorcode;
}
exit;
}
?>
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

Hi Mike,
Just checking through your code and it would seem you are not using PHPMailer but X-Mailer instead. From my point of view the PHPMailer is a better way to go and my code is based on that. All the differences are based on that selection you have made. Only other difference I found was that your form is in a layout grid, but I can't imagine that makes a difference.
Have you selected - Advanced, Mail engine, Use PHPMailer instead of standard PHP mail, also Mail Engine, mail. This is found by right clicking on a blank part of the form and selecting object properties then clicking on the Advanced button
Also
$mail->FromName = 'my email name';
Don't forget to change 'my email name' in the custom code to something more meaningful to you i.e. 'Mike's Site"
Other than that, have nothing further to offer.
Regards
Roger
Bloodhound2
 
 
Posts: 13
Joined: Thu Aug 25, 2016 6:44 am

Re: php mailer using standard email form

Post by Bloodhound2 »

Thank you Roger,
I have corrected the omission I had made and now have it working perfectly but this was only after I had uploaded the entire web site again.

It would be good if Pablo was to include this in a future update as it greatly improves compatibility with Gmail and others whilst retaining the assistance for users so they do not have to remember to copy the enquirer's email address across.

Best regards
Mike
User avatar
rogerl
 
 
Posts: 196
Joined: Tue May 03, 2016 8:24 am

Re: php mailer using standard email form

Post by rogerl »

See the info on the next upgrade to V15 - Pablo has added ability to add FromName as part of the form where you add the From address. Nothing about fixing the replyTo issue though.??

Update!!
Check out Pablo's reply below. New form format in V15. Replace the contents of the replyTo field with:

Code: Select all

$_POST['email'], $_POST['name']
and put something meaningful in the FromName field.

Note !!
For those using Thunderbird as your email client, there are a couple of issues around using replyTo which can be overcome but also do not test using your own name and email as it realises this relates to your profile and won't use as a replyTo address.
Last edited by rogerl on Sat Sep 14, 2019 4:59 am, edited 3 times in total.
harville
 
 
Posts: 33
Joined: Fri Mar 23, 2007 3:07 am

Re: php mailer using standard email form

Post by harville »

Thank you Roger!
I too was having the same issue you have and was able to resolve it by adding the 3 lines of code you gave. Thank you for making it easy! I'm not a php guru either, but like you, I'm pretty good at solving problems.

Pablo, is there any way to work this into your software as an option?
User avatar
Pablo
 
Posts: 21574
Joined: Sun Mar 28, 2004 12:00 pm
Location: Europe
Contact:

Re: php mailer using standard email form

Post by Pablo »

This functionality is available in WB15.

Image

See also:
https://www.wysiwygwebbuilder.com/support/wb15.html
harville
 
 
Posts: 33
Joined: Fri Mar 23, 2007 3:07 am

Re: php mailer using standard email form

Post by harville »

Thanks! I'm currently using WB14, may consider upgrading before too long.
qutuba
 
 
Posts: 2
Joined: Wed Sep 25, 2019 8:12 am

Re: php mailer using standard email form

Post by qutuba »

Hi

any one have script of contact form used by PHP mailer through web builder 15 or can write in detail pls.
User avatar
BaconFries
 
 
Posts: 5325
Joined: Thu Aug 16, 2007 7:32 pm

Re: php mailer using standard email form

Post by BaconFries »

Post Reply