0

I'm trying to send emails using PHP mail function.

This is my code:

$from = "mymail@example.com";
$replyto = "Encarnação"; 
        
$headers = 'Content-Type: text/html; charset=utf-8' . "\r\n";
$headers .= 'Content-Transfer-Encoding: base64' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: '. $replyto . "\r\n";
$headers .= 'Bcc: ohtermail@example.com'; 
mail($to, $subject, $message, $headers);

The emails are sent, but how the sender is showing as:

mymail@example.com

And I want it as:

My Name

Is this possible? I'm reading the manual and the examples provided seem to work, but why isn't my code? My server is Linux.

I've also tried:

$from = "Encarnação <mymail@example.com>"

But the accentuated characters are looking really strange

unstuck
  • 563
  • 2
  • 12
  • 29
  • 2
    `$from = "My Name "` and your current `Reply-To:` header is invalid and needs to follow the same format or at least be an actual email address. – Sammitch Feb 18 '21 at 21:34
  • Thank you. My Name has accentuated characters: Encarnação. It really looks strange with all those messed characters now – unstuck Feb 18 '21 at 21:38
  • All email headers _must_ be 7-bit-safe ASCII, so anything beyond the basic english alphabet needs to be encoded using a method that adheres to that. [eg: https://stackoverflow.com/questions/17064510/utf-8-encoding-for-subject-in-contact-form-email] Similarly, while email bodies _can_ be non-7bit-safe, it's generally not recommended as there are plenty of badly-configured email systems that will munge the encoding. – Sammitch Feb 18 '21 at 22:03
  • However these are just a couple of the literal hundreds of cases that you need to account for in order to send mail that will be: 1. Displayed properly by the recipient's mail client. 2. Not marked as spam by intermediate mail exchanges. 3. Not in any way required by SMTP, but are specified in hundreds of very boring and tedious RFCs. Use a mailing library like PHPMailer or SwiftMailer that has most of these built-in so that you don't have to waste time implementing them yourself. – Sammitch Feb 18 '21 at 22:07
  • @Sammitch thank you for the explanation. I'm going to do some research about how to implement and use PHP libraries, this is new to me – unstuck Feb 18 '21 at 22:13
  • I very strongly recommend [Composer](https://getcomposer.org/doc/00-intro.md) as the de-facto PHP library/dependency manager. There's a bit of a learning curve, but IMO it massively simplifies PHP development. – Sammitch Feb 18 '21 at 22:17
  • And I thought it was just one or two characters in the sender name LOL, this is really amazing! – unstuck Feb 18 '21 at 22:21
  • Email is a very boring rabbit hole that goes all the way down to the boiler room of hell itself. Any opportunity to abstract that away behind a library someone else maintains is a good one in my books. – Sammitch Feb 18 '21 at 22:30

0 Answers0