How to set the from name in the mail() function [duplicate] - php

This question already has answers here:
problem with php mail 'From' header
(8 answers)
What is the format for e-mail headers that display a name rather than the e-mail?
(3 answers)
Closed 8 years ago.
When I send a message using the code below, it sends fine. But if I change the $headers to have 'From: Firstname Lastname' it doesn't send for some reason. How do I add my name to the header instead of the email address?
$subject = 'Test message';
$headers = 'From: mymail#mydomain.com' . "\r\n" .
'Reply-To: mymail#mydomain.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $themessage, $headers);

Some (most, I think) email clients check the From field to make sure the domain is the same as the domain that sent the message. This is to prevent spoofing. Try something like
"From: Firstname Lastname <name#domain.com>"

Related

The e-mail header from the online form is displayed incorrectly

I have a questionnaire form on the web. After filling in this form, I send it to me by email, using the PHP function mail(). The form body and the data it contains, including the private message are displayed correctly on gmail.com. The problem, however, occurs in the header of the email itself. Some characters are displayed incorrectly.
Here is a sample header:
$headers = "Content-Type:text/html; charset=utf-8\r\n";
$headers .= "From:" .$email . "\r\n";
$headers .= "Reply:" . $email . "\r\n";
$headers .= "X-Mailer: PHP/". phpversion() . "\r\n" ;
Required display of email subject:
Nový dotaz -- námět, od Fořt Petr <p.fort1990#gmail.com>
Simultaneous displaying of the subject:
Nový dotaz -- námÄ☒t od: FoÅ☒t Petr <p.fort1990#gmail.com>
The squared times symbol is more like a rectangle.
Is anything wrong? Or where should I look for a mistake?
I'm not sure \r\n works on all platforms
see : Which line break in php mail header, \r\n or \n?
instead
("xxx\r\n\yyy");
use
Header('xxx');
Header('yyy');
or use PHP_EOL, not "\r\n"
Problem solved. My hosting provider uses different character encoding for the headers - I can't explain why, but the following php function will do it all.
function recode_to_utf8 ($text, $encoding = "utf-8")
{
return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}
And now all you have to do is send an email using the mail () method in combination with the method defined above recode_to_utf8(). Like this:
mail(recode_to_utf8($mail_to), recode_to_utf8($subject), recode_to_utf8($message), recode_to_utf8($headers));
I hope it helps others if they have the same problem as me.

php html mail not working on cpanel server [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have a code to send html mail with php on cpanel but doesn't send without any errors. I have tested plain text mail and that is working fine, but once I add the header like below, it doesn't work.
//send html mail
ini_set('sendmail_from', 'info#toludimaokojie.com');
$headers = "From: info#toludimaokojie.com\r\n"."X-Mailer: php";
$headers .= "Content-type: text/html\r\n";
$html = '<html>
<body>
<h2>Result Analysis for Test With Reference Number:'.$reference.'</h2>
<br/>
<p><b>Personality: '.$personality_mail.'</b></p>
<br/>
<p><b>MBQ Score: '.$resultTotal.'</b></p>
<br/>
<ul>'.$analyseData.'</ul>
<pre>Mail Sent on '.date("l, F Y H:i:sa").'</pre>
</body>
</html>';
mail("olaegbesamuel#gmail.com", "MBQ TEST ANALYSIS", $html, $headers);
This doesn't work. Please help, i guess am doing everything correctly here. I have tested without html and confirmed that mail is working
You have defined $headers with all mandatory email parameters but look at your $header variable, there are two things need to be changed,
1) You have used $header and $headers both so use either of them and append them to one variable.
2) your 2nd line of $header variable is missing . to append the previous header values, so the corrected code should be:
$headers = "From: info#toludimaokojie.com\r\n"."X-Mailer: php";
$headers .= "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
As you missed . operator in 2nd line, it will remove From header and so it would not be sending email.
Try after changing these lines.

Gmail IMAP php mail send as reply of email

I want to send an email as reply in gmail through my Laravel CRM system.
My code is as below:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.Auth::user()->f_name.' '.Auth::user()->l_name.'<'.$gmail_address.'>'." \r\n" .
'Reply-To: <'.$gmail_address.'>'. "\r\n" .
'Subject: '.$subject."\r\n".
'To: '.$to."\r\n".
'In-Reply-To: <56F15324.7050704#xxxx.xxx>'. "\r\n" .
'References: <56F15324.7050704#xxxx.xxx>'. "\r\n" .
'X-Mailer: PHP/' . phpversion();
imap_mail ( $to , $subject ,$body,$headers);
But it sends it as a new email, i.e not as a reply.
Ideally it should add "Re: " to subject and append the actual email at the end of reply email body.
Any Help please..
Your In-Reply-To and References headers are hardcoded to some magic value; that's probably not what you want to do.
Here is how I would improve the code:
Use a library which handles the rather low level bits of RFC2047, RFC2231 and especially RFC 5322 for you. It's very likely that any non-ascii characters in your user's l_name produce a non-compliant message. Read the whole RFC5322 to understand how e-mails work. Read about various encodings which come into play.
Track the Message-Id of the message(s) that you're replying to, and set your own In-Reply-To & References headers accordingly.

