Rabu, 09 November 2011

Sending emails with PHP, ranging from regular email, HTML email to the email with attachments


Sending emails with PHP is not a difficult thing, PHP has provided the function mail () to send email with PHP. You can send plain text emails, HTML emails even emails with attachments easily.
Function mail ()
First we learn first the function mail (). The syntax is like this:
mail (string to, string subject, string message [, string additional_headers [, string additional_parameters]])
Function mail () has 3 mandatory parameters and one optional parameter. To the third parameter is the destination email, subject line and email content, as well as an optional parameter that is an email header.
You need to change php.ini settings on the SMTP to the mail function () can work on your computer. If you are using Unix / Linux then the function mail () using Sendmail on Linux, while the Windows function mail () will use a remote SMTP mail server to send mail. My advice if you want to try the function mail (), try on your hosting. All our hosting that supports PHP can perform the function mail () without any problems.
Okay rather than confused, we just practice to make the mail function () is simple:
<?

mail ("admin@websitesaya.com", "Welcome", "Hello admin \ n Thank you \ n for your response");

?>
If you try the example above then PHP will send an email to admin@websitesaya.com with the title "Welcome" and the contents of his email like this:

Hello admin
Thank you
for your response
How can I create a new line in the email content? course by using the \ n (New Line) we can put content into the email. If you try the code on your computer (localhost) and you have not done the SMTP settings, it will display an error message PHP. If we want a more humane error message if email is successfully sent or failed, you can use if as in the following example:
<?

if (mail ("admin@websitesaya.com", "Welcome", "Hello admin \ n Thank you \ n for your response")) {

echo "Email has been sent";

Else {}

echo "Email failed messages";

}

?>
With the code, if for some reason fails emails sent, it will display a message that the email failed messages. Now we try a more complex code.
<?

if (mail ("admin@websitesaya.com", "Welcome", "Hello this is content of email",, "From: Dhimas <tes@dhimasronggobramantyo.com>")) {

echo "Email has been sent";

Else {}

echo "Email failed messages";

}

?>
By using code like that, then the recipient will receive information that the sender is Dhimas with tes@dhimasronggobramantyo.com email address. Easy right? Now we learn that even more difficult.
HTML email
To send emails in HTML is also not difficult. We just need to notify the email destination that email that we send HTML forms. How do I? course by adding a header to indicate that our email HTML email. Okay instead of just trying to confuse the following php code:
<? Php

mail ('admin@websitesaya.com', 'Email Subject',

'<html> <body> <p> <i> Hello world </ i>, this HTML emails you know. </ P> </ body> </ html>',

"To: The Receiver <admin@websitesaya.com> \ n".

"From: The Sender <adadeh@gmail.com> \ n".

"MIME-Version: 1.0 \ n".

"Content-type: text / html; charset = iso-8859-1");
Header that we use to send HTML email is:
"MIME-Version: 1.0 \ n".
"Content-type: text / html; charset = iso-8859-1");
With a header like that, then that email will be read as an HTML file and of course for our email we can insert HTML code into any HTML emails.
Remember, if the email client / destination does not support HTML, the email is not opened. But most current email address already supports HTML. Remember, most, means there is not support HTML.
Email with Attachment
Now you may ask? can not send emails with attachments? The answer could be? So how? of course by changing its header. First of all we do we need a form to upload a file, and we then take the uploaded file variable (see articles on uploading files with php).
All email using base64 encoding for attachments mengencoding either in the form of a binary file or text file.
Because we need mengencoding our attachment to Base64 encoding, then we need a PHP function base64_encode (). Once encoded, we put our results into the header and send the email.
Okay, rather than just trying to confuse us directly. We will create two files, the first is form.html sending an email that contains a form. And the second is mail.php which contains a function to send us an email. Immediately, we make form.html and fill it with the following code:
<html>

<head>

<title> Send e-mail with attachments </ title>

</ Head>

<body>

<h1> Send Email with Attachment </ h1>

<form method="POST" action="mail.php" enctype="multipart/form-data">

<p> To: <input type="text" name="to" value="" /> <br />

From: <input type="text" name="from" value="" /> <br />

Title: <input type="text" name="subject" value="" /> </ p>

<p> Message: <br />

<textarea cols="70" rows="20" name="message"> </ textarea> </ p>

<p> File: <input type="file" name="fileatt" /> </ p>

<p> <input type="submit" value="Kirim" /> </ p>

</ Form>

</ Body>

</ Html>
Okay, you would have understood the code, because the code is just a normal HTML code. Whereby when the send button is clicked, then we call the mail.php file. Now create mail.php and fill it with the following code:
<html>

<head>

<title> Sending Email with Attachment </ title>

</ Head>

<body>

<?

$ To = $ _POST ['to'];

$ From = $ _POST ['from'];

$ Subject = $ _POST ['subject'];

$ Message = $ _POST ['message'];



$ Fileatt = $ _FILES ['fileatt'] ['tmp_name'];

$ Fileatt_type = $ _FILES ['fileatt'] ['type'];

$ Fileatt_name = $ _FILES ['fileatt'] ['name'];



$ Headers = "From: $ from";



if (is_uploaded_file ($ fileatt)) {

$ File = fopen ($ fileatt, 'rb');

$ Data = fread ($ file, filesize ($ fileatt));

fclose ($ file);

$ Semi_rand = md5 (time ());

$ Mime_boundary = "== Multipart_Boundary_x semi_rand} {$ x";

$ Headers .= "\ nMIME-Version: 1.0 \ n".

"Content-Type: multipart / mixed; \ n".

"Boundary = \" {$ mime_boundary} \ "";

$ Message = "Email with attachments and MIME format. \ N \ n".

"--{$ Mime_boundary} \ n ".

"Content-Type: text / plain; charset = \" iso-8859-1 \ "\ n".

"Content-Transfer-Encoding: 7bit \ n \ n".

$ Message. "\ N \ n";

$ Data = chunk_split (base64_encode ($ data));



$ Message .= "--{$ mime_boundary} \ n ".

"Content-Type: {$ fileatt_type}; \ n".

"Name = \" {$ fileatt_name} \ "\ n".

"Content-Transfer-Encoding: base64 \ n \ n".

$ Data. "\ N \ n".

"--{$ Mime_boundary} - \ n ";

}



$ Ok = @ mail ($ to, $ subject, $ message, $ headers);

if ($ ok) {

echo "Email has been sent <p> </ p>";

Else {}

echo "Email failed <p> sent! </ p>";

}

?>

</ Body>

</ Html>
If you have, run form.html, fill out forms that are available. Do not forget to retrieve files from your computer, then send the email. Easy right? we need to remember our mengencoding file attachments with base64 encoding. Good luck ...

Tidak ada komentar:

Posting Komentar