php email doesn't send from specified From: address - php

When I run this script to send an email, the email is being sent from serveradmin#myhosting. They say that the script isn't configured correctly.
I've changed the domain names and info shown below before posting here.
Is there really a problem with the scrip? The email sends, everything is fine.
It takes a bit long...don't know why, the only problem is it doesn't send from the domain I specified.
$to = $_POST['fes-email'];
$subject = 'TEST';
$body = 'TEST TEST';
$headers = 'From: NAME \(Info\) <name#domain.tld>' . "\r\n" .
'Reply-To: name#domain.tld' . "\r\n" .
'Return-Path: name#domain.tld' . "\r\n" .
'X-Priority: 1' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
mail($to, $subject, $body, $headers);

Try using quotes instead of escaping the parentheses:
$headers = 'From: "NAME (Info)" <name#domain.tld>' . "\r\n" .
You should remove the Return-path: header. This header is always recreated by the receiving server, using the envelope information sent via SMTP.

Have you tried putting just <name#domain.tld> in the headers for From?

Related

PHP: Some parts of Token in URL will be replaced by other Chars in generated HTML eMail

im developing a Login-System and one Step of it is, that a Token will be generated in PHP, stored in DB and placed in a eMail with a Login-Link. At my local xampp windows machine its working perfectly. If i try it on my Webserver, i get a issue:
Random parts of my Login-Link will be replaced by other Chars. I observed, that its only appears in Token-Part of the Login-Link. So i think there is some issue with the charset maybe?
Code Part for generating Token and send it by eMail:
<?php
// Create Token
$user_logintoken = bin2hex(openssl_random_pseudo_bytes(16));
// Create eMail Content
$to= "user#email.com;
$subject = "Login Link";
$loginurl = "https://www.website.com/index.php?page=login&token=".$user_logintoken;
$message= "Hi User \r\n".
"to Login, use this Link:\r\n
".$loginurl."\r\n";
$header = 'From: admin#email.com' . "\r\n" .
'Reply-To: admin#email.com' . "\r\n" .
'Mime-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=utf-8' . "\r\n" .
'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
'X-Mailer: PHP ' . phpversion();
// Send eMail
mail($to, $subject, $message, $header);
?>
So for example:
$user_logintoken = "e25790f817421cee485074856df12ca4"
In the HTML eMail i got:
https://www.website.com/index.php?page=login&token�5790f817421cee485074856df12ca4
In HTML eMail Source Code, everythink is fine:
https://www.website.com/index.php?page=login&token=e25790f817421cee485074856df12ca4
So i think there is an issue with encoding... i already tried htmlentities($loginurl), urlencode($loginurl) and rawurlencode($loginurl) but its not fixing my problem.
Can anyone please help me ? :-)

Unable to send emails while i pass headers to mail() function

I am trying to send emails with HTML content and whenever i pass headers of any kind the method mail() return false but with no headers i get true .. any clue ?
$to = $user->email;
$name = $user->first_name.' '.$user->last_name;
$from = $this->config->item('admin_email', 'ion_auth');
$subject = $this->config->item('site_title', 'ion_auth') . ' - ' . $this->lang->line('email_forgotten_password_subject');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: ' . $name . ' <' . $from .'>' . " \r\n" .
'Reply-To: '. $from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$retval = mail ($to,$subject,$message,$headers);
die(var_dump($retval));
Make sure that variables $name, $from, $to, $subject has the right data and are not empty/null. Your code is functional and should send the email.
If still doesn't work, check the configurations on postfix or what other mail server you're using.
And I suggest to use PhpMailer or SwiftMailer instead of simple mail function.
Try adding a CRLF after your last header. Used to work for me in the olden days.
i.e.,
...
$headers .= 'From: ' . $name . ' <' . $from .'>' . " \r\n" .
'Reply-To: '. $from . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n";
...
Also, the header Content-Type has a capital T. Sometimes, such trivial things may also break it. That's why everyone suggests to move away from it.
That should do it.
Suggestion: Switch to PHPMailer.

PHP mail script goes to junk mail when using an auto generated URL

