Solved

Email not receiving after submitting contact form using HTML and PHP mail() method.

Posted on 2016-08-04
10
56 Views
Last Modified: 2016-08-08
I have created a form with input fields in html. After submitting form, it takes to thankyou.php where the mail() method sends the email to the recipient.
After that it navigates to email_success.php where a  confirmation message is displayed – “Email Sent. Thank you.”

Here is the code for HTML form:

<form action="thankyou.php" name="form1"  id="form1" method="post">	
<div class="row">		
<div class="col-md-6 form-group">
 <label class="sr-only" for="contact-email">Name</label>
   <input type="text" name="name" placeholder="Name" class="contact-email form-control" id="txtBoxName">
   </div>
    <div class="col-md-6 form-group">
    <label class="sr-only" for="contact-email">Email</label>
  <input type="text" name="email" placeholder="Email" class="contact-email form-control" id="txtBoxEmail">
  </div>
  </div>
  <div class="form-group">
 <label class="sr-only" for="contact-message">Message</label>
 <textarea name="message" placeholder="Message" class="contact-message form-control" id="txtBoxComment"></textarea>
 </div>
   <input name="buttonSend" type="submit" class="btn" id="buttonSend" value="Submit" />                                  
                                </form>

Open in new window




Here is the thankyou.php

<?
$name = $_POST['name'];
$email_from = $_POST['email'];
$email_to = 'you@yourdomain.com’;
$email_subject = "Message from Contact form";
$message = $_POST['message'];
$headers = "From: $email_from \r\n";
$email_message = "
Name: ".$name."
Email: ".$email_from."
Message: ".$message."
";
mail($email_to,$email_subject,$email_message,$headers);
header ("Location: email_success.php");
?>

Open in new window


Here are the scenarios where it works:

1.      If $email_to is gmail account and $email_from is gmail    - It works and sends email.
2.      If $email_to is gmail account and $email_from is other domain account – It works and sends email.

Here are the scenarios where it does not work:

1.     If $email_to is outlook/other domain account and $email_from is gmail  - It is not working (not receiving email to outlook/ other domain account).
2.    If $email_to is outlook/other domain account and $email_from is also same outlook/other domain account – Then also it is not working.

How to make it work to receive an email to outlook/ other domain emails?

Thank you
0
Comment
Question by:Angel02
10 Comments
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 500 total points
ID: 41743216
email_from should be your domains mail address. You're using your own domains mail server to send the mail to yourself, even though the user submitting your form enters his own mail address, the mail doesn't start from his computer, it starts from your PHP codes mail() function, and that sends from your own domain.

So what's wrong is:
$email_from = $_POST['email'];
...
$headers = "From: $email_from \r\n";

Open in new window



The from address should a) rather be configured in php.ini, or you explicitly set this to a mail address of your domain, that means you literally send a mail from yourself to yourself.

If you want the entered mail address to appear when you click on reply, add it as a Reply-To header:
<?
$name = $_POST['name'];
$email_from = 'you@yourdomain.com';
$email_to = 'you@yourdomain.com';
$reply_to = $_POST['email'];
$email_subject = "Message from Contact form";
$message = $_POST['message'];
$headers = "From: $email_from \r\nReply-To: $reply_to\r\n";
$email_message = "
Name: ".$name."
Email: ".$email_from."
Message: ".$message."
";
mail($email_to,$email_subject,$email_message,$headers);
header ("Location: email_success.php");
?>

Open in new window


In short:
mail() uses your domain's mail server and that only accepts your domain mail addresses as sender, you can't act on your domain mail server as if the mail was coming from elsewhere. You rather configure php.ini to set a general From address, also to specify the reply recipient you add the Reply-To header.

Bye, Olaf.
1
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41743230
Email is one of those things that sounds simple, until you actually try to write a computer program to use it.  Unfortunately, email is so full of inconsistencies and trip-wires that I cannot in good conscience counsel anyone to try to use a PHP script to send email messages.  There are just too many things that can go wrong, as you are beginning to see.  I have seen all of these things go wrong, in my own work, and in dozens, if not hundreds of E-E questions over the years.  So just don't do that!  

