How to send text messages using PHP. PHP Tips: Sending Emails with PHP: File Attachment Final message html

Contains only text (low message weight)

$name = "Peter Petrovich"; // optional variable as an example to insert into a message

$subject = "Subject of the message"; //Message subject
$message = "Hello".$name."!\n
Just ask how you are!\n
Sincerely, Ivan Ivanovich"; // message content
mail($email, $subject, $message, "From: [email protected]\nReply-To: [email protected]\nContent-type:text/plain; Charset=utf-8\r\n"); //send message
?>

2. Sending a letter mail php Type text/html

May contain graphic html elements, links, etc.
More information transferred

// send to multiple recipients
$to=" [email protected]" . ", "; // send to whom
$to .=" [email protected]" . ", "; // Attention! So we write the second and so on addresses
// don't forget the comma. Even in the last contact it will not be superfluous
// For beginners! $to .= dot in this case for appending to a variable

// set the content-type of the message if we want
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=utf-8 \r\n";

// additional data
$headers .= "From: yournick \r\n"; // from whom
$headers .= "Cc: [email protected]" . "\r\n"; // a copy of the message to this address
$headers .= "Bcc: [email protected]\r\n"; // blind copy of the message to this
mail($to, $subject, $message, $headers);
?>

3. How to use CSS in writing?

Here I want to upset you a little right away - CSS in the form you are used to should not be used. Mail, Yandex, Google, Outlook - throw styles out of emails. However, there is a way out.

Let's fulfill several conditions:
1) I hope there is no need to explain what we use content-type: text/html

2) Recall what is table Verstskaya. Now all styles are entered only in it. You can also remember center, font.




From: Ivanov Ivan Ivanovich
The address: [email protected]
Message: Watch and enjoy

FROM div same

The text you need

3) In the example above with sending a letter, this line was given. So let's not forget it

$headers = "MIME-Version: 1.0" . "\r\n";

4) We use HTML 3.2. Paste at the beginning of the html letter.


5) As a background - you can color. If you want an image background, then make all text and background an image. Alternatively, you can specify a background-image for the body.

6) The only picture in the cell in Gmail has a 3px indent from the bottom, to avoid this, we indicate it

// Open the file for reading in binary format $file=fopen("file.zip", "rb"); // Read it into a string $str_file $str_file=fread($file,filesize("file.zip")); // Convert this string to base64 format $str_file=base64_encode($str_file);

Now the variable $str_file, which contains the file, can be inserted into the email.

To finalize the material, let's write a function that sends a letter in HTML format to the specified addressee with an attached file:

/* $to - mail recipient's address $from_mail - mail sender's address $from_name - mail sender's name $subject - mail subject $message - the message itself in HTML format $file_name - the path to the file to be attached to the mail (it can be the name of the file selected in the field ) */ function sendMail($to,$from_mail,$from_name,$subject,$message,$file_name) ( $bound="spravkaweb-1234"; $header="From: "$from_name" n"; $header. ="To: $ton"; $header.="Subject: $subjectn"; $header.="Mime-Version: 1.0n"; $header.="Content-Type: multipart/mixed; boundary="$bound ""; $body="nn--$boundn"; $body.="Content-type: text/html; charset="windows-1251"\n"; $body.="Content-Transfer-Encoding: quoted -printablenn"; $body.="$message"; $file=fopen($file_name,"rb"); $body.="nn--$boundn"; $body.="Content-Type: application/octet -stream;"; $body.="name=".basename($file_name)."\n"; $body.="Content-Transfer-Encoding:base64n"; $body.="Content-Disposition:attachmentnn" ; $body.=base64_encode(fread($file,filesize($file_name)))."n"; $body.="$bound--nn"; if(mail($to, $subject, $body, $ header)) ( echo " Email was sent successfully!"; ) else ( echo " Message not sent! "; ); );

HTTP is based on the client-server architecture model and a stateless request/response protocol that operates by exchanging messages across a reliable TCP/IP connection.

An HTTP "client" is a program (Web browser or any other client) that establishes a connection to a server for the purpose of sending one or more HTTP request messages. An HTTP "server" is a program (generally a web server like Apache Web Server or Internet Information Services IIS, etc.) that accepts connections in order to serve HTTP requests by sending HTTP response messages.

HTTP makes use of the Uniform Resource Identifier (URI) to identify a given resource and to establish a connection. Once the connection is established, HTTP messages are passed in a format similar to that used by the Internet mail and the Multipurpose Internet Mail Extensions (MIME) . These messages include requests from client to server and responses from server to client which will have the following format:

http-message= | ; HTTP/1.1 messages

HTTP requests and HTTP responses use a generic message format of RFC 822 for transferring the required data. This generic message format consists of the following four items.

  • A start-line
  • Zero or more header fields followed by CRLF
  • An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields
  • Optionally a message-body

In the following sections, we will explain each of the entities used in an HTTP message.

Message start-line

A start-line will have the following generic syntax:

Start-line = Request-Line | Status Line

We will discuss Request-Line and Status-Line while discussing HTTP Request and HTTP Response messages respectively. For now, let's see the examples of start line in case of request and response:

GET /hello.htm HTTP/1.1 (This is Request-Line sent by the client) HTTP/1.1 200 OK (This is Status-Line sent by the server)

Header Fields

HTTP header fields provide required information about the request or response, or about the object sent in the message body. There are four types of HTTP message headers:

    general-header: These header fields have general applicability for both request and response messages.

    request-header: These header fields have applicability only for request messages.

    response-header: These header fields have applicability only for response messages.

    entity-header: These header fields define meta information about the entity-body or, if no body is present, about the resource identified by the request.

All the above mentioned headers follow the same generic format and each of the header field consists of a name followed by a colon ( : ) and the field value as follows:

Message-header = field-name ":" [ field-value ]

The following are the examples of various header fields:

User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3 Host: www.example.com Accept-Language: en, mi Date: Mon, 27 Jul 2009 12:28:53 PM GMT Server: Apache Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT ETag: "34aa387-d-1568eb00" Accept-Ranges: bytes Content-Length: 51 Vary: Accept-Encoding Content-Type: text/plain

message body

The message body part is optional for an HTTP message but if it is available, then it is used to carry the entity-body associated with the request or response. If entity body is associated, then usually content-type and content length headers lines specify the nature of the body associated.