Here is the PHP code I am using to send my email. For some reason this will go to junk mail if I include the following line: $message .= WEBSITE_URL . '/violation.php?email=' . urlencode($fm_email) . "&key=$activation"; but if I remove this it will come to my inbox.
Can anyone help me solve this issue to allow this url in my email?
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: My Name <noreply#myname.com>' . "\r\n";
$subject = 'Subject is here';
$message = "Here is the message";
$message .= WEBSITE_URL . '/violation.php?email=' . urlencode($fm_email) . "&key=$activation";
mail($fm_email, $subject, $message, $headers);
Dont use mail() function of php it will send your mail to junk only. Instead use SMTP php mailer function.
Why we should use SMTP instead PHP mail():
SMTP log in to an actual account on a mailserver and send the mail through SMTP to another mail server. If the mail server is configured correctly, your mails are sent from an actual account on a mailserver and will not wind up flagged as spam.
Mail sent with the mail() function is sent with sendmail in most cases. There is no authentication going on and it will almost always be flagged as spam if you use the "From:" in the extra headers.
This is because if you take a look at an original email file in say, gmail, you will see the headers that are sent. You are actually sending from user#serverhostname.tld and not someone#example.com like you had told the mail function to do.
If you use SMTP and view the original the email is actually sent from someone#example.com
You can download SMTP class from:
https://code.google.com/a/apache-extras.org/p/phpmailer/source/browse/trunk/class.smtp.php?r=170
http://www.phpclasses.org/package/14-PHP-Sends-e-mail-messages-via-SMTP-protocol.html
Using the following headers should prevent your email from getting in the junk/spam folder.
$headers = 'From: ' . $website_name . ' <' . $own_emailaddress . '>' . PHP_EOL;
$headers .= 'Reply-To: ' . $name_sender . ' <' . $email_sender . '>' . PHP_EOL;
$headers .= 'Return-Path: Mail-Error <' . $error_emailaddress . '>' . PHP_EOL;
$headers .= 'X-Mailer: PHP/' . phpversion() . PHP_EOL;
$headers .= 'X-Priority: Normal' . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-type: text/html; charset=iso-8859-1' . PHP_EOL;
Don't forget to define the variables in the code.

PHP - Not Sending Emails with Header Information

I am having a problem sending emails when I add header information.
However when I just remove the header parameter it works. What is wrong? Is it the code? Or some setting I need to change on the web server admin panel to say "Allow-Headers" or something?
I am trying to send to hotmail in case this has any relavance in determining the problem.
Any help would be greatly appreciated. Thanks.
Below Doesn't Send Email:
<?php
$to = 'iputmyrealemailhere#hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com';
mail($to, $subject, $message, $headers);
?>
Below Sends Email:
<?php
$to = 'iputmyrealemailhere#hotmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com';
mail($to, $subject, $message);
?>
I use these headers in my php mailing function and it works well. Note: I also use a third party mail-routing service to avoid having my mails marked as coming from a spammy IP. You might want to look into that also.
$headers = 'From: '.$from.'#foo.net' . "\r\n" .
'Reply-To: '.$from.'#foo.net' . "\r\n" .
'X-Mailer: PHP/' . phpversion() . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n";
I also use the optional fifth parameter to mail() to set the envelope address, e.g.:
$parameters = '-f '.$from.'#foo.net';
so the final call is:
mail($to, $subject, $message, $headers, $parameters);
You can just delete the "FROM:" from the headers list .. it prevents it in some hosts .But the real question then will be how ca I change the sent from email address to a specific email that I want

PHP Mailing Code sending mail to spam/quarantine

The title explains itself. It is a website for in-house employees to buy and sell from each other. Its based solely around Microsoft Outlook emailing addresses. All the emails are supposed to be sent from the seller's email as they post items. Except when I enter <php phpinfo(); ?> on the action php page it tells me that the sendmail_from attribute thing is sending from a bogus email on the server. It seems to be the automatic email for the php script to send from. This may be why the emails are getting sent to spam, because the email is not valid. Also, I read online about having full and valid headers but most headers seem optional and i cant find anywhere that explains optimal headers. My mailing code:
//send approval email to the approver
$from = isset($_POST['from'])? $_POST['from']:1;
$message = isset($_POST['message'])? $_POST['message']:1;
$message = $message . '<a href="http://dev-corkboard/newapproval.php?id='
.$result[0][0].'"> Click here to approve website post.</a>';
// In case any of our lines are larger than 70 characters, we should use
// wordwrap()
$message = wordwrap($message, 70);
$to = 'clehane#eatonvance.com';
$replyto = isset($_POST['replyto'])? $_POST['replyto']:1;
$subject = isset($_POST['subject'])? $_POST['subject']:1;
$headers = "MIME-Version: 1.0" . "\r\n" . 'From: "'.$from.'"' . "\r\n" .
'Reply-To: "'.$replyto.'"' . "\r\n" .
'Content-Type:text/html;charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
//test message for email
}
header ("location: newindex.php"); `
Any ideas?
And bam! Solved it, needed to put email addresses as such:
$from = 'MyName <myemail#mycompany.com>';
And I also included these headers:
"X-Priority: 0\r\n".
"X-MSMail-Priority: Normal\r\n".
"X-Mailer: mycompany.com

Categories