Send mail with PHP (advanced)

June 30, 2009, Posted by admin at 6:00 am

This tutorial is based on the “send mail with PHP” tutorial is basic and the dispatch to the BCC and CC recipients explain. Furthermore, additional header information such as sender, reply-to address or specifying a destination address for errors when sending mail addressed.

As in the tutorial Basic to read the features expected for at least three parameters. Optionally, you can now, however, two more parameters are added:

mail (recipient, subject, message, header, extended parameter);

Additional Options
The parameters “header” can be a lot of additional information are made. We would like here once the most important additional information present:

Sender address + name
If no sender address, by default, very curious how wwwrun@domain.de e-mail addresses or webmailer@domain.de as the sender. Looks for a very unprofessional and also confused, and some Web users. To make a correct sender, we use the following syntax:
$ optheader = “From: <test@example.com> first name last name”;

Reply-to address
Should the recipient to any other email address as the sender’s reply address, it can also be specified. The syntax for this citation:
$ optheader = “Reply-To: <reply@example.com> Name Surname”;

CC recipients
CC stands for Carbon Copy and it means the receivers that have this option to be approximated, a copy of the e-mail and receive a copy of receiver also can be displayed. As with “normal” recipients may also use multiple recipients with a comma separated terms. CC recipients are as follows:
$ optheader = “CC: <copy@example.com> Name Surname”;

BCC recipients
BCC stands for Blind Carbon Copy and it means the receivers that have this option, in the e-mail is not visible, just blind or invisible, are nevertheless a copy of the eMail. They are neither in the mail headers in the simple recipient list. As with “normal” recipients may also use multiple recipients with a comma separated terms. BCC recipients are defined as follows:
$ optheader = “BCC: <blind@example.com> Name Surname”;

Type the e-mail content
This option defines the MIME type of the email. The most common indications are here: text / html or text / plain. The specification text / html e-mail says the application of the content as HTML to be interpreted. The default is set but the indication text / plain, which an e-mail as a normal plain-text displays. Type of the email content is placed as follows:
$ optheader = “Content-type: text / html”;

Several options
Well, in the examples were used each option individually. If you want more of these options combine, you have between each data a newline ( n) to insert. The easiest way is with the Array implode method. It defines the various options in a whole array and then adds them to the function implode together.

Example:

$ optheader [] = “From: <test@example.com> first name last name”;
$ optheader [] = “Reply-To: <reply@example.com> Name Surname”;
$ header = implode ( ” n”, $ optheader);

Last but not Least
With the fifth and last parameter to the commands you can E-mail-order program are sent. The frequently used and interesting applications for the newsletter parameter is “-f”. In combination with one email address can be determined, while the recipient of error messages addresses to be sent. Example: If an e-mail account does not exist, send the destination server, usually an error mail with the message that this email could not be delivered. This email is then applied to the address directed by the webmaster and can be analyzed in order to check the email address from the newsletter distribution list to delete.

The syntax for this command is as follows:
$ extheader = “ferror @ example.com”;
Note: This is not a spelling mistake! The email address is right behind the parameter “-f”!

Abstract
As the crowning conclusion we now send an email with various options and parameters and thus conclude our mail with PHP “tutorial series.

$ receiver = “<vorname@example.com> first name”;
$ subject = “This is a test”;
$ message = “Hello, first name, how’s it going? n nViele greetings, your friend”;
$ bccarray = array ( “blind@example.com”, “bcc@example.com”);
$ bccempfaenger = implode (”,”, $ bccarray);
$ optheader [] = “From: <test@example.com> first name last name”;
$ optheader [] = “Reply-To: Name Surname <reply@example.com”;
$ optheader [] = “bcc:”. bccempfaenger $;
$ header = implode ( ” n”, $ optheader);
$ extheader = “ferror @ example.com”;
mail ($ receiver, $ subject, $ message, $ header, $ extheader);

No comment yet.

Leave a Reply