How to Send UTF8 Email With Sendmail
A Web developer may design his website to automatically send emails. This allows him to instantly confirm the email address of a new subscriber to the website or to send out email updates and newsletters. The developer may use the PHP scripting language and its mail function. This function uses his server's operating system's sendmail feature to send the mail. The mail function also allows the developer to attach email header information. One of the header values, known as content-type, formats the email into the developer's character set of choice. This allows him to send the email in the UTF-8 character set.
Instructions
-
-
1
Assign a destination point address it to a new variable. In this sample code, the destination point has been established as recepient@gmail.com. The email address lives in the newly created "to" variable:
$to = "recepient@gmail.com";
-
2
Attach a subject. The email should have a subject. In this example, a variable named "subject" will contain the subject of the email:
$subject = "This is the subject of my email";
-
-
3
Write the message. Assign the message to a variable. This text will appear in the main body of the email:
$message = "Thank you for reading this email.";
-
4
Create the email headers and establish the UTF-8 character set. The headers provide important information about the email's origination point. Always include headers in each email or you will risk having the email marked as spam and never reaching the intended recipient.
-
5
Look at the sample code. You will notice that the from and reply-to addresses have been assigned. Also, take note that the content-type value has also been established. The UTF-8 character set setting should go here:
$headers = 'From: myAddress@myDomain.com' . "\n" .
'Reply-To: myAddress@myDomain.com' . "\n" .
'Content-Type: text/html; charset="utf-8"' . "\n" .
'X-Mailer: PHP/' . phpversion();
-
6
Include a return path. A return path setting also helps avoid the dreaded spam categorization. Here the return path has been assigned to the returnpath variable:
$returnpath = "-ftest@dmvchangeofaddress.com";
-
7
Execute the PHP mail function. Pass all of the variables inside of the function. Once executed, the mail function will use your server's sendmail feature to send the mail:
mail($to, $subject, $message,$headers,$returnpath);
-
8
Place the code between PHP brackets, name and save the PHP file and upload it to the server. The complete code appears as follows:
<?php
$to = "recepient@gmail.com";
$subject = "This is the subject of my email";
$message = "Thank you for reading this email.";
$headers = 'From: myAddress@myDomain.com' . "\n" .
'Reply-To: myAddress@myDomain.com' . "\n" .
'Content-Type: text/html; charset="utf-8"' . "\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message,$headers,$returnpath);
?>
-
1
References
- Photo Credit Comstock/Comstock/Getty Images