PHP regular mail() function prevent spam [duplicate] - php

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.
Here is the simple using of php mail() function codes :
<?php
$from = "From email goes here";
$to = "To email goes here";
ini_set("display_errors", "On");
ini_set("sendmail_from", $from);
ini_set("SMTP", "localhost");
ini_set("smtp_port", "25");
$subject = "Hello, This is subject";
$message = "<p style='color: green;font-weight: bold;'>Hello World, This is Body</p>";
$headers = 'MIME-Version: 1.0' . "\r\n";
//$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Content-Type: text/html; Charset=UTF-8' . "\r\n";
$headers .= 'From: Birthday Reminder <' . $from . '>' . "\r\n";
$headers .= 'To: Mary <' . $to . '>' . "\r\n";
$headers .= 'Reply-To: Birthday Reminder <' . $from . '>' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message,$headers))
{
echo("<p style='color: blue;font-weight: bold;'>Email Sent :D</p>");
}
else
{
echo("<p style='color: red;font-weight: bold;'>Email Message delivery failed...</p>");
}
?>
Why emails were sent using these codes always go to the spam folder?
Is the order of headers ok?
Which extra headers should i use for prevent spam?
Edit :
I know there are some threads in stack about that.
But i am looking for an updated answers.
So it's not duplicate.

This not only about your script, you may check the reputation of your IP, you shall also set SPF record and DKIM key for help
https://mxtoolbox.com/
This online tool will help you to setup properly your mail server
You may check the senderscore of your IP, since it can affect badly the delivery of your mails
https://www.senderscore.org

Related

PHP xammp sendmail error: Message is missing sender's address [duplicate]

This question already has answers here:
php single and double quotes
(2 answers)
Closed 1 year ago.
I have set up the Sendmail client on my localhost xammp server running on my home pc but I am getting the following error:xx-xx-xx xx:xx:xx: Message is missing sender's address. I have gotten it to work when I use the hard code what I want to send, like so:
$msg = "hello world";
mail("example#gmail.com","My subject",$msg, 'From: admin#myEmailClient.com');
but when I try and implement my login validation script It does not work and I get the aforementioned error.
emailvalidation.php
$to = $email;
$sub = 'email verification';
$msg = "<a href='http://localhost/verify.php?vkey=$vkey'>account verification </a>";
$headers = "From: admin#myEmailClient.com \r\n";
$headers = "MIME-Version: 1.0". '\r\n';
$headers = "Content-type:text/html;charset=UTF-8 ". '\r\n';
mail($to, $sub, $msg, $headers);
my email variable is set by the user if that helps, I don't think its a problem with the items being parsed because when I check my database all rows are properly filled in.
thank you for your time
edit
I think it's a problem with the r\n parts, these are the parts that enable HTML in emails. when I get rid of them it works
I'm not sure if I should delete this question because of the speed I was able to solve it but I hope this helps someone who had the same problem as I did.
here is how I solved the problem
$message = "<a href='http://localhost/verify.php?vkey=$vkey'>account verification </a>";
$to = $email;
$subject = 'account validation';
$from = 'admin#myEmailClient.com';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){
echo 'Your mail has been sent successfully.';
} else{
header("location: index.PHP");
}
I got the solution from here. tutorial republic

