PHP Mail sending to external e-mail addresses but not internal - php

Started off with e-mails not being sent at all, but then I used the "-f" parameter in the function, which then works to send to external addresses (Gmail and Hotmail tested so far) but it won't work for addresses that are on the domain though. Just wondering if it's in the code or is it a problem with the server setup?
if ($Valid == 1) {
$_POST = array_map('strip_tags', $_POST);
$_POST = array_map('stripslashes', $_POST);
$To = "user#domain.ca";
$Subject = "Online Driver Application";
$Body = "All the values of the form that was filled out (removed because there was a lot and it doesn't affect the problem)";
$Headers = 'MIME-Version: 1.0' . "\r\n";
$Headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$Headers .= 'From: Company <info#domain.ca>' . "\r\n";
$Headers .= 'Reply-To: no-reply#domain.ca' . "\r\n";
$Headers .= 'X-Mailer: PHP/' . phpversion();
mail($To, $Subject, $Body, $Headers, -finfo#domain.ca);
echo '<div class="success">Thank You! Your form has been successfully submitted.</div>';
} else {
if ($ErrorMsg != '') {
echo '<div class="error">'.$ErrorMsg.'</div>';
}
Again, unless I have the -finfo#domain.ca in the mail function, e-mails don't get sent out at all.
Thanks.

The additional_parameters parameter (-f or -r) can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.
The additional_parameters(> 4.2.2) is disabled in safe_mode and the mail() function will expose a warning message and return FALSE when used.
The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.
(or, remove sendmail and install postfix (much easier) if still in problem.)

The code seems OK. This is probably something not setup correctly (like PHP.ini not having the SMTP server or a Firewall blocking something). Also, make sure newlines are "\n" in $Body I've seen some mail servers rejecting mails because of this. Using mail directly is always tricky. Try to use a library like Swiftmailer or PHPMailer instead. This way you don't have to worry about how to send HTML, attachments...

Related

how do i add From in your php email [duplicate]

I'm building a website that sends and email to a user when he registers.
My code (the gist of it):
<?php
$to = "helloworld#gmail.com";
$subject = "Test mail";
$message = "Hello! \nThis is a simple email message.";
$headers = "From: munged#gmail.com";
$headers .= "\r\nReply-To: munged#gmail.com";
$headers .= "\r\nX-Mailer: PHP/".phpversion();
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
the problem is that when the mail is delivered, the from header remains munged#box123.bluehost.com, while reply-to gets changed to the specified value.
box123.bluehost.com is the hostname of the server on which the website is hosted.
So what am I doing wrong? What can I do to get the "From" address the same as the reply-to address?
Is it something I'm doing wrong, or is the web host playing foul?
Edit: I just noted that you are trying to use a gmail address as the from value. This is not going to work, and the ISP is right in overwriting it. If you want to redirect the replies to your outgoing messages, use reply-to.
A workaround for valid addresses that works with many ISPs:
try adding a fifth parameter to your mail() command:
mail($to,$subject,$message,$headers,"-f your#email.here");
It turns out the original poster's server (blueHost) has a FAQ concerning this very question.
Article 206.
This is because our servers require you (or your script) to use a properly formatted, valid From: field in the email's header. If the From: field is not formatted correctly, empty or the email address does not exist in the cPanel, the From: address will be changed to username#box###.bluehost.com.
You must change the script you are using to correctly use a valid From: header.
Examples of headers that should work would be:
From: user#domain.com
From: "user" <user#domain.com>
Examples of headers that will NOT work:
From: "user#domain.com"
From: user # domain.com
From: user#domain.com <user#domain.com>
Our servers will not accept the name for the email address and the email address to be the same. It will not accept a double declaration of the email address.
For scripts such as Joomla and Wordpress, you will need to follow their documentation for formatting the from fields properly. Wordpress will require the Mail From plugin.
Note: The email address you use must be a valid created account in the
cPanel.
I had the same Issue, I checked the php.net site. And found the right format.
This is my updated code.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $fromName . ' <' . $fromEmail .'>' . " \r\n" .
'Reply-To: '. $fromEmail . "\r\n" .
'X-Mailer: PHP/' . phpversion();
The \r\n should be in double quotes(") itself, the single quotes(') will not work.
In order to prevent phishing, some mail servers prevent the From from being rewritten.
I realize this is an old thread, but i had the same problem since i moved to bluehost yesterday. It may not have been the selected answer but i support the bluehost article 206 reply.
I created a valid email in control panel and used it as my From address and it worked.
I solved this by adding email accounts in Cpanel and also adding that same email to the header from field like this
$header = 'From: XXXXXXXX <test#test.org>' . "\r\n";
The web host is not really playing foul. It's not strictly according to the rules - but compared with some some of the amazing inventions intended to prevent spam, its not a particularly bad one.
If you really do want to send mail from '#gmail.com' why not just use the gmail SMTP service? If you can't reconfigure the server where PHP is running, then there are lots of email wrapper tools out there which allow you to specify a custom SMTP relay phpmailer springs to mind.
C.
headers were not working for me on my shared hosting, reason was i was using my hotmail email address in header.
i created a email on my cpanel and i set that same email in the header yeah it worked like a charm!
$header = 'From: ShopFive <site#mysite.org>' . "\r\n";

PHP mail() not sending

I have a WebFaction server and the following code:
$to = "outreach#bmun.org";
$reply_to = "From: " . $_POST['email'];
$name = $_POST['name'];
$subject = "Outreach Request Session for " . $_POST['school'] . " on " . $_POST['date'];
$em = $_POST['message'] . "\n-" . $name;
$sentmail = mail($to, $subject, $em, $reply_to);
$sentmail returns true, but the email is not sending for some reason.
$sentmail returns true, but the email is not sending for some reason.
The mail function in PHP simply sends the mail via an MTA (mail transfer agent) on the server. A true can just mean the local MTA accepted it. But that is not all you need.
First, does your hosting provider actually allow outgoing mail? Or are messages sent to a virtual “black hole?”
Now, let’s assume that your local MTA—most likely sendmail—works, and the mail jumped off of the server & made it into the real world. Okay, great!
But not so fast…
The thing is just because you send a mail, it doesn’t mean that the receiving end thinks the mail is valid. And chances are the receiving end has decided a random e-mail sent off of a random server is simply SPAM.
I have posted a more detailed answer here, but when it comes to SPAM it basically boils down to this: Do you have an SPF (Sender Policy Framework) record setup for your domain? Do you also have a PTR (reverse DNS) record set for that domain?
If you do not have an SPF or PTR record, the chance of your message simply being flagged as SPAM is quite high.
If you are serious about sending mails off of your server, you need to at least get your SPF record & PTR record set.
You don't have any headers in your email.
Although they don't seem to be required, your emails will be blocked by spamcheckers a whole lot sooner.
This is an example:
<?php
$headers = "MIME-Version: 1.0\n" ;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$headers .= "X-Priority: 1 (Highest)\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "Importance: High\n";
$status = mail($to, $subject, $message,$headers);
?>

How to format an email that Hotmail / Outlook is happy with?

$body = 'This is a test';
$subject = 'Confirmation';
$headers = 'From: Testing Site' . "\r\n";
$headers .= 'Reply-To: admin#myserver.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n";
$headers .= 'Delivery-Date: ' . date("r") . "\r\n";
//$headers .= 'Message-Id: <20140316055950.DA8ED58A13CE#myserver.com>' . "\r\n";
mail("example#hotmail.com", $subject, $body, $headers, "-f admin#myserver.com");
mail("example#gmail.com", $subject, $body, $headers, "-f admin#myserver.com");
Emails send fine to Gmail but are always rejected by Hotmail with this error:
host mx1.hotmail.com[65.55.33.119] said: 550 5.7.0 (COL0-MC5-F28)
Message could not be delivered. Please ensure the message is RFC 5322
compliant. (in reply to end of DATA command).
Message ID header is generated automatically by the server but it doesn't help to supply one manually either.
Why isn't Hotmail happy?
Mail server has SPF record, reverse DNS, is not blacklisted and passes all checks at mxtoolbox.com.
The From header is invalid. It must have the following syntax:
From: "name" <email-address>
In your case:
From: "Testing Site" <admin#myserver.com>
The same goes for your Reply-To header:
Reply-To: "Testing Site" <admin#myserver.com>
Which you can omit if it's the same as the From header (like in your case).
PS: RFC 2822 doesn't state that the display-name in an address should be quoted. In other words: the following 3 headers should all work:
From: "Testing Site" <admin#myserver.com>
From: 'Testing Site' <admin#myserver.com>
From: Testing Site <admin#myserver.com>
If you're using WordPress, you can look up plugin for Hotmail/Outlook friendly emailing capability.
However if it is a standalone script you might wanna look into Microsoft's official answer to this query on the URL : http://answers.microsoft.com/en-us/outlook_com/forum/oemail-osend/why-are-the-emails-sent-to-microsoft-account/b64e3e4a-0d93-40c8-8e28-4be849012f9c
In-short Email-Server provider has to fill this form (once) : https://support.live.com/eform.aspx?productKey=edfsmsbl3&ct=eformts&wa=wsignin1.0&scrx=1
In order to get their emails accepted by Hotmail/Outlook.
Using the PHPMailer library to send mail instead of the mail() function has finally sorted this problem and is the working solution for me. Answer by Jasper N. Brouwer probably more correctly answers the question though I've not had a chance to try it.
1 ) Go to SPF record wizard
2) create a new SPF record for your DNS domain
3) Add that DNS record to your domain's DNS
4) if you fail somewhere in the process, read the detailed SPF record specification
After you complete this process HOTMAIL will be happy with your email.