PHP Mail not working for me [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a mail form with PHP and I have something like this:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$to = 'mymail#gmail.com';
$subject = 'Test mail';
$body = "From: $nombre\n E-Mail: $email";
if ($_POST['submit']){
mail ($to, $subject, $body);
header("Location: http://www.example.com/");
}
?>
As you can see the code is really simple, it's just sending a name and an adress but it doesn't work. Whenever I try to send it, the page just change the title to "mypage/send.php" and that's all.
It does not even redirect me to an example page.
I've tried:
Using headers (as recommended by others from here).
Checking if there's a grammatical error.
Testing it in a real page and in localhost with SMPT.
EDIT: HTML USED:
<form method="post" action="send.php">
<input type="text" name="name" placeholder="Your name">
<input type="email" name="email" placeholder="Email">
<input type="submit" value="send">
</form>
Firstly, you don't have an assigned variable called $nombre so having error reporting set would have thrown an undefined variable warning; you may have meant to use $name instead.
Check your Spam since you don't have a proper From: in your header, and/or the Email is being rejected altogether due to that reason, this is a common thing nowadays.
Check that all your form elements are indeed named, this includes your submit button, since your conditional statement depends on this, and you haven't provided your HTML form.
I.e.: name="name" and name="email" and for the submit button name="submit".
It's also better to use isset(). I.e.:
if(isset($_POST['submit'])){...}
instead of if ($_POST['submit'])
For more information on mail/headers, visit:
http://php.net/manual/en/function.mail.php
Example from PHP.net:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Add error reporting to the top of your file(s) which will help find errors either.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
For more information on mail/headers, visit:
http://php.net/manual/en/function.mail.php
Example from PHP.net:
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
A few things to note are:
Is PHP installed on your server and properly configured?
Is mail installed and properly configured?
If running from your own computer, all of the above apply.
"it's just sending a name and an adress but it doesn't work"
You will need to be more precise with this. "It doesn't work" doesn't define nor describe how it's not working, or any error messages you may have gotten.
If it isn't redirecting, then you may be outputting before header, another common mistake, which is something that error reporting will catch, do use it.
Here is an article on Stack about outputting before header (headers already sent), should you get that warning:
How to fix "Headers already sent" error in PHP
You should check the value of $_POST["submit"] with var_dump and the return value from mail(which is boolean).
Obs.: It is a good pratice to filter all input coming from $_GET and $_POST.

Best way to send emails generated by visitors using website get quota form

I'm struggling with this issue for months now.
I have a simple website wich has a "Get price quota" php form that interested visitors can use to ask for prices.
The PHP form sends an email to the admin using PHP's "mail".
Everything works fine with Google, Hotmail, etc EXCEPT, I get my email rejected all the time from Yahoo recipients. Every two or three months I have to get my dedicated IP changed and the hosting company to make unblacklisting requests because emails sent back to the customers bu the admin using email accounts on the same server\domain are rejected. Weird or not, not all of them are rejected.
I went through all info and tips I could find about securing my form, checked the logs to see if somehow my domain\website is used for spam, I have DKIM enabled, etc, nothing works, I know Yahoo has bad problems but I wonder if there is another way to send emails from forms, small amounts, max 100 per month.
I'm going to post my php email script below, please tell me if there is something wrong that could cause Yahoo to blacklist my domain or is it just Yahoo.
//Secure The Submitted Data
$quoteFirstName = stripslashes($_POST["quoteFirstName"]);
$quoteLastName = stripslashes($_POST["quoteLastName"]);
$quoteEmail = stripslashes($_POST["quoteEmail"]);
$quoteDetails = stripslashes($_POST["quoteDetails"]);
$quoteProduct = stripslashes($_POST["quoteProduct"]);
$quoteTel = stripslashes($_POST["quoteTel"]);
$mailtolink = '' . $quoteEmail .'';
//Build The Email To The Admin
$to = "orders#mydomain.com";
$subject = "Price quota request";
$theEmail = "$quoteFirstName $quoteLastName <br /> $mailtolink <br /> Telephone $quoteTel <br /> Price quota for <br /> - $quoteProduct.<br /> Additional info: $quoteDetails";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: " . $quoteEmail . "\r\n" . "Reply-To: " . $quoteEmail . "\r\n" . "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $theEmail, $headers);

Categories