Mail() Function unexpectantly sending to unspecified/called/passed emails - php

It's very strange and I don't know why, but when I pass my '$to' string to the mail() function, for some unknown reason, It sends to all users in the database, instead of just those being passed into the $to string.
EXAMPLE: I pass the following it.
'User1 , Mod1 '
Result: On the positive side, all those listed receive emails as intended. But so does everyone else which makes me feel like i'm spamming some folks unintentionally with notices they don't want/didn't ask for/have nothing to do with them.
'User1 <user1#example.com>, User2 <user2#example.com>, User3 <user3#example.com>, User4 <user4#example.com>, User5 <user5#example.com>, Mod5 <mod5#example.com>'
I have been through my script several times, and I have var_dump() and xDebugged the $to variable just before and after 'Mail()' is called, and the string shows only the people I expect for the mail function to send to, yet it still sends too all users. I am using php 5.6 currently on DreamHost. It works in my local host (Ampps), but when i go live, its very unexpected results in which everyone is, as previously mentioned, received the email notifications.
This is how I set up the variables to send via mail()
// To send HTML mail, the Content-type header must be set
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Additional headers
$headers[] = 'To: ' . $to;
$headers[] = 'From: myDomain - ' . $ttl . ' <noreply#myDomain.com>';
$headers[] = 'Cc: ' . MOD_EMAILS;
$headers[] = 'Bcc: xxxxxxx#gmail.com';
//var_dumped() //i var_dump here, string was as expected
// Mail it
mail($to, $subject, $message, implode("\r\n", $headers));
//var_dumped() //var_dump here, string was as expected
Does anyone have/have had an experience like this? Or an idea of how to resolve it? I'm a newbie and trying to build my own CMS as a means of teaching myself to write PHP and stuff.
Thank you.

I solved the issue, which had as I thought nothing to do with he database or any of the sql calls. Rather, it had to do with the fact that I had the $to variable both in the $headers and then also in the actual mail() function call, along with the $variable. By commenting out the $headers[] = $to, the issue was solved.

Related

Subject issue with mail function - PHP

I'm trying to send e-mail using PHP. I took a piece of code that I have been using earlier, but it doesn't work anymore. I figure out the problem was coming from the subject.
When my subject is "test", the mail is sent, but when my subject is anything else with for example capital letters, like "Test", it doesn't work. I'm pretty sure this is a quick fix, but I can't find the answer.
Here is my code.
In index.php
$subject= 'test';
$message = implode("\n" , $_SESSION['data']);
mail_html($subject, $message, $email);
In functions.php
function mail_html ( string $subject, string $message, string $receiver) {
$headers = 'From: Template <me#me.com>' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8';
mail($receiver, $subject, $message, $headers);
}
Thank you everyone for helping me. I feel stupid getting blocked by things like that.
Sending E-Mails is a very difficult process. So you can't send E-Mails so easy from your Computer or Server if you don't have alle the common SPAM prevention mechanisms enabled.
So when you don't a Reverse-DNS entry and you send an Email with "Test" you should not wonder if the emails can't send to the other site. So Take a look at the SPF-System, Reverse-DNS and generic names for servers (not working for some providers). Then you can send some E-Mails.
To find some problems check your mail log /var/log/mail.log.
When you try to prevent that problems use an SMTP account from your provider and send that E-Mails over SMTP.

I am using php script to mail . inside mail gmail gives warning-- this type of mail is used to steal data or suspicious link

gmail gives this msg --Be careful with this message. It contains content that's typically used to steal personal information.and suspicious link warning when clicking on link in my mail.
I think the To header is also required in the mail function of PHP.
$headers[] = 'To: Name <'.$user_email.'>';
$headers[] = 'From: Your Website <'.$email#yourdomain.com.'>';
mail($user_email, 'Subject', $message, implode("\r\n", $headers));
https://www.tinywebhut.com/suspicious-link-message-in-gmail-50

Mailer Error: Language string failed to load: instantiate - PHPMailer

I am working on sending some emails and PHPMailer have been working perfectly on my test server.
Now, after i moved it to the production server, none of my mails will fire and i am getting a
Mailer Error: Language string failed to load: instantiate.
I have been googling and toiling with phpmailer to see what seems to be the problem and from what i gathered, i was not able to make a regular php mail() request (this is the var that needs to return true for the error to stop
$rt = #mail($to, $this->EncodeHeader($this->Subject), $body,
$header, $params);
But even when setting subject and body to one word and the headers to the simplest kind
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
The mail function still returns false and i am really running out of ideas at this point
Anyone have anything to help me out with? It is getting extremly frustration at this point.
Thanks in advance
I think you are sending a subject header encoded as UTF-8, which the PHPMailer class would reject since it contained a perculiar swedish letter, namely “å”.
Something like that:
$subject = utf8_decode("Någon har svarat på din fråga på $site");
This converts the string to ISO-8859-1 encoding, which, of course, worked fine.