Mail() not working, any ideas why?

Hi guys i am using the mail() from the contact form and for some reason it is not working.
The php coding i have setup is as follows:
// sending email to sales#xxx.com
$to = "hello#xxx.com";
$subject = 'Email From {$name} from your website';
$message = "$name has contacted you from the website and says:
$mcontent
$name 's contact email address is: $email";
$headers = $email . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($to, $subject, $message, $headers) or die ("Failure");
// end of sending mail
returnheader("/xxx/email-sent");
i get no errors at all, it even goes to the success page when completed.
Any ideas why this would not work.
I have checked your code and it is working fine on my server , i am getting e-mail.
Here , it looks like some problem with your SMTP server settings.
There is nothing wrong with your PHP script.
You can find your solution here.
php.ini & SMTP= - how do you pass username & password
Also in windows environment ,
http://www.ruhanirabin.com/php-sendmail-setup-with-smtp-iis-and-windows-servers/
If you are on WIndows, make sure you have an SMTP server in your
php.ini
If you are una Unix, make sure the MTA is running: If it is (at least partly) installed but not runnng, you will get exactly this effect
Edit
If your MTA is not running, and you start it, the mails sent with PHP will go out! They have beein queued, but not processed.
This is probably related to the mail setup. Do you have a mailserver running on the machine? Check sendmail / smtp_server settings in php.ini.
mail() uses the sendmail, by default: sendmail -t -i
It returns TRUE if the mail has been accepted, not if it has been sent:
Return Values
Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.
I would suggest using http://swiftmailer.org/ with SMTP rather than mail().
The first line of $headers is invalid if $email is just an email address. Presumably you would want something like:
$headers = "Cc: " . $email . "\r\n";
or
$headers = "From: " . $email . "\r\n";
The cause can be for example, you don't have setup a mail server, or configuration of firewall if you has it.
You should use double quotes instead of single quotes in the variable $subject
$subject = "Email From {$name} from your website";

