UTF-8 in PHP mail() for ÜÖÄ - php

I have this PHP Code to send a Mail
if(Input::has('email')){
$to = Input::get('email');
$link = 'blabla';
$subject = 'blabla';
$message = "Ü Ä Ö jfklsfjal";
$headers = 'From: dontreply#asdf.com' . "\r\n" .
'Reply-To: dontreply#asdf.com' . "\r\n" .
'Content-Type: text/plain;charset=utf-8\r\n' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Problem is, that it still doesnt send the Ö Ä etc. not right (but I just added the content-type)!
Does anybody have any idea why it doesnt work?

I think your header is incorrect, because you have \r\n part of the content-type (which is wrapped in single quote), i.e.:
$headers = 'From: dontreply#asdf.com' . "\r\n" .
'Reply-To: dontreply#asdf.com' . "\r\n" .
'Content-Type: text/plain;charset=utf-8\r\n' . "\r\n" .
-------------------------------------------------------^^^^
'X-Mailer: PHP/' . phpversion();
Try changing the above to:
$headers = 'From: dontreply#asdf.com' . "\r\n" .
'Reply-To: dontreply#asdf.com' . "\r\n" .
'Content-Type: text/plain;charset=utf-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
If that doesn't work, try this:
$headers = 'From: dontreply#asdf.com' . "\r\n" .
'Reply-To: dontreply#asdf.com' . "\r\n" .
'Content-Type: text/html;charset=utf-8' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

Related

$_SERVER['SERVER_SOFTWARE'] doesn't work on command line

I try to run a PHP script from command line to check if a specific webserver is running, but it doesn't work.
This one works:
$to = 'mail#address.com';
$subject = 'LSWS Check';
$message = 'Apache is Running';
$headers = 'From: root' . "\r\n" . 'Reply-To: mail#address.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$serverSoftware = $_SERVER['SERVER_SOFTWARE'];
$isWebserver = 'Apache';
mail($to, $subject, $message, $headers);
This one doesn't work:
$to = 'mail#address.com';
$subject = 'LSWS Check';
$message = 'Apache is Running';
$headers = 'From: root' . "\r\n" . 'Reply-To: mail#address.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$serverSoftware = $_SERVER['SERVER_SOFTWARE'];
$isWebserver = 'Apache';
if (strpos($_SERVER[$serverSoftware], $isWebserver) !== false) {
mail($to, $subject, $message, $headers);
}
strpos($_SERVER[$serverSoftware] doesn't work, but why?

Why I can't insert a new line with mail() function?

I am trying to send few emails and ofcourse I need to breake some lines and put some spaces.
This is how I'm trying to do it, without luck:
for ($i = 0; $i < count($dataArray); $i++) {
$to = $dataArray[$i]['email'];
$subject = 'New member message';
$message = "Hello!" . "\r\n" . "Member message: " . "\r\n" . "\r\n" . $dataArray[$i]['message'] . "\r\n" . "\r\n" . "This is an automated message, please do not respond to it!";
$headers = 'From: info#domain.com' . "\r\n" .
'Reply-To: info#domain.com' . "\r\n" .
'Content-Type: text/html; charset=UTF-8' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
But the result leads to a single line text, including the \r\n in it as text - it is not inserting a new line.
How can I insert a new line? Why \r\n is not working?
I know that I'm missing a really, really small part here, but as a php beginner, I'm not able to spot it.
For Content-type: text/html, new line is <br>
For Content-type: text/plain, new line is \r\n
Try <br> tag before /r/n. hope it will work.
You have to use a simple
<br />
for a new line, the
\r\n
is just for the header
Try this code.
for ($i = 0; $i < count($dataArray); $i++) {
$to = $dataArray[$i]['email'];
$subject = 'Testing sendmail.exe';
$message = "Hello!" . "\r\n" . "Member message: " . "\r\n" . "\r\n" . $dataArray[$i]['message'] . "\r\n" . "\r\n" . "This is an automated message, please do not respond to it!";
$headers = 'From: info#domain.com' . "\r\n" .
'Reply-To: info#domain.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 'mail send';
}
else
{
echo 'mail fail';
}
}
Change content type of header to 'Content-type: text/html; charset=iso-8859-1'

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