This question already has answers here:
Why is my e-mail still being picked up as spam? Using mail() function
(5 answers)
Closed 9 years ago.
first time post. I did read through the site on this question but I didn't find the answer or didn't realize I found the answer. I'm putting a simple PHP email sign-up box on a web site. Here is my code:
enter code here
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
$recipient = "mymail#mydomain.com";
$subject = "Email subscription list";
$sender = $recipient;
$subscription = $_REQUEST['subscription'];
if (isset($_REQUEST['emaillist']))
$mailcheck = spamcheck($_REQUEST['emaillist']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$body .= "Email: ".$_REQUEST['emaillist']." \n";
$body .= "Subscribe: ".$_REQUEST['subscription']." \n";
if ($subscription == "subscribe")
{$location = "thankyou.html";}
else {$location = "thankyou2.html";};
mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");
header( "Location: $location" ); } ?>
The emails go to the spam folder using either my gmail or an email on the site's domain. I think it's because the subject and recipient are the same, but it could simply be a matter of telling our site host to allow these mails through. Any help/suggestions are appreciated and thank you in advance.
$sender = $recipient;
Since you are sending an email to yourself, create a filter to prevent mails from yourself going into spam. Creating filters is explained here
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 2 years ago.
I have a problem to send cc in php mail function to get feedback from the client.
Here is a simple php code-
<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["email"]==""||$_POST["sub"]==""||$_POST["msg"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$subject = $_POST['sub'];
$name = $_POST["name"];
$message = $_POST['msg'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
// Send Mail By PHP Mail Function
mail("xyz#example.com", $subject, $message, $headers);
echo "Your mail has been sent successfuly !";
}
}
}
?>
The mail is sent to the sender correctly but cc is not sent. Moreover when I fixed the email address in the Cc field i.e abc#xxx.com instead of $email, then Cc works. Help me to solve the problem.
I used to have tons of troubles with this. Try to install php mailer plugin
https://github.com/PHPMailer/PHPMailer.
This will also prevent future issues and save you loads of time when you realize you might want to get more out of sending an email.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.
I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)
Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)
Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 7 years ago.
Can someone help me figure out why the below PHP is causing the email to be sent to Gmail spam? I tried following other directions for setting proper headers, but I still run into problems with my emails going into the spam filter in Gmail.
Any help would be appreciated!
<?php
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['submit']))
{
$name = trim($_POST['txt_name']);
$fromemail = trim($_POST['txt_email']);
$inquiry = trim($_POST['txt_inquiry']);
$message = trim($_POST['txt_msg']);
//name can contain only alpha characters and space
if (!preg_match("/^[a-zA-Z ]+$/",$name))
{
$error = true;
$name_error = "Please enter a real name";
}
if(!filter_var($fromemail,FILTER_VALIDATE_EMAIL))
{
$error = true;
$fromemail_error = "Please enter a valid email address";
}
if(empty($inquiry))
{
$error = true;
$inquiry_error = "Please enter your subject";
}
if(empty($message))
{
$error = true;
$message_error = "Please enter your message";
}
if (!$error)
{
//send mail
$toemail = "myemail#gmail.com";
$subject = "inquiry from visitor " . $name;
$body = "Here goes your Message Details: \n\n Name: $name \n From: $fromemail \n Inquiry: $inquiry \n Message: \n $message";
$headers = "From: $fromemail\n";
$headers .= "Reply-To: $fromemail";
$headers .= "Return-Path: $fromemail";
if (mail ($toemail, $inquiry, $body, $headers))
$alertmsg = '<div class="alert alert-success text-center">Message sent successfully. We will get back to you shortly!</div>';
else
$alertmsg = '<div class="alert alert-danger text-center">There is error in sending mail. Please try again later.</div>';
}
}
?>
It could be that your ip address of the server that you are sending the email with it used for other purposes that marked it as a spam address.
Most of the time when you add DKIM and SPF to your email server (if you use one) will solve this problem.
An essier solution would be to use an external mailing service so that you they can handle that for you
I often made the experience, that mails that are directly getting sent by PHP are recognized as SPAM...
my approach is now, to always use a SMTP-Server for sending those emails...
this post could help you!
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I tried below code but it was not working correctly. please help me
<?php
$email = $_POST['email'];
$feedback = $_POST['feedback'];
$to = "balvant1#gmail.com";
$subject = "Mail from HRMSsystem for New Requirement";
$message .= "<b>Email : $email </b>";
$message .= "<b>feedback : $feedback </b>";
$header = "From:xyz#info.com \r\n";
$header = "Cc:balvant#gmail.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail($to,$subject,$message,$hearder);
if( $retval == true )
{echo "Message sent successfully...";}
else{ echo "Message could not be sent..."; }
?>
Thanks in advance.
Three things...
Most hosting companies don't allow you to "bounce" mail off of their servers so unless you own the domain info.com AND you are hosting it on their server, chances are your mail is being blocked.
As #Louis Loudog Trottier said, $retval = mail($to,$subject,$message,$hearder); should be $header not $hearder
You don't necessarily have to have "xyz#info.com" set up. I often use noreply#my-domain-name.tld (which is a non-existent email address) to send mail, BUT you do have to comply with #1.
Also, see the comment by #Svengali as well
Although not a direct answer to your question, using mail() to send your emails is seldom a good idea. Using an email class library is simpler and more straight forward in my opinion.
Please have a look at swiftmailer.org for example.
This question already has answers here:
How to avoid my mails sent from PHP mail() being marked as spam?
(2 answers)
php mail function: legitimate mails marked as spam by gmail and hotmail
(2 answers)
Closed 8 years ago.
i have used mail() , when i tried with "test" as subject mails are going fine to the inbox but when i change the subject line to 6 or 7 words mails are going to spam . Please can any one help me out . because my subject will be keep on changing so i need a permanent solution even after i change the subject line .
i have tried to put the sender in $header and i checked with mime type still it does not work
require_once("mailer.php");
if(isset($_POST['to']) && !empty($_POST['to']) && isset($_POST['subject']) && !empty($_POST['subject']) && isset($_POST['from'])
&& !empty($_POST['from'])){
$receieverEmailIdArray = explode(',',$_POST['to']);
$from= $_POST['from'];
$subject= $_POST['subject'];
}
else {
echo "Fill all the fields.";
die();
}
$smtptype= 'godaddy';
$content= "<html>
</html>"
Thanks,
Raghu
You can add the Detailed headers Code below.
$headers = "From: Example#site.com\r\n";
$headers .= "Reply-To: Example#site.com\r\n";
$headers .= "Return-Path: Example#site.com\r\n";
$headers .= "CC: Example#site.com\r\n";
if ( mail($to,$subject,$message,$headers) )
{
echo "The email has been Successfully sent!";
}
else
{
echo "Email Sending Failed";
}
?>
Refer this Site this has some useful Information Click Here!!!