how to know if php mail failed

I am sending mails from php mail() : and I want to receive a failed message if sending is failed to the destinatio .
$to = 'itsdfdsf#7sisters.in';
$email_from = "info#7sisters.in";
$full_name = 'XXXX';
$from_mail = $full_name.'<'.$email_from.'>';
$subject = "testing sender name";
$message = "";
$message .= '
<p><strong>This is only a test mail. Please do not reply.</strong><br />
';
$from = $from_mail;
//$headers = "" .
// "Reply-To:" . $from . "\r\n" .
// "X-Mailer: PHP/" . phpversion();
$headers = "From:" . $from_mail . "\r\n" .
"Reply-To:" . $from_mail . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
if(!mail($to,$subject,$message,$headers))
{
echo 'failed !!';
}
But although $to mail does no exist,it is not showing failed !!
The mail method is just sending the mail out. If it does not receive any errors (e.g. by not finding the server etc), it will return succesfull. You will not be able to know if the mail actually landed in the inbox of the recipient unless you create some code around bounced emails etc.
I think what you want is to check for a real email not only a valid formatted email. So I would suggest you to have a look at this blog
check the return from of mail
Returns TRUE if the mail was successfully accepted for delivery, FALSE
otherwise.
It is important to note that just because the mail was accepted for
delivery, it does NOT mean the mail will actually reach the intended
destination.
Although the fact it is returning true probably means that your mail program is accepting the message but then failing when it tries to send to no one...
You should run the $to through a validator to check its a valid address and then throw an error if its not, don't rely on mail() to filter out things which you already know are wrong, or can check against easily.
--UPDATE
Then check out #SeRPRo , but what your trying to do is hard work to test programatically - its far easier and more reliable to send an e-mail which requires the user to click a link to verify that it's real than try querying SMTP servers which all have different behaviour (read: are broken to different degrees). Also note that your intended behaviour (code wise) is hard to differentiate from a spammers so don't be surprised to find it difficult going if you avoid the verification e-mail route.
But although $to mail does no exist,it is not showing failed !!
actually the fact that mail is being delivered to SMTP server, doesn't mean it will be delivered to the end user. There's no easy way in PHP to check whether it's delivered.
You could CC yourself as a way of testing that it is leaving the outbox.
In my case it helped to set the return-path via the commandline parameter "-f", which can be passed in the $additional_parameters parameter of mail(). so i call
mail($to, $subject, $message, $headers, "-f address.where.i.want.the.bounces#xy.com");
... according to some comments on http://www.php.net/manual/en/function.mail.php hosting-inviroments react different and have different restrictions (address might need to be registered in the same hosting-account, or be on the same domain, the same as the "From:" in the heade ... and so on)
The page where I got the bounces to be received (with non of the mentioned restrictions, as it seems) is hosted at Domainfactory http://www.df.eu
Use phpmailer to send email and set $mail->AddCustomHeader('Return-path:bounce#mail.com');
This will send bounce email at bounce#mail.com if recipient mail id does not exist or recipient does not receive email by any other case.

Categories