Send Email using PHP
PHP must be configured correctly in the php.ini file with details about how your system sends email. Open php.ini available in the / etc / directory and find the section that starts with [mail function].
PHP must be correctly configured in the php.ini file with details about how your system sends email. Open php.ini available in the / etc / directory and find the section that starts with [mail function] .
Windows users should make sure that there are 2 instructions provided. The first one is called SMTP, which defines your Server email address. The second is called sendmail_from, which defines your own email address.
Configuration for Windows looks like this:
[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net
; For win32 only
sendmail_from = webmaster@quantangang.com
Linux users need for PHP to know the location of the sendmail application. The path must be specified for the sendmail_path directive.
The configuration for Linux will look like this:
[mail function]
; For Win32 only.
SMTP =
; For win32 only
sendmail_from =
; For Unix only
sendmail_path = / usr / sbin / sendmail -t -i
Let us now send emails using PHP.
Send simple text emails in PHP
PHP uses the mail () function to send an email. This function requires 3 required parameters to determine the recipient's email address, email header and email content, in addition to 2 optional parameters.
mail (to, subject, message, headers, parameters);
The following table describes these parameters:
Parameter Descriptionto Required. Specify the email address of the subject recipient. Specify the title of the email. This parameter cannot contain newline characters (new line) Required message. Specify the content of the email. Lines are separated by an LF (n). Each line does not exceed 70 arbitrary headers. Define additional headings, such as From, Cc and Bcc. Additional headings should be separated from an arbitrary CRLF (rn) parameters. Specify an additional parameter for the mailing programAs soon as the mail function is called, PHP will send this email, then it will return true if successful and false if it fails.
You can specify multiple recipients by assigning a recipient's email address list, separated by commas (,) for the first parameter of the mail () function.
Send HTML email in PHP
When you send a text message using PHP, all content will be processed as plain text. Even if you add HTML tags in a text email, it will display as regular text and HTML tags will not be formatted in HTML syntax. However, PHP offers the option to email HTML as a true HTML email.
When sending an email message, you can define a Mime version, content type and character set to send an HTML email.
For example
The following example sends an HTML email to xyz@somedomain.com copying it to afgh@somedomain.com. You can code this program in the form of: The user will enter all content and then it will send the email.
Gửi email sử dụng PHP php $to = "xyz@somedomain.com" ; $subject = "Đây là tiêu đề email" ; $message = " Đây là nội dung email. " ; $message .= "
Have a good day!
" ; $header = "From:abc@somedomain.com rn" ; $header .= "Cc:afgh@somedomain.com rn" ; $header .= "MIME-Version: 1.0rn" ; $header .= "Content-type: text/htmlrn" ; $retval = mail ( $to , $subject , $message , $header ); if ( $retval == true ) { echo "Email đã gửi thành công." ; } else { echo "Email không gửi được." ; } ?>
Attach the same Email in PHP
To send an email with mixed content, set the Content-type header to multipart / mixed . The text and attachment can then be specified within the boundaries.
A boundary is started with 2 dashes followed by a unique number that cannot appear in the content of the email. The md5 () function of PHP is used to create a 32-digit hexa number and is unique. The last Boundary indicating the last part of the email must also end with two hyphens.
php // Đây là các biến quan trọng phải có $from = $_REQUEST [ "from" ]; $emaila = $_REQUEST [ "emaila" ]; $filea = $_REQUEST [ "filea" ]; if ( $filea ) { function mail_attachment ( $from , $to , $subject , $message , $attachment ){ $fileatt = $attachment ; // Đường dẫn tới file $fileatt_type = "application/octet-stream" ; // Loại file $start = strrpos ( $attachment , '/' ) == - 1 ? strrpos ( $attachment , '//' ) : strrpos ( $attachment , '/' )+ 1 ; $fileatt_name = substr ( $attachment , $start , strlen ( $attachment )); // Tên file sẽ được sử dụng cho file như phần đính kèm $email_from = $from ; // Địa chỉ email của gười gửi $subject = "Email đính kèm mới" ; $email_subject = $subject ; // Tiêu đề email $email_txt = $message ; // Nội dung email $email_to = $to ; // Địa chỉ email người nhận $headers = "Từ: " . $email_from ; $file = fopen ( $fileatt , 'rb' ); $data = fread ( $file , filesize ( $fileatt )); fclose ( $file ); $msg_txt = "nn Bạn đã nhận được email đính kèm mới từ $from" ; $semi_rand = md5 ( time ()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x" ; $headers .= "nMIME-Version: 1.0n" . "Content-Type: multipart/mixed;n" . " boundary="{$mime_boundary}"" ; $email_txt .= $msg_txt ; $email_message .= "Đây là email gồm nhiều phần trong định dạng MIME.nn" . "--{$mime_boundary}n" . "Content-Type:text/html; charset = "iso-8859-1"n" . "Content-Transfer-Encoding: 7bitnn" . $email_txt . "nn" ; $data = chunk_split ( base64_encode ( $data )); $email_message .= "--{$mime_boundary}n" . "Content-Type: {$fileatt_type};n" . " name = "{$fileatt_name}"n" . //"Content-Disposition: attachment;n" . //" filename = "{$fileatt_name}"n" . "Content-Transfer-Encoding: base64nn " . $data . " nn " . " --{ $mime_boundary }-- n "; $ok = mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo " File đã gửi thành công . "; unlink($attachment); // Xóa file sau khi Attachment đã được gửi. }else { die("Rất tiếc email không thể gửi, vui lòng thử lại ! "); } } move_uploaded_file($_FILES[" filea "][" tmp_name "], 'temp/'.basename($_FILES['filea']['name'])); mail_attachment(" $from ", " youremailaddress@gmail . com ", " subject ", " message ", (" temp / ".$_FILES[" filea "][" name "])); } ?>