Handling mail failures using PHP?

If I send an email through GMail to this address nkhkhlkhlkjlkjkljlkjlk#gmail.com
I got error like:
Delivery to the following recipient failed permanently
My question is, if I send using the PHP mail function, how can I catch bounce emails?
My code:
// 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: 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";
$to = "nkhkhlkhlkjlkjkljlkjlk#gmail.com";
$subject = "Testing";
$message = "Testing body";
mail($to, $subject, $message, $headers);
You can't detect such errors just by using mail(). You have to specify a valid return address and then connect to that mailbox (via IMAP or POP, there are functions for that) and check if any message bounced. Bear in mind that bounces may take a long time (even hours, due to transient errors) so you have to do this check asynchronously.
you could get the message by mail when you use this:
$bounce = 'test#domian.com';
mail($to, $subject, $message, $headers,"-f $bounce");
This will send all bounce back mails to your address
You could use PEAR MAil, it contains a parseAddressList() method which checks if the server of an email address accepts the email address. It also returns useful error messages.
The PHP mail() function does have an error status as the function return value, but it won't pick up this kind of error because it only checks that the email has been sent, not that it has been received.
(this is because it needs to return control back to the program immediately; it can't wait for a possible bounce message because they can take a long time to come back - you wouldn't want your program to sit and wait for five days just in case the email bounced, would you?)
Therefore, the only way to determine whether a message has bounced is to have a separate program which checks the mailbox which the bounces would be sent to.
You can specify the mailbox for bounces using the -f option in the mail options string (see the PHP mail() function man page for more info on this).
Then have a separate program periodically check that mailbox for bounce messages, and report them to you as required. (there are a number of PHP libraries that allow you to check a mailbox; Google will help here, or look in PEAR)
Determining which email it was will depend on the quality of the bounce message. You should definitely be able to see the intended recipient's email address, but you may not be able to tell which email it was, ie to match it up to a specific instance of when you called the mail() function in the first place.
Hope that helps.
You should login via IMAP or POP3 to your inbox and parse the email address and delivery error.
It's also worth mentioning that you can specify (while sending) custom headers to better track the message source, I believe MailChimp and others do this do track campaigns, etc...
PHP does not know anything about the mail after it got out of the mail function, so no way to do it at the same time as you send the mail.

Required Mail Headers

I have a website in which I send a confirmation mail as part of the registration process.
Some time ago, I had some troubles with the mails I sent since I used no headers (PHP mail function).
Once I put some headers, I've gotten more responses from users, but I suspect that not every message reaches its destination.
How can I be sure that the messages reach their destination?
Which are the headers that can be considered a 'must'?
This is the code of my SendMail function
mail($to,
$subject,
$message,
"MIME-Version: 1.0\n".
"Content-type: text/plain; charset=ISO-8859-1; format=flowder\n".
"Content-Transfer-Encoding: 8bit\n".
"Message-Id: <" . md5(uniqid(microtime())) . "#mysite.com>\n".
"Return-Path: <admin#mysite.com>\n".
"X-Mailer: PHP v".phpversion()."\n".
"From: admin# mysite.com");
You should use external library for working with e-mails in php like PhpMailer , SwiftMailer or Zend_Mail. All your problems will go away.
The headers need a white space at the bottom to separate the header from main body.
Tools like Spam Assassin will give you a big mark down for that.
Also you should use \r\n as a line terminator instead of just \n
From PHP.net
Multiple extra headers should be separated with a CRLF (\r\n).
The headers seems quite good to me. The only glitch I see is an extra whitespace in the From header.
I'm sure you already checked it, but just in case ...
"From: admin# mysite.com");
should be (?)
"From: admin#mysite.com");
This is a working mail function I'm using for html mail and variable $return is defined to get error report from mail server in case of fail delivery.
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: <'.$from.'>' . "\r\n";
$return = '-f'.$from;
#mail($to, $subject, $msg, $headers, $return);
you can see more detail at here sugunan.com
The headers look ok, except for the details pointed by #Eineki. Also if you are using Windows you need to send the $to param in the form "user#mail.com" and not "Username ", because it may cause trouble, due to the way the mail() function is implemented on windows platform, the "to" address may be parsed incorrectly.
You should add a Date: header (its mandatory by RFC5322) and some mail-clients may assume January 1 1970 as an e-mail date if none is given (and it gets lost between all the other old messages).

Categories