mail() function not working for domain mail address [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to use php mail() function on my new web server(linux based server) for my website. The issue is, emails are not sending to domain email addresses like some1#domain.com but its working fine for gmail, yahoo. I don't know what is the issue on it?. please give me suggestions or advice how to solve this issue. I want to send emails to domain-based email addresses.
my code is
//$to = $_POST['femail'];
$to = "<toadd#domain.com>";
$message = "
<html>
<head>
<title>".$subject."</title>
</head>
<body>
<p>Registration request from site</p>
<table>
<tr>
<td>Project Requested</td>
<td>".$project."</td>
</tr>
</table>
</body>
</html>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <info#domain.com>' . "\r\n";
$headers .= 'Cc: <some#domain.com>' . "\r\n";
$headers .= 'X-Mailer: PHP/' .PHP_VERSION. "\r\n";
#mail($to,$subject,$message,$headers);
I have faced the same issue..after a talk with hosting service provider I came to know that either sender or receiver's email id should be of the hosting domain id like if you have a site test.com either sender or receiver should have #test.com so you may do the same or talk to your hosting service provider.
$to = "toadd#domain.com"; //to address
$subject ="Your Subject";
$message = "Your message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: info#domain.com' . "\r\n"; //from address
if (mail($to, $subject, $message, $headers) )
{
echo "Mail sent successfully";
}
else
{
echo "failed to send mail";
}

mail function in php not sending headers

I brought a shared sever, and got a little storage space. When i run my web site, the mail function works perfectly. Mail has been sent with subject and message to all receivers. But the mail which is sent mentioned "From : ". Headers what i included are not working.
I want to send with my headers Eg:Form: mymail#mysite.com . How can i do it?
<?php
$output="";
if(isset($_POST['submit']) && !empty($_POST['message']))
{
$sub=$_POST['subject'];
$msg=$_POST['message'];
$sql = "select email from login where status='client'";
$query = mysqli_query($con,$sql);
$to=array();
while($row = mysqli_fetch_array($query))
{
$to[] = $row['email'];
$parts = implode(',',$to);
$headers = 'From: mymail#mysiet.com;';
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$subject = $sub;
$content = $msg;
}
if(mail($parts,$subject,$content,$headers))
{
$output="mail sent";
}
else
{
$output="Error in connection, Mail could not be sent. Please try again";
}
}
?>
You need to manually specify the headers, this is the function I use and it delivers to pretty much every provider beautifully. The headers you want are From: and Reply-To: and I also advise the X-Mailer follows what is seen here, I found it years ago via SO as well.
<?php
$to = "user#anotherdomain.com";
$headers = 'From: My Name <email#domain.com.au>' . "\r\n" .
'Reply-To: My Name <email2#domain.com.au>' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'Content-type: text/html; charset=iso-8859-1';
$subject = "My subject line here";
$message = 'Message HTML and Content Here.';
if(mail($to, $subject, $message, $headers)){
// Success Here
}else{
// Failed Here
}
?>
I am not 100% certain it will work for you as I am on a dedicated machine, having said that - I do use it on multiple domains and have never messed with my Apache or php configuration in relation to mail settings.
Read More about the Mail function in PHP: http://php.net/manual/en/function.mail.php
You can try below code, just ripped from php.net
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
Off topic but good to use PHPMailer.

PHP mail won't send when there is a url a tag in the body

<?php
// multiple recipients
$to = 'ali.dzinemedia#gmail.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '<html>
<head></head>
<body>Content here and this is a link</body>
</html>';
// To send HTML mail, the Content-type header must be set
// Additional headers
$headers = 'To: Mary <ali.dzinemedia#gmail.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <ali.dzinemedia#gmail.com>' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset: utf8\r\n";
echo "To : ".$to;
// Mail it
mail($to, $subject, $message, $headers);
?>
please solved this . when i added Download in the mail body mail not send.
if i remove this a tag mail sent and all other content display as per my requirement .
I dont know where is the exact problem, i m using godady hosting with PHP 5.3 version.
if any one have better solutions please share with me .
I've adjusted your code a little bit. Main change is in From: header - $header = "From: \"HCS Support Team\" <$from_id>\r\n" . "Reply-To: $from_id\r\n";
$erm.= '<TR>
<TD WIDTH="50%">' . "Download-" . $j . '</TD>
<TD WIDTH="50%">Download</TD>
</TR>';
$from_id = "support#xyz.com";
$header = "From: \"HCS Support Team\" <$from_id>\r\n" . "Reply-To: $from_id\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
$subject = "Request for Case Study Solution Received";
#mail("ali.dzinemedia#gmail.com", $subject, $erm, $header);
It seems the mail is being sent, but it goes straight to SPAM folder. Please read about spam rules which make the mails go to spam folder: http://spamassassin.apache.org/tests_3_3_x.html
In your case the main problem with gmail is probably it's being sent with standard PHP mail function. Please try PHPMailer or PEAR::Mail.
The rules which are met by spamassasin in your mail are:
HTML_MESSAGE
HTML_MIME_NO_HTML_TAG
MIME_HTML_ONLY

php mail function with customizes from value

Why does this send emails?
$from_admin = $_POST[EMAIL];
$message_admin = 'some html...';
mail($to_admin,"subject",$message_admin,"FROM:$from_admin");
This email looks like it came from my gmail account.
While this doesn't send the email.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To:' . $to . "\r\n";
$headers .= 'From: Registration <register#myserver.org>' . "\r\n";
mail($to, $subject, $message, $headers);
When I contacted my hosting support, they said that I need to do SMTP authentication. But, then why does the first mail function work? I would be ok if its not actually using that server, but people can reply the the html email and still reach the correct account. Is there any way around this?

Categories