I have a form on my page and the information I submit keeps going to my junk folder within my Hotmail email.
I'm not sure what to do I keep changing the headers but the mail still keeps coming into my junk folder.
The code for the PHP is:
<?php
$boundary = uniqid('np');
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'user#hotmail.co.uk';
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP's mail() Function\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
$headers .= "Subject: Contact\r\n";
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
$message .= "This is the text/plain version.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
$message .= "This is the <b>text/html</b> version.";
$message .= "\r\n\r\n--" . $boundary . "--";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. I will contact you shortly.');
window.location = 'http://www.user.com';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to user#hotmail.co.uk');
window.location = 'url';
</script>
<?php
}
?>
One possible problem is if the smtp server from which you are sending the emails, doesn't match the "from" email.
For example if you fill the from field with "yourmail#gmail.com" and the smtp server is not the gmail's one, this might be marked as spam by some mail providers. Unfortunately, there is nothing you can do about it, besides from sending emails from your own domain, that resolves to your smtp server.
Related
This question already has answers here:
How do you make sure email you send programmatically is not automatically marked as spam?
(24 answers)
Closed 2 years ago.
I am using PHP's mail() function to send emails (sendmail process is running). But all the mails are going to spam (in case of gmail). I have tried many tricks that I found on the net but none is working, please tell me about any sure-shot trick.
You must to add a needle headers:
Sample code :
$headers = "From: myplace#example.com\r\n";
$headers .= "Reply-To: myplace2#example.com\r\n";
$headers .= "Return-Path: myplace#example.com\r\n";
$headers .= "CC: sombodyelse#example.com\r\n";
$headers .= "BCC: hidden#example.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
There is no sure shot trick. You need to explore the reasons why your mails are classified as spam. SpamAssassin hase a page describing Some Tips for Legitimate Senders to Avoid False Positives. See also Coding Horror: So You'd Like to Send Some Email (Through Code)
<?php
$subject = "this is a subject";
$message = "testing a message";
// declare variable
$headers = "Reply-To: The Sender <sender#domain.com>\r\n";
// add more info
$headers .= "Return-Path: The Sender <sender#domain.com>\r\n";
$headers .= "From: The Sender <sender#domain.com>\r\n";
$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
mail("reciever#domain.com", $subject, $message, $headers);
?>
Try PHP Mailer library.
Or Send mail through SMTP filter it before sending it.
Also Try to give all details like FROM, return-path.
$fromMail = 'set your from mail';
$boundary = str_replace(" ", "", date('l jS \of F Y h i s A'));
$subjectMail = "New design submitted by " . $userDisplayName;
$contentHtml = '<div>Dear Admin<br /><br />The following design is submitted by '. $userName .'.<br /><br /><b>Click here</b> to check the design.</div>';
$contentHtml .= '<div><img src="'.$imageUrl.'" width="250" height="95" border="0" alt="my picture"></div>';
$contentHtml .= '<div>Name : '.$name.'<br />Description : '. $description .'</div>';
$headersMail = '';
$headersMail .= 'From: ' . $fromMail . "\r\n" . 'Reply-To: ' . $fromMail . "\r\n";
$headersMail .= 'Return-Path: ' . $fromMail . "\r\n";
$headersMail .= 'MIME-Version: 1.0' . "\r\n";
$headersMail .= "Content-Type: multipart/alternative; boundary = \"" . $boundary . "\"\r\n\r\n";
$headersMail .= '--' . $boundary . "\r\n";
$headersMail .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headersMail .= 'Content-Transfer-Encoding: base64' . "\r\n\r\n";
$headersMail .= rtrim(chunk_split(base64_encode($contentHtml)));
try {
if (mail($toMail, $subjectMail, "", $headersMail)) {
$status = 'success';
$msg = 'Mail sent successfully.';
} else {
$status = 'failed';
$msg = 'Unable to send mail.';
}
} catch(Exception $e) {
$msg = $e->getMessage();
}
This works fine for me.It includes mail with image and a link and works for all sorts of mail ids. The clue is to use all the header perfectly.
If you are testing it from localhost, then set the below before checking:
How to set mail send from localhost xampp:
comment everything in D:/xampp/sendmail/sendmail.ini and mention the below under
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=yourmailid#domain.com
auth_password=your-mail-password
force_sender=yourmailid#domain.com
In D:/xampp/php/php.ini
a. Under
[mail function]
SMTP = smtp.gmail.com
smtp_port = 587
b. set sendmail_from = yourmailid#domain.com
c. uncomment sendmail_path = "\"D:\xamp\sendmail\sendmail.exe\" -t"
Hence it should be look like below
sendmail_path = "\"D:\xamp\sendmail\sendmail.exe\" -t"
d. comment sendmail_path="D:\xamp\mailtodisk\mailtodisk.exe"
Hence it should be look like below
;sendmail_path="D:\xamp\mailtodisk\mailtodisk.exe"
e. mail.add_x_header=Off
<?php
$to1 = 'test#gmail.com';
$subject = 'Tester subject';
// To send HTML mail, the Content-type header must be set
$headers .= "Reply-To: The Sender <sender#sender.com>\r\n";
$headers .= "Return-Path: The Sender <sender#sender.com>\r\n";
$headers .= "From: sender#sender.com" ."\r\n" .
$headers .= "Organization: Sender Organization\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;
?>
I am trying to generate an email on signing up to website, I am using PHP to create and send the email. My domain has both SPF and DKIM running on it. However when i spam check my email i get this:
[SPF] srv61.hosting24.com does not allow your server 31.220.105.111 to use face159#srv61.hosting24.com
[Sender ID] srv61.hosting24.com does not allow your server 31.220.105.111 to use face159#srv61.hosting24.com
Your message is not signed with DKIM
I have contacted my hosting and they confirm that all the authentication systems are working on there end, so i must be doing something wrong in the PHP code but for the life of me i can't find what here is how i generate the email:
//Emails link to voucher
//create a boundary for the email. This
$boundary = uniqid('np');
$subject = "The Forum - Little Thank You";
//headers - specify your from email address and name here
//and specify the boundary for the email
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: TheForum#freewifisystems.co.uk" . "\r\n";
$headers .= "To: " . $email . "\r\n";
$headers .= "Content-Type: multipart/alternative;boundary=" . $boundary . "\r\n";
//here is the content body
$message = "This is a MIME encoded message.";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/plain;charset=utf-8\r\n\r\n";
//Plain Email
$content2 = "plain text email here";
//Plain text body
$message .= "Hello,\nThis is a text email as HMTL is dissable, Please enable HTML to continue. \n\nRegards,\nThe Forum";
$message .= "\r\n\r\n--" . $boundary . "\r\n";
$message .= "Content-type: text/html;charset=utf-8\r\n\r\n";
//Html body
$message .= $content3;
$message .= "\r\n\r\n--" . $boundary . "--";
//invoke the PHP mail function
mail($email, $subject, $message, $headers);
?>
$email and $content3 are set via SQL and are setting correctly the email forms and is well formatted.
I am not receiving emails from this contact form, but the message seems to sending okay and it also redirects me to the sent page.
I don't have access to the server only via FTP.
PHP
<?php
$to = 'test#gmail.com';
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$body = <<<EMAIL
<html>
<p><h3>Email Submited From Website.</h3></p>
<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Subject:</strong> $subject</p>
<p><strong>Message:</strong> $comment</p>
</html>
EMAIL;
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: test email' . "\r\n";
$headers .= 'From: <website#mt.co.uk>' . "\r\n";
//$headers .= 'Cc: noreply#example.com' . "\r\n";
//$headers .= 'Bcc: noreply#example.com' . "\r\n";
if ($_POST['submit']){
mail($to, $subject, $body, $headers);
header ("Location: message-sent.php");
die();
} else {
header ("Location: message-failed.php");
die();
}
?>
Check if mail is actually being sent:
if (mail($to, $subject, $body, $headers)===false) {
echo "Not sent!";
} else {
echo "Sent!";
}
Change this:
$headers .= 'To: test email' . "\r\n";
$headers .= 'From: <website#mt.co.uk>' . "\r\n";
To this:
$headers .= "To: $to <test email>\r\n";
$headers .= "From: website#mt.co.uk <website#mt.co.uk>\r\n";
Also, you need to sanitize the subject and body of the email so that the email arrives, but this will usually be reflected in the results after email() reports a success, in that case the email will bounce, go to the spambox, or simply be refused.
If your hosting provider doesn't have an email server, you could try to use a free email server and phpMailer. https://github.com/PHPMailer/PHPMailer
I have this php mail() code on my site, which works fine.
It is an ecard system, my client can fill out the from info (name, email) and to info (name, email), etc and Mail goes straight to Inbox, on google, hotmail, yahoo, etc.
When the receiver gets the email he can use the reply button, and it gets the right info.
The problem is the From: header in my mail form, I want to change this from noreply#example.com to the receiver's info, or any other info. When I do that, the mail goes into SPAM mail.
Here is the code I'm using
<?php
$name = $_REQUEST['name'] ;
$motive = $_REQUEST['email'] ;
$name2 = $_REQUEST['name2'] ;
$email2 = $_REQUEST['email2'] ;
$message = $_REQUEST['message'] ;
$subject = $_REQUEST['subject'] ;
$message = urldecode(stripslashes($message));
$headers = 'From:' . $name . ' John Q<noreply#example.com>' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers.= "Reply-To:" .$name. "<" . $motive. ">\n";
$headers.= "Return-Path: My Company<admin#datatopixels.com>\n";
$headers.= "X-Mailer: PHP". phpversion() ."\n";
$messagee = "
<html>
<head>
<title>Title here</title>
</head>
<body>
<center>
<br>
</center>
</body>
</html>
";
mail($to, $subject, $messagee, $headers);
?>
You must to add a needle headers:
Sample code :
$headers = "From: myplace#example.com\r\n";
$headers .= "Reply-To: myplace2#example.com\r\n";
$headers .= "Return-Path: myplace#example.com\r\n";
$headers .= "CC: sombodyelse#example.com\r\n";
$headers .= "BCC: hidden#example.com\r\n";
if ( mail($to,$subject,$message,$headers) ) {
echo "The email has been sent!";
} else {
echo "The email has failed!";
}
?>
And I would suggest to read oswalds's
I have problem with non english characters with this e-mail script. If I set the script to send e-mail to my gmail account, I have no problem. However, if I set the script to send email to my domain account and if i open the email with windows live mail or with microsoft outlook then the e-mail is not readable. I must to go to encoding menu, and then select the utf-8, and then i can read the email.
If I open the mail on iMac mail client, i have no problem.
My customer see this as big problem and want me to solve it. Anyone can help?
Here is the code:
<?php
// send the form to the specify email
// CONFIG VARS
$subject = "mysite.com | contact form";
$to = "myaccount#somemail.com";
$from = 'another#somemail.com';
//data
$msg = "Name: " .$_POST['namesup'] ."<br>\n";
$msg .= "Email: " .$_POST['emailsup'] ."<br>\n";
$msg .= "Phone: " .$_POST['phonesup'] ."<br>\n";
$msg .= "Message: " .$_POST['yourtextsup'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: <".$from. ">" ;
//send
mail($to, $subject, $msg, $headers);
?>
Any help will be deeply appreciated.
Zoran
Checkout this code it will work for me,
<?php
$subject = "mysite.com | contact form";
$to = "myaccount#somemail.com";
$from = 'another#somemail.com';
$msg = '<html>
<head>
</head>
<body>
<p>
Name: ".$_POST['namesup']."<br>
Email: ".$_POST['emailsup']."<br>
Phone: ".$_POST['phonesup']."<br>
Message: ".$_POST['yourtextsup']."<br>
</p>
</body>
</html>';
$headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n";
$headers .= "X-Mailer: PHP \r\n";
$headers .= "From: <".$from. ">" ;
mail($to,$subject,$msg,$headers);
?>
This line $headers = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/html; charset=UTF-8' . "\r\n"; is resolved your issue of like Arabic language or others.
I would suggest making sure your message is valid HTML, including the <html> and <head> tags, and also make sure you include the <meta http-equiv="Content-Type" content="text/html charset=UTF-8" /> tag. So:
//data
$msg = '<html><head><meta http-equiv="Content-Type" content="text/html charset=UTF-8" /></head><body>';
$msg .= "Name: " .$_POST['namesup'] ."<br>\n";
$msg .= "Email: " .$_POST['emailsup'] ."<br>\n";
$msg .= "Phone: " .$_POST['phonesup'] ."<br>\n";
$msg .= "Message: " .$_POST['yourtextsup'] ."<br>\n";
$msg .= '</body></html>';
Try changing your scripts charset to:
<?php
// send the form to the specify email
// CONFIG VARS
$subject = "mysite.com | contact form";
$to = "myaccount#somemail.com";
$from = 'another#somemail.com';
//data
$msg = "Name: " .$_POST['namesup'] ."<br>\n";
$msg .= "Email: " .$_POST['emailsup'] ."<br>\n";
$msg .= "Phone: " .$_POST['phonesup'] ."<br>\n";
$msg .= "Message: " .$_POST['yourtextsup'] ."<br>\n";
//Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";
$headers .= "From: <".$from. ">" ;
//send
mail($to, $subject, $msg, $headers);
?>