I have a contact form that I'm writing using jQuery and PHP. Below is the PHP bit, which works perfectly fine when I change the $youremail variable to my gmail account, but the emails are never received in my office Outlook account.
Is there something I'm missing?
<?php
// Email Vars
$youremail = "my_email#on_outlook2007.com";
$headers = "From: $name <$email>\n";
$subject = "Subject Line Here";
$ip = $_SERVER['REMOTE_ADDR'];
$message_clean = html_entity_decode(stripslashes($message));
// Format Email
$email_format_cc = $message_clean . "\n\n" . "___________________________________________________________________________" . "\n\n" .
"Name: " . $name . "\n" .
"Company: " . $company . "\n" .
"IP Address: " . $ip . "\n" .
"Sent: " . $timestamp;
$email_format = $email_format_cc . "\n" .
"Page: " . $from_url;
mail($youremail, $subject, $email_format, $headers);
?>
Check your server's mail log to see if/why gmail is bouncing the mail. PHP's mail() function is extraordinarily stupid and claims success even as the universe explodes around it.
Related
So I managed to use "reply-to" and set an email to be replied to, however I'm wondering if its possible to change the subject of the email after its replied? instead of Re: 'Original Subject'?
$header = "From: " . $from ."\r\nReply-to: " . $email . "\r\n";
maybe something like this?
P.S. this 2nd code doesn't work, its just to help me get my point across
$header = "From: " . $from ."\r\nReply-to: " . $email . "\r\nReply-subject: Company Support Message\r\n";
Strange issue - my sendmail.php is working perfectly on desktop and on mobile devices only when requesting desktop websites (in Chrome app), but when using mobile site he does not work at all.
Can someone help me figure this out?
here is the code:
<?php
if(isset($_POST['email'])) {
if (!check_email($_POST['email']))
{
echo 'Please enter a valid email address<br />';
}
else send_email();
}
exit;
function check_email($emailAddress) {
if (filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
return TRUE;
} else {
return FALSE;
}
}
function send_email() {
$message = "\nName: " . $_POST['name'] .
"\nEmail: " . $_POST['email'] ;
$message .= "\nMessage: " . $_POST['comment'] .
"\n\nBrowser Info: " . $_SERVER["HTTP_USER_AGENT"] .
"\nIP: " . $_SERVER["REMOTE_ADDR"] .
"\n\nDate: " . date("Y-m-d h:i:s");
$siteEmail = $_POST['receiver'];
$emailTitle = $_POST['subject'];
$thankYouMessage = "Thank you for contacting us, we'll get back to you shortly.";
if(!mail($siteEmail, $emailTitle, $message, 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>'))
{
echo 'error';
}
else
{
echo 'success';
}
}
?>
You must make sure the mobile form has these two elements:
It is being submitted using method="POST".
Your email input has the attribute and value name="email".
I demonstrate where these things are set in the following code. This is incomplete, of course, it's just designed to show you where the two required parts must be.
<form ... method="POST">
...
<input type="text" name="email" ... >
...
</form>
I need to mention one more thing ...
That being said, what you're doing here is EXTREMELY INSECURE. You are allowing someone to set an email's from and two address on a web form. A (not so) clever hacker can easily write a script to turn your server into an open relay for spam or other evil activities. At minimum, you should remove $_POST['receiver'] and replace with with a hard-coded email address or at least not something that can be altered by an end-user when they POST to your form.
So I drilled down and found that the mobile form is directing submissions through this PHP file which was 404.
The issue now is, even when this file is avaiable, emails are not sent...
' . "\r\n"; $headers .=
'From: ' . "\r\n"; $headers = 'MIME-Version: 1.0' .
"\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message .= "Name: " . $name . ""; $message .= "Subject: "
. $subject . ""; $message .= "Email: " . $email . ""; $message .= "Message: " . $message_text . "";
mail($to, $subject, $message, $headers, "-f noreplay#info.com");
echo 1; }
if(isset($_POST['sendmail']) && $_POST['sendmail'] == 1){
send_mail(); } ?>
I am very new to PHP, so be gentle.
It seems I cant run HTML in PHP.
For example when I try to
echo "<script> success(); </script>";
Result will be:
<script> success(); </script>
Another example:
$msg2 = "<b>Dear</b> ".$name ."\n\n" ."Thank you for your registration, we will be contacting you within 48h."."\n\n" ."Yours ....";
Result will be:
<b>Dear</b> (name of the client)
Thank you for your registration, we will be contacting you within 48h.
Yours ....
I checked the meta its:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I will give You guys the code ( some of it is in Estonian )
<?php
if (isset($_REQUEST['email'], $_REQUEST['nimi'], $_REQUEST['koolitus'], $_REQUEST['telf'], $_REQUEST['kuupaev'], $_REQUEST['tingimused'])) {
$admin_email = "ville.madison#gmail.com";
$email = $_REQUEST['email'];
$koolitus = $_REQUEST['koolitus'];
$nimi = $_REQUEST['nimi'];
$kuupaev = $_REQUEST['kuupaev'];
$telf = $_REQUEST['telf'];
$marked = $_REQUEST['marked'];
$kodulehemail = "koolitused#registreerumine.ee";
$koolitaja = $_REQUEST['koolitaja'];
$beauty = "info#beautyfactory.ee";
$msg = "Koolitusele registreerumine: " . $koolitus . "\n" . "Soovitud koolitaja: " . $koolitaja . "\n\n" . " Nimi: " . $nimi . "\n" . " Email: " . $email . "\n" . " Telefon: " . $telf . "\n" . " Soovitud kuupäev (aaaa-kk-pp): " . $kuupaev . "\n\n" . "Märked: " . $marked;
$msg2 = '<b>Hea</b> ' . $nimi . "\n\n" . "Täname Teid koolitusele broneerimast, teatame Teile registreeringu kinnitamisest 48h jooksul." . "\n\n" . "Teie Beauty Factory";
mail($admin_email, "Koolitusele registreerumine", $msg, "From:" . $kodulehemail);
mail($email, "Koolitusele registreerumine", $msg2, "From:" . $beauty);
}
?>
At the moment there is only one word that I am trying to make in bold for the sake of testing. But when I get it working I will start editing it more and I want to echo a javascript function in the php if statement.
I have tried using single quotes, double quotes and the:
nl2br(htmlentities($msg2));
None of it works
The message you send is not interpreted as HTML. The default
header of the mail function is text/plain.
If you want your customers to see the bold text, you must specify the content-type as HTML in the message header.
<?php
// Set the content type of the email to HTML (and UTF-8)
$header = 'Content-type: text/html; charset=utf-8' . "\r\n";
// Add the from field
$header .= 'From: ' . $beauty . "\r\n";
// Send email
mail ($email, "Koolitusele registreerumine", $msg2, $header);
?>
However you will not be able to use Javascript as it is intepreted client side. PHP does not interpret Javascript, and most of the mail clients will not execute Javascript for obvious security reasons.
Here is a sample example of how to use html tags in php:
<?php
var $name = "xyz";
var $email = "xyz#gmail.com";
echo '<table border="1" style="width:20%">';
echo '<tr><th>UserName</th><th>Email</th></tr><tr><td>';
echo $name."<br>";
echo '</td><td>';
echo $email."<br>";
echo '</td></tr></table>';
?>
This is basic php and html table format example,you can follow this for write html tags.
The problem was that the function mail() had a default content of text/plain, just needed the $header to make text/html. Thanks guys for your quick answers !
The problem I'm having is that I want to pass through data from a number of fields in a form into one email send via PHP, the concerned code is as follows:
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
// This still needs to be debugged online as soon as possible
$from = $_POST['inputemail'];
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$date = $_POST['inputdate'];
$time = $_POST['inputtime'];
$data = $_POST['inputprotection'];
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'];
$message = wordwrap($message, 70);
mail("UP498701#myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
This is where I'm declaring all the information to be passed as a header for the email. The current PHP passes syntax check as works to the extent that an email is sent containing the $to, $subject, $message all fine, but the $header only passes through the final part (Keep Data? $data\n). When I remove all the other fields and simply keep the $data part, the email stops sending any $header. I also have an issue with the redirect, which has been removed from the below code and will be inserted as soon the current issue is resolved. The current full PHP is:
<html>
<body>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
if (isset($_POST['inputemail']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
// This still needs to be debugged online as soon as possible
$from = $_POST['inputemail'];
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$date = $_POST['inputdate'];
$time = $_POST['inputtime'];
$data = $_POST['inputprotection'];
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'];
$message = wordwrap($message, 70);
mail("UP498701#myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
?>
</body>
and is temporarily hosted at http://luke-hale.com/temp/contact.html. I'm sure this is just a case of being misinformed on my part, but any help would be great, cheers!
EDIT:
okay so that issue is sorted with:
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'] . "\r\n\r\n" . $_POST['inputname'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
Which works though doesn't display the $headers anywhere, but I'm not too fussed about this, the next thing is the redirect, which I would usually run through via:
header("refresh:5;url=http://www.domain.com")
Though this was not working correctly earlier, I will apply an edit when tested for anyones future reference.
EDIT
So the form works but the re-direct does not. The site is still hosted on the same domain, but now the full PHP looks like this:
<html>
<body>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
if (isset($_POST['inputemail']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
header("refresh:5;url=http://www.domain.com")
?>
</body>
The form still sends fine but the return states that the line beginning "header" cannot be passed because the browser data has already been modified. I'm not sure sure what I've done or where so if anyone could lend a hand, that'd be great!
FINAL EDIT
My final, fully working, code:
<?php
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
ob_start();
if (isset($_REQUEST['inputemail'])&&($_REQUEST['inputname'])&&($_REQUEST['inputmessage']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid email, you'll be redirected ";
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for your messgae, I'll get back to you as soon as possible! You'll be redirected in 5 seconds.";
}
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
echo "You did not fill all the required fields, please try again.";
header("refresh:5;url=http://www.luke-hale.com/");
}
?>
Try putting your form data into the $message variable, insert \n after each attribute to give a new line. Do not use the header.
Location and Refresh both require an absolute URI
header('Location: http://www.domain.com');
Examples for mail:
https://stackoverflow.com/a/22833890/3489793
https://stackoverflow.com/a/22831825/3489793
mail("Your Email Address Here",$subject,$therest,"subject: $subject\n");
U said this:
"The mail function is also not the issue, and correct syntax is" mail($to,$subject,$message,$headers,$parameters)
Explanation:
your email adress = $to
$subject = $subject
$therest = $message (rename it if you want)
"subject: $subject\n" = the header (change it to $headers if you want that)
Header example (as in the documentation)
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You could echo a Javascript at the bottom of your PHP script which redirects instead, like this:
$url = "url";
$time = 5000 // time in milliseconds
print "<script>window.setTimeout(function(){window.location='".$url."'},".$time.") </script>"
I wrote a script such that if the form fails, the user sees an error message and an email should be sent to me. However, I'm not getting the email. I don't see any mistakes in my script and the email is nowhere to be found (not even in spam). What could be the issue? I believe we are working on a Linux server, so a Mail agent should be installed by default, right? Could a firewall be blocking the messages?
The code:
<?php
if (isset($aErrors) && count($aErrors) > 0 ) {
print '<ul class="errorlist">';
foreach ( $aErrors as $error ) {
print '<li>' . $error . '</li>';
}
print '</ul><br />';
$mail_ontv = "info#example.com";
$_POST['onderwerp'] = "Geen Twinfield Abonnement";
// set datum
$datum = date("d.m.Y H:i");
// set ip
$ip = $_SERVER['REMOTE_ADDR'];
$inhoud_mail .= $_SERVER['SCRIPT_URI'] . "\n\n";
$inhoud_mail .= "Iemand probeerde zich aan te melden, maar had geen Twinfield abonnement!\n";
$inhoud_mail .= "Bedrijfsnaam: " . $_SESSION['bedrijfsnaam'] . "\n";
$inhoud_mail .= "Telefoonnummer: " . $_SESSION['telefoonnummer'] . "\n";
$inhoud_mail .= "E-mail adres: " . $_SESSION['email'] . "\n";
$inhoud_mail .= "Telefoonnummer contactpersoon: " . $_SESSION['telefoonnummercontact'] . "\n";
$inhoud_mail .= "E-mail adres contactpersoon: " . $_SESSION['emailcontact'] . "\n";
$inhoud_mail .= "Gewenste gebruikersnaam: " . $_SESSION['gebruikersnaam'] . "\n";
$inhoud_mail .= "Gekozen abonnement: " . $_SESSION['abonnement'] . "\n\n";
$inhoud_mail .= "Verstuurd op " . $datum . " via het ip " . $ip . "\n\n";
$headers = "From: " . $_SESSION['bedrijfsnaam'] . " <" . $_SESSION['emailcontact'] . ">";
$headers = stripslashes($headers);
$headers = str_replace("\n", "", $headers); // Verwijder \n
$headers = str_replace("\r", "", $headers); // Verwijder \r
$headers = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $headers)); // Slashes van quotes
$_POST['onderwerp'] = str_replace("\n", "", $_POST['onderwerp']); // Verwijder \n
$_POST['onderwerp'] = str_replace("\r", "", $_POST['onderwerp']); // Verwijder \r
$_POST['onderwerp'] = str_replace("\"", "\\\"", str_replace("\\", "\\\\", $_POST['onderwerp'])); // Slashes van quotes
mail($mail_ontv, $_POST['onderwerp'], $inhoud_mail, $headers);
}
?>
Thanks guys!
Several extra thoughts for you beyond what Mac B suggested:
1) There are many PHP libraries out there that will take care of sending an email for you. They can work with a local or a remote SMTP server. For example, http://swiftmailer.org/.
2) Most email providers these days (Gmail, Yahoo, etc) will spam-block email that comes from just your local computer. I recommend using a cheap third-party SMTP service to send your emails - this will help with reliability. One I've been using for years is called CritSend (critsend.com). There are many others.
Did you check the return value of mail()? If you get a boolean fALSE, something blew up while PHP was trying to handle the email over to your SMTP server.
If mail returns true, then the handoff was successful, and you'll have to go look at the SMTP server's logs to find out what happened to the email from that point.
Remember: PHP's involvement with sending email, in a real world equivalent, is taking the envelope and dropping it into a mailbox. That's it. Everything else is completely outside of PHP's purview and control.