Add space between two variables in the phpmailer headers - php

I'm sending a form mail with php.
I need to set the "from:" to the form fields which live in the variables:
$user_name and $user_lastname.
So i tried:
$headers .= 'From: ' . $user_name . " " . $user_lastname . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
But then i get "unknown sender" instead of name and last name.

try this:
$headers .= 'From: "'.$user_name." ".$user_lastname.'"<'.$user_email.'>'."\r\n";

I believe you're simply using the wrong (").
Try this:
$headers .= 'From: '. $user_name .' '. $user_lastname;
$headers .= 'X-Mailer: PHP/' . phpversion();
Let me know if it isn't working

Related

Send Full HTML Mail with PHP

I would like to send an Email with PHP mail function but my HTML is being sent as plain text.
This is my code:
$from = 'admin#bla.com';
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: text/html; charset=UTF-8' . "\r\n";
$message = file_get_contents('cancelemail.php'); //Includes my HTML Page
thanks!
your headers should be array like:
$headers = array("Content-Type: text/html; charset=UTF-8", "From: $from ");
Also, there is no point setting content as php file, content inside will be read as html, the php code will not be executed;

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.

HTML Email is seen as text email despite setting headers

I've looked at the documentation and here's how I'm sending my email:
$headers = 'From: aaaaaa#aaa.com' . "\r\n" .
'Reply-To: aaaa#aaa.com' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send
mail('aaaaa'aaaa.com', 'Contact: ' . $name . ' from ' .$company, $message, $header);
Where $message is a string containing several <br /> tags for line breaks.
I've set the Content-type to text/html so I would expect the email to arrive as HTML, but in my inbox it's clearly interpreted as just text (IE: I just see the text of my <br /> tags)
It's this
$headers = 'From: email#example.com' . "\r\n" .
^ - notice the s?
$company, $message, $header )
^ missing the s

PHP / Wordpress sending email to multiple addresses

I am trying this on wordpress, trying to email multiple email address. I have 2 email addresses in the database.
Here is the code:
$r = $wpdb->get_results($wpdb->prepare( "SELECT * FROM wp_users", $ID, $user_email ));
foreach ($r as $row) {
$to = 'someone#myhost.com';
$bcc = $row->user_email;
$subject = $_POST["subject"];
$message = $_POST["message"];
$headers = 'From: me#mymail.com' . "\r\n" .
'Reply-To: me#mymail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)) {
echo "Email sent";
}
else {
echo "Email sending failed";
}
It's sending emails BUT what's happening is that the TO (someone#myhost.com) is getting 2 emails and the $bcc is getting none.
What am I doing wrong here?
Yep, this behavior it is pretty normal, you forgot to put in $headers the Bcc: part, it should be like:
$headers = 'From: me#mymail.com' . "\r\n" .
'Reply-To: me#mymail.com' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Bcc: '.$bcc. "\r\n".
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

How to send email using PHP?

I want to send email using PHP when an HTML form is submitted.
I learnt from a tutorial and made this PHP script, but when I check it in my webmail, I only see email address, subject and message, but no name. What do I need to do to get the name to show up?
Form variables:
if (empty($_POST) === false) {
$errors = array();
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$subject = trim($_POST["subject"]);
$message = trim($_POST["message"]);
$answerbox = trim($_POST["answerbox"]);
// ... etc (validation)
if (empty($errors) === true) {
$headers = 'From: '.$email. "\r\n" . $name.
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail('mail#example.com',$subject,$message,$headers);
print "<p class='formerrors'>Thank you for your message, I'll get back to you shortly!</p>";
}
The format of your "From" header isn't right. It should be in the following Format:
"From: Sender Name <sender#domain.com>"
So your $headers assignment should read:
$headers = 'From: '.$name.' <'.$email. ">\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You should modify your $headers variable as:
$headers = 'From: ' .$name. ' <' .$email. '>' . "\r\n" .
'Reply-To: '.$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
Please read more about mail function on php.net over here: http://php.net/manual/en/ref.mail.php
Moreover maybe PHPMailer, see https://github.com/PHPMailer/PHPMailer is interesting for you to enrich your mails.

Categories