-
send mails in php
Am trying to send mail to 3 gmail recipient account. The codes runs but could not send mail to any of the account. am using xammp server. can someone tell me what is wrong
Thanks
Code:
<?php
if(!isset($_POST['submit']))
{
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
require('pdo_db.php');
$result = $db->prepare('SELECT * FROM email_list
order by id asc');
$result->execute(array());
while ($row = $result->fetch())
{
$addresses[]=$row['email'];
}
$email_from = 'me@gmail.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
$to = implode(", ", $addresses);
mail($to,$email_subject,$email_body,$headers);
//echo $to;
header('Location: thanks.html');
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
-
xampp? Is this on your local machine? If so, have you installed and configured an email server?
Also, you don't check if your call to mail
was successful or not. And do you have error reporting enabled?
~~~~~~~~~~~~~~~~~~~
not related to your specific question:
You make an attempt to check the submitted email address for injection attempts, but you don't check if it a valid email address.
You can use filter_var
, with the FILTER_VALIDATE_EMAIL
flag, to check. As an added benefit, this will fail if there are any line break characters, so it will also stop header injections in the email field.
-
i have re-write the mail as follows. it works now however, it sends email to spam instead of the mail inbox. what could be the problem.
Code:
<?php
if(!isset($_POST['submit']))
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
}
$message = strip_tags($_POST['message']);
if(empty($message))
{
echo "message is mandatory!";
exit;
}
require('pdo_db.php');
$result = $db->prepare('SELECT * FROM email_list
order by id asc');
$result->execute(array());
while ($row = $result->fetch())
{
$addresses[]=$row['email'];
}
$to = implode(", ", $addresses);
$subject = "Testing";
//$message = "Hello! mail from .";
$from = "me@gmail.com";
$headers = "From:" . $from;
$retval = mail($to,$subject,$message,$headers);
if( $retval == true )
{
echo "Message sent successfully...";
}
else
{
echo "Message could not be sent...";
}
?>
Thanks
-
Tips & Tricks to Stay Out of the Spam Folder
Most likely cause, in this case, is item #1: "misleading header information". When the "From" header has one domain ("gmail.com", for example) but the email came from a server that doesn't belong to that domain (for example, your web host's mail server), you get spam points. This, by itself, is often enough to get your email into the spam folder.
1) Use your website's domain name in the From header (it doesn't need to be a real account; "no-reply@your.site.example.com" is fine), or,
2) Add a "Sender" header that uses you website's domain name.
There may be any number of additional causes, however, so don't be surprised if this doesn't automatically work. Many are harder to solve, and much harder to diagnose. Your best bet, since you're sending to your own email account, is to whitelist the "From" email in your email client.