Instead of trying to send your own email from a PHP script, put all of that aside and use ConstantContact.com.  They are embarrassingly inexpensive and very dependable.  They will save you countless thankless work hours.  They will get your messages to go through.  They will give you statistics about the messages.  They will handle legal compliance and keep you from being charged as a criminal spammer.  In short, they are the "right answer" when a PHP script is not the right answer.  They have competitors, to be sure, but I know that Constant Contact can be relied on, so I am glad to recommend them.

From the look of the PHP scripts posted here, you might be new to PHP.  In case you want to learn PHP programming, this article can help you find the good learning resources and more importantly, can help keep you away from the many terrible PHP examples that litter the internet.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

Best of luck with your project, and don't write any more PHP email scripts until you've tried the free trial from Constant Contact.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41743261
Ray,

you seem to interpret this as sending a marketing mail, a newsletter or other mailing list, while it's the opposite. The user enters his mail address and a comment to contact Angel02.

You're right in the aspect that still many things can go wrong, eg when specifying more or other things than a mail address this simple PHP could transfer something malicious, as that code doesn't check and sanitize the entered data, Angel02. Also many samples I just browsed through make the same mistake with the from address. The code also lacks spam protection by asking for a captcha, for example.

Bye, Olaf.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41743276
Ahh, I see.  In that case, I would not send any email at all - just add the client information to the database and be done with it.  I don't need to receive an email to know that someone registered on my web site.  I can just check the database with a small PHP script.  Or I can have a CRON job check the database and send a Tweet.  Email was a great idea in 1976.  Almost anything is preferable to email these days!
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41743282
That's indeed possible. Still, it's quite convenient to have a mail you can respond to. Another solution would be making use of CRM systems like insightly. Their Web to Contact feature creates an HTML form for you, that submits to your insightly account and adds a contact there.

Bye, Olaf.
1
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
ID: 41743770
Another thought, that comes to mind: To make this less weird, you could let the PHP send from a system mail account you set up if it doesn't already exist, eg noreply@yourdomain.com, info@yourdomain.com, php@yourdomain.com, or yourdomain@yourdomain.com or even contactform@yourdomain.com.

That as from address and sending to Angel02@yourdomain.com isn't so weird anymore and makes clearer what really happens. The text does come from the customer, but not via mail. The mail is initialized by your php code on your mail server. Ideally the php script would rather just store the mail as it would have come from the customer into your inbox, which perhaps could even be done somehow, but the easiest way to do so with PHP mail() is to send the mail to yourself. The mail server does not really send out the mail, as it recognizes it is the recipient itself, so the mail is put into your inbox.

You could also create a mailing list like helpdesk@yourdomain.com, which is configured to forward to current helpdesk employees, to several employee@yourdomain.com addresses. Again this wouldn't go out and back in, as the destination mail server is the origin mail server, so it's your simplest way to sort in contact form submit data as mails into the different mail slots.

Besides that you could also send a mail from noreply@yourdomain.com to the entered mail address to inform the customer like "Thank you for contacting us. We will get back to you soon." Aside of that confirmation of the contact message submit this is a first test of the mail address existing.

Bye, Olaf.
1
 
LVL 7

Expert Comment

by:Swatantra Bhargava
ID: 41747470
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41747483
It would be nice, if you came back and took your time to close this question, Angel02.

Just one last thought: This contact form might have come from being an HTML form with action="mailto:somebody@example.com", which would work this way, it would really make the customer start his mail client and prepare a mail he could send or save as draft or even extend with an attachment, but for that and other reasons would mean less control of what is sent and be like going back to the stone age of the internet.

So finally the suggestion of Ray to store this contact comment into a database also is no bad idea, the comment already arrived at your site with the form submit, there is no need to send it again, it already arrived. If stored as database entry with a status or flag of being new and to be read and reacted to, like an unread mail, it would also enable any employee committed to customer service with access to this contact data to handle this, better than even a mail to a list of recipients could do.

A CRM is for that, for the tracking of communication with a customer, mail does not do that, unless you're on your own and there is no need to track which employee reacted to a customer's message and with what answer, but it can of course help better organise communications over longer time spans than mail clients even when they also handle and associate emails with "conversations". A CRM does not only convert a comment to a contact, it can track all communication and give an overview, so you  can avoid giving same info multiple times and embitter customers, especially when they reacted to an info with a further question as I'm sure everybody has had a story with a ticket system of any larger corporation or at least knows of some case.

Bye, Olaf.
0
 

Author Comment

by:Angel02
ID: 41747836
Thank you
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 41747844
Thanks.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now