I have a script that recognizes the level and I have added alerts to let me know if I have passed through the code. The two email addresses are reflecting. I want to send out two emails seperately, but my inbox is not working. I have checked my junk mail.
Is there something that am i missing?
function emaillog($to,$id,$subject,$message){
include("dbconnect.php");
mysql_query("INSERT INTO emlog(mm,tt,ss,rr) VALUES('$message','$to','$subject','$id')");
}
if($level == 1){
$assignedtowho_email_result = mysql_query("SELECT Email FROM sheet1 WHERE id IN(SELECT assignedtowho FROM tbl_one WHERE id =$id)");
while($row_email=mysql_fetch_array($assignedtowho_email_result)){
$assignedtowho_email=$row_email['Email'];
}
// Email Sending department
$to = $senderEmail;
$subject = "Refferal status updated by recieving r";
$message = "Your status has been updated by ";
$from = "info#test.co.za";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
emaillog($to,$id,$subject,$message);
$message_alert="Sender mail sent: ".$to;
echo '<script>alert("'.$message_alert.'")</script>';
//echo "Mail Sent.";
// Email Recieving department
$to = $assignedtowho_email;
$subject = "Refferal status updated ";
$message = "Your refferal status has been updated";
$from = "info#test.co.za";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
emaillog($to,$id,$subject,$message);
$message_alert_2="Assigned mail sent: ".$to;
echo '<script>alert("'.$message_alert_2.'")</script>';
//echo "Mail Sent.";
}
Requirements
For the Mail functions to be available, PHP must have access to the sendmail binary on your system during compile time. If you use another mail program, such as qmail or postfix, be sure to use the appropriate sendmail wrappers that come with them. PHP will first look for sendmail in your PATH, and then in the following: /usr/bin:/usr/sbin:/usr/etc:/etc:/usr/ucblib:/usr/lib. It's highly recommended to have sendmail available from your PATH. Also, the user that compiled PHP must have permission to access the sendmail binary.
http://tr2.php.net/manual/en/mail.requirements.php
Be sure you have all requirements and SMTP server.
Therefore, mail() function prop. can change depends on your OS and PHP version
You can see all of this [here][1]
in ChangeLog side.
If you are using Windows , you must use PEAR Mail . İt's here
For Linux OS such as Ubuntu and Centos , you must be sure configure php.ini and apache/httpd.ini
You use this for on every OS
<?php # Is the OS Windows or Mac or Linux
if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
$eol="\r\n";
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
$eol="\r";
} else {
$eol="\n";
} ?>
<?php
# File for Attachment
$f_name="../../letters/".$letter; // use relative path OR ELSE big headaches. $letter is my file for attaching.
$handle=fopen($f_name, 'rb');
$f_contents=fread($handle, filesize($f_name));
$f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode();
$f_type=filetype($f_name);
fclose($handle);
# To Email Address
$emailaddress="user#example.com";
# Message Subject
$emailsubject="Heres An Email with a PDF".date("Y/m/d H:i:s");
# Message Body
ob_start();
require("emailbody.php"); // i made a simple & pretty page for showing in the email
$body=ob_get_contents(); ob_end_clean();
# Common Headers
$headers .= 'From: Jonny <jon#example.com>'.$eol;
$headers .= 'Reply-To: Jonny <jon#example.com>'.$eol;
$headers .= 'Return-Path: Jonny <jon#example.com>'.$eol; // these two to set reply address
$headers .= "Message-ID:<".$now." TheSystem#".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers
$mime_boundary=md5(time());
$headers .= 'MIME-Version: 1.0'.$eol;
$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;
$msg = "";
# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: application/pdf; name=\"".$letter."\"".$eol; // sometimes i have to send MS Word, use 'msword' instead of 'pdf'
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".$letter."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
# Setup for text OR html
$msg .= "Content-Type: multipart/alternative".$eol;
# Text Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= "This is a multi-part message in MIME format.".$eol;
$msg .= "If you are reading this, please update your email-reading-software.".$eol;
$msg .= "+ + Text Only Email from Genius Jon + +".$eol.$eol;
# HTML Version
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol;
$msg .= $body.$eol.$eol;
# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection.
# SEND THE EMAIL
ini_set(sendmail_from,'from#example.com'); // the INI lines are to force the From Address to be used !
mail($emailaddress, $emailsubject, $msg, $headers);
ini_restore(sendmail_from);
?>
Related
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am using this code for my contact form in a html website but mail is not coming in Gmail Inbox.
Can any one help me i am trying to solve this issue but i don't have any guide.
<?php
session_cache_limiter( 'nocache' );
$subject = $_REQUEST['subject']; // Subject of your email
$to = "iamuser#gmail.com"; //Recipient's E-mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "From: " . $_REQUEST['name'].'<'.$_REQUEST['email'] .'>'. "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = 'Name: ' . $_REQUEST['name'] . "<br>";
$message .= 'Company: ' . $_REQUEST['company'] . "<br>";
$message .= $_REQUEST['message'];
if (#mail($to, $subject, $message, $headers))
{
// Transfer the value 'sent' to ajax function for showing success message.
echo 'sent';
// header('Location: ../index.html');
}
else
{
// Transfer the value 'failed' to ajax function for showing error message.
echo 'failed';
}
?>
There are some problem in setting the header.
And most important is that you need to define the correct and valid Email Id in the From section because google generally used to validate the domain, the mail coming from.
If it is not white-listed at google end then it wills end the mail to Spam automatically, that is happening now as I think.
The problem is simple that the PHP-Mail function is not using a well configured SMTP Server.
Nowadays Email-Clients and Servers perform massive checks on the emails sending server, like Reverse-DNS-Lookups, Graylisting and whatevs. All this tests will fail with the php mail() function. If you are using a dynamic ip, its even worse.Use the PHPMailer-Class and configure it to use smtp-auth along with a well configured, dedicated SMTP Server (either a local one, or a remote one) and your problems are
You can try the below 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";
Reference Link.
https://github.com/PHPMailer/PHPMailerPHPMailer/PHPMailerPHPMailer
There is another code you can try:
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain;charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream;name=\"".$filename."\"\r\n";
// use different content types here$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment;filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {echo "mail send ... OK";
// or use booleans here} else {echo "mail send ... ERROR!";
}
}
If you are developing this program in local server. The emails will not be sent to your gmail account.
If you want to test your code on a local machine please install Test Mail Server Tool.
No email will be delivered while running on local machine but you will get an idea how the email will look like.
When you run the same on web hosting server, the email will be delivered to the email id specified in $to field.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
My mails using php mail() are going to the junk mail folder - why?
<?php
ob_start();
session_start();
//define the receiver of the email
$from='mrashidap#gmail.com';
$to = 'mrashidap#gmail.com';
//define the subject of the email
$subject = 'inform me please, when u receive this mail';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
#Now We Can Use HTML Tags
$headers = "From: " . strip_tags($from). "\r\n";
$headers .= "Organization: appstribes\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "Reply-To: ".($from) . "\r\n";
$headers .= "Return-Path: ".($from) . "\r\n";
$headers .= "X-Priority:3 \r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
$headers .= "X-MSMail-Priority: high\r\n";
$msg = '<html><body>';
$msg .= '<p>Dear Sir/Madam</p><br/>';
$msg .= '<p><strong>GOOD DAY..!!!</strong></p>';
$msg .= '<p>Thank you for contacting Keita Customer Service. We regret any inconvenience you have experienced. Your request has been received, and a ticket has been created for you with,</p>';
$msg .= '<p><strong>Reference ID : khd004</strong></p>';
$msg .= '<p>Our team is looking into your request and you can expect next to be contacted with either an answer to your question(s) or a solution to the issue(s) raised. Our expected time-frames to respond will vary according to the severity or complexity of the matter being addressed and volume of contacts reaching us. However we assure you that we are working to get back to you as fast as possible and to properly address your questions and concern.</p>';
$msg .= '<p>Regards,</p>';
$msg .= '<p>Keita IT Team</p><br/>';
$msg .= '</body></html>';
$message=$msg;
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Most of the mails are going to junk. However mail sending to gmail are getting in inbox itself.
Your script claims this email is sent from gmail, which is not true
Not clear whether your server signs emails with DKIM or not, does it have SPF or not
Server may be graylisted
Google may not designate your domain as a permitted sender
Any of these reasons may lead to considering your emails as spam.
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.
For some reason, when php mail() is sent to a text number (via email) i.e. 123456789#vtext.com; the from shows up as the centos apache server output email (apache#host-name.com). However, I've included the correct headers; so when the same mail() is pushed to (for example) gmail it comes and shows normal with all the correct headers / mime type / from.
Any idea?
Here's the code for the normal mail
$headers = "From: Alert#thedomain.com \r\n";
$headers .= "Date: ". date('r') . "\r\n";
$headers .= "Content-Type: text/html; charset=utf-8";
$headers .= "MIME-Version: 1.0 ";
$body = "body message";
mail($userinfo->username,"thedomain",$body,$headers);
And the code for the mobile: (We don't use date/content type/ mime for text msgs, or else the headers shows up in the txt msg)
$headers = "From: Alert#thedomain.com \r\n";
$body = "body message";
mail($userinfo->mobile,"thedomain",$body,$headers);
Try sending with the additional -f flag to the mail function:
mail($userinfo->username,"thedomain",$body,$headers, "-ffromaddress#example.com");
See the manual for more information on additional parameters
ok, so I put together a very basic mail function and while testing this, I used a couple of email accounts, one my google account and the other my work account. I get all emails at the google account, but not to those pointing at my work. I'm thinking that could be because they have been caught up with the anti-spam software. Any ideas on how can I develop the mail function to avoid being caught on with spam software?
Here is a copy of my mail function
$to = 'account#gmail.com';
$subject = 'The subject';
$message = 'Hello,'."\n";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers = 'From: me#mycompnay.com' . "\r\n" .
'CC: anotherone#mycompany.com' . "\r\n";
$mail_sent = mail($to, $subject, $message, $headers);
if($mail_sent) {
header("location:newlocation.php");
}
}
A lot of times this has less to do with PHP's mail() function and much more to do with the configuration of your mail transport agent. A lot of mail servers will bounce messages they assume are from spammers (i.e. unconfigured/misconfigured senders) before they even get passed to a spam filter.
If you check your MTA's logs you'll probably find some bounce messages the likes of, "Mail from this server not allowed, see blacklist info at [insert url].
Spam filters use a lot of different methods to determine if the mail coming in is actually spam or not.
Here are a few things that I would suggest:
Descriptive subject line
Descriptive message, careful with HTML and other rich content inside the body as sometimes spam filters will pick up on it as an "advertisement".
Full complete headers with realistic information in there as much as possible.
Try experimenting with different combination's and see if you can get one through to your work. The good thing is that your google account got the e-mail so you know its not an server side issue locally.
you probably need to format your headers and content properly. boundaries are missing.
Here's one simple function with HTML formatting mail:
<?php
function html_mail($i){
$to = $i['to'];
$to_name = $i['to-name'];
$subject = $i['subject'];
$html_message = $i['message'];
$from = $i['from'];
$from_name = $i['from-name'];
$reply_to = $i['reply-to'];
$reply_to_name = $i['reply-to-name'];
if(!$to || !validate::email($to)){return false;}
$email_message = '';
$email_subject = $subject;$email_txt = $html_message;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$email_to = ($to_name ? $to_name.'<'.$to.'>':$to);
$headers = "From: ".($from_name!='' ? $from_name.'<'.$from.'>':$from)."\n";
if($reply_to){
$headers .= "Reply-To: ".($reply_to_name ? $reply_to_name.'<'.$reply_to.'>':$reply_to)."\n";
}
$headers .= "MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n";
$email_message .= "--{$mime_boundary}\n";
$email_message .= "Content-Type: text/html; charset=utf-8\n";
$email_message .= "Content-Transfer-Encoding: 8bit\n\n";
$email_message .= $email_txt;
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}\n";
$email_message .= "Content-Type: text/plain; charset=utf-8\n";
$email_message .= "Content-Transfer-Encoding: 8bit\n\n";
$email_message .= trim(strip_tags(str_replace(array('<br/>','<br />','<br/>'),"\r\n",$email_txt)));
$email_message .= "\n\n";
$email_message .= "--{$mime_boundary}--";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
return $ok;
}
?>
when you have a properly formatted mail, you probably will be able to by pass the filters.
Spam determination is entirely determined by the software that's running the spam heuristics. You would have to look into the anti-spam software that your company uses and see why it's being caught as spam. More often than not it has to do with your mail server set up. A key factor that a lot of software uses is a valid reverse DNS entry, so you could look into that.
You have to realize that if there was an easy way around anti-spam software catching your email as spam just by modifying a few headers, then anti-spam software would be entirely useless, since the spammers would know those methods as well.
Adding a valid 'from' header would be the first thing to do, I think.
and thank you very much for all your suggestions. I found the answer on this post
How to change envelope from address using PHP mail?
It worked.
L.