Send Email using PHP
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 "])); } ?>
Follow tutorialspoint
Last lesson: Session in PHP
Next article: Upload File in PHP
You should read it
- How to send bulk emails on Apple Mail
- How to Send Email with Telnet
- How to use Mailspre to send anonymous emails
- How to write emails effectively and professionally?
- 9 ways to attach large files to emails
- How to Send a Message to Your Phone via Email
- The self-canceling email feature (Confidential Mode) has been officially updated for Gmail on Android
- How to Send Emails
May be interested
- Instructions for sending free SMS via Outlookon microsoft outlook 2007, you can easily send (or receive) sms messages on your computer to any phone, just compose a new sms (the same way you compose an email on outlook), then then click send to finish.
- 10 lessons from using Email listin this article we will show you how to manage your lists and choose what you write, what to send, how to send it ...
- The self-canceling email feature (Confidential Mode) has been officially updated for Gmail on Androidwith this self-destructive email (confidential mode) feature, the sender can set the lifetime of email and attachments. the recipient will not read the email nor download the attachments after the time has expired.
- How to write emails effectively and professionally?every day you work with colleagues, partners via email, chat programs ... is a main and professional communication channel at work, how to be effective as well as optimize time and performance which email messages do you bring to you?
- How to send an anonymous anonymous email on Eskiimoeskiimo helps you send an email anonymously without having to log in or create an account.
- Trick to use Gmail more effectivelyit can be said that email is something that most people use every day. most people choose gmail for reasons like robustness or simply because of its ease of use.
- How to Send Videosit is relatively simple to send videos to people if you have the right tools. you usually need to store video files on your computer before you can send them. while sending a video directly from your email client is possible, other ways...
- Instructions for recovering email sent on Gmail iPhone / iPadif you accidentally send the wrong message on gmail, we can completely postpone the sending process thanks to the undo send feature available on gmail.
- How to send photos and audio recordings via email with VoNovono application will help you store a lot of content such as audio files, images, handwritten content and send to others via email.
- 10 types of emails that cause 'inhibition' should be removed immediatelyemail 'life buoy' in many business situations but this is not an appropriate choice so you can send anything you want.