Sending Hebrew subject in php mail goes Klingon...? - php

I'm trying to send email with hebrew content/subject like so:
$to = 'email#email.com';
$subject = "איזה יום יפה היום";
$message = 'ממש יום יפה';
$headers = 'From: email#email.com' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
mail($to, $subject, $message, $headers);
But what I get in the subject is more Klingon than modern Hebrew. The message itself comes out fine, it's just the subject that's all messed up.
What can I do? (I'm open to any hacks you got)

The Content-Type does only describe the message content but not the header. You need to apply the encoded-word encoding on the Subject value. See my answer on PHP email header subject encoding problem for further information.

Related

Send apostrophe s ('s) in email message using was error encode using PHP?

Send apostrophe s ('s) in email message using was error encode using PHP ?
<?PHP
include("connect.php");
$to = "example#example.com";
$subject = "TEST SUBJECT";
$message = "peter’s test";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: EXAMPLE <noreply#example.com>' . "\r\n";
$headers .= 'Return-Path: return#example.com' . "\r\n";
mail($to, $subject, $message, $headers, '-freturn#example.com');
?>
When i open my email , i see peter’s ’s test
How can i send apostrophe s ('s) in email message using php ?
you need to use mb_convert_encoding on the string containing the apostrophe:
$message = mb_convert_encoding("peter’s test", 'UTF-8');
You are using the character encoding charset=iso-8859-1 in your mail header. I used utf-8 and it worked fine. Try replacing charset=iso-8859-1 with charset=utf-8

Personalised 'To' field when sending large email list using PHP mail and multiple addresses in the Bcc

Im trying to remember a trick i was taught a while back but can not.
Basically, im using PHP mail() in this fashion:
$to = "emailAddress1#domain.com";
$subject = "Welcome to BuildSanctuary";
$messageContent = "Thankyou for registering Bla bla bla";
$message = 'SOME HTML EMAIL STUFF including the $messageContent var';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To:' . "\r\n";
$headers .= 'From: Accounts<accounts#domain.com>' . "\r\n";
$headers .= 'Bcc: emailAddress1#domain.com,emailAddress2#domain.com,emailAddress3#domain.com,emailAddress4#domain.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
I got told a while back that there was a way that you could put your own email in the to field, but when the users receive the emails it looks like they were personally sent the email and the to field shows just their email.
Is this possible? Cant think how.
Thanks.

PHP mail can not send when there is a url a tag in the body

When I added "a href tag " in the mail body the mail is not sent.
If I remove this 'a href and www' tag, the mail sends and all other content display as per my requirement.
I don't know where is the exact problem, I'm using GoDaddy hosting with PHP 5.3 version.
If anyone has a better solutions please share with me .
<?php
// multiple recipients
$to = 'ali.dzinemedia#gmail.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '<a href=www.google.com>Click here</a>';
// To send HTML `enter code here`mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
// Additional headers
$headers .= 'To: Mary <ali.dzinemedia#gmail.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <ali.dzinemedia#gmail.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
echo "To : ".$to;
// Mail it
mail($to, $subject, $message, $headers);
Use this as the header:
$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset: utf8\r\n";
that's if you want to use HTML in the body, but you have to create well formatted HTML, you know with all of its tags: html, head, body, and close them all.
<html>
<head></head>
<body>Content here and this is a link</body>
</html>
I had the same problem once, turned out my spam filter blocked the mail when a link was in it and let it trough when I removed the link.
Took me some time to notice that

Sending a Confirmation Email in PHP

I am quite new to PHP and am experimenting with an online ordering website in php. I have come across a problem when trying to send a confirmation email using a 'cc'.
Every time an order is processed, the order always sends to the 'CC' address specified as the '$from' and the sender (the from) is what is specified in the '$to' section. In the email confirmation received, it only displays the from section and the 'to' and 'cc' are empty as demonstrated below:
From: **$_SESSION['od_email']**
To: *This space is empty*
CC: orders#business.co.uk
Can anyone help to point out where I am going wrong? I have attached the code below.
//THIS IS CODE FOR THE EMAIL WHICH IS AUTOMATICALLY SENT TO THE CUSTOMER AND THE BUSINESS WHEN AN ORDER IS SUCCESSFULLY COMPLETED
//define the receiver of the email
$to = $_SESSION['od_email'];
//define the subject of the email
$subject = 'Order Confirmation | Ref No for Order: '. $_SESSION['orderId'];
//define the message to be sent. Each line should be separated with \n
$message = 'test';
//Who the message is from
$from = "orders#business.co.uk";
$cc = "orders#business.co.uk";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From:" . $from;
//bcc header going to the to go to the business as confirmation
$headers = "cc:" . $cc;
//send the email
$mail_sent = #mail( $to, $subject, $message, $headers );
What I need for it to show is:
From: **orders#business.co.uk**
To: **$_SESSION['od_email'];**
CC: **orders#business.co.uk**
Thanks in advance for any help given
When you're going to set the CC, you're overwriting what was in $headers. Try:
$headers = "From:" . $from . "\r\n";
$headers.= "Cc:" . $cc;
And if that doesn't help, there's always PHP's documentation on mail.
I would go all out with your headers and make sure you set everything you need to,
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";
also, I believe mail() needs a valid email address, I do not know how you have the email set before $_SESSION['od_email'] but if you are running a test and literally expecting $_SESSION['od_email'] to come with it that wont work, it will see it as a not valid email and return false, thus returning nothing. To further diagnose that problem tho we would need to know how $_SESSION['od_email'] is setup
EDIT
Okay I am pretty sure the issue is caused how you are setting up $_SESSION['od_email]' based on your comment below.
Note: I am assuming your database is $db, but if not you need to change $db to the prober name
Let's try this and see how it works
// Get Data from the Database
$query = "SELECT `od_email` FROM `tbl_order`";
$result = $db->query($query);
while ($data = $db->fetch_array($result)) {
// Set Up the SESSION email
$_SESSION['od_email'] = $data['od_email'];
// Prepare the Email
$to = $_SESSION['od_email'];
$subject = 'Order Confirmation | Ref No for Order: '. $_SESSION['orderId'];
$message = 'test';
$from = "orders#business.co.uk";
$cc = "orders#business.co.uk";
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: ' .$to. "\r\n";
$headers .= 'From: ' .$from. "\r\n";
$headers .= 'Cc: '.$cc. "\r\n";
// Send the email
mail($to,$subject,$message,$headers);
}
Couldn't he just add an #mail ? such as...
$mail_sent = #mail( $od_email, $subject, $message, $headers );
or
mail( $od_email, $subject, $message, $headers );
Replacing any variables.
Always worked for me.

How to add custom header to PHP mail?

I have a mail function with the standard parameters:
mail($to, $subject, $message, $headers);
Where:
$headers = "From: admin#mysite.org";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
Now, I'm trying to get the sent e-mails to show 'My Site' as the sender instead of 'admin#mysite.org' but if someone responds to the mail it still needs to go to admin#mysite.org.
I've tried putting "My Site" in the "From" field before and after the e-mail address but it just appends to the address. I've tried appending it to the $headers variable but that messes the whole e-mail up. I've also tried adding it this way:
$headers .= "\r\nMy Site";
but that doesn't seem to do anything.
I know this can't be complicated as I've seen it done a hundred times, but I can't seem to find a straight-forward answer for this - how do I 'mask' the admin e-mail with the site name but still keep it as the address any response goes to?
Change your From: header to "From: My Site <admin#mysite.org>"
$headers = "From: My Site <admin#mysite.org>";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
From the PHP Docs.
There is the "From: something <something#something.com>"; header, and then there is also the optional fifth parameter of mail() which allows you to override the from setting that may or may not be set in php.ini.
<?php
$to = 'name#domain.com.au';
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "From: Name <noreply#domain.info>" . "\r\n";
$fifthp = '-f noreply#domain.info';
mail($to, $subject, $message, $header,$fifthp);
?>

Categories