Send a message with an attachment using the Mail function in PHP

Source: Internet
Author: User
Tags base64 eol md5 mixed rand trim win32

mail function, sending mail

Syntax: Mail (to,subject,message,headers,parameters)

To specify the recipient of the message
Subject specify the subject of the message. This parameter cannot contain any newline characters
Message stipulates the messages to be sent
Headers specify additional headers, such as from, Cc, and BCC
Parameters specify additional parameters for the SendMail program.
Encountered the main problem is garbled, just started some clients receive mail good (such as QQ mailbox, estimated with automatic that identification code) some not foxmail, ipad display garbled, the correct way to set this mail headers on the line, Here are some examples of the perfect gibberish that I use.


The process of configuring the php.ini file in PHP is divided into two steps:

1. First find the place where you put all the php,apache,mysql files, in the PHP folder you can find a file: php.ini, open, find mail function Place, change the original configuration code as follows (Windows system only):

[Mail Function]
; For Win32.
SMTP =smtp.sohu.com
Mtp_port=25


; For Win32.
Sendmail_from = Fill in the full name of your email.

Here is the Sohu mail server settings, if you use 163 of the mailbox, set to: smtp.163.com

2. In the C-disk search php.ini, the choice is not a shortcut to the php.ini, should be inside the c/windows, open it, like the above modify it, save.

When you are done, remember to restart the Apache server, and then the Mail () function is available.

Cases

The code is as follows Copy Code

<?php
When you send an HTML e-mail message, always set the Content-type
$headers = "mime-version:1.0". "RN";

$headers. = "content-type:text/html; Charset=utf-8 ";

Mail ($to, $subject, $message, $headers);
?>

The above function can not bring attachments, let's upgrade the following

The code is as follows Copy Code

<?php

Class Mail {
Private $topic;
Private $toaddr;
Private $fromaddr;
Private $CC;
Private $content;
Private $attach;
Private $header;
private $domain;//mailbox domain
Private $msg;

Private $filename;
Private $filemime;
Private $filestr;

Private $boundary;
Private $uniqid;

Private $eol; The type of line break added at the end of each line


function __construct () {
$this->geteot ();//Generate End line break
$this->getuniq_id ();
$this->header= ';
$this->attach= ';
$this->cc= ';
$this->msg= ';


}


Public Function getfromaddr () {
return $this->fromaddr;
}

Public Function setfromaddr ($FROMADDR) {
$this->fromaddr = $fromaddr;
}

Public Function Gettopic () {
return $this->topic;
}


Public Function gettoaddr () {
return $this->toaddr;
}


Public Function GETCC () {
return $this->cc;
}

Public Function getcontent () {
return $this->content;
}


Public Function Getattach () {
return $this->attach;
}


Public Function Settopic ($topic) {
$this->topic = mb_convert_encoding (Trim ($topic), ' UTF-8 ', ' auto ');
}

Public Function settoaddr ($TOADDR) {
$this->toaddr = Trim ($TOADDR);
}


Public Function SETCC ($CC) {
$this->cc = Trim ($CC);
}


Public Function SetContent ($content) {
$this->content = mb_convert_encoding (Trim ($content), ' UTF-8 ', ' auto ');
}


Public Function Setattach ($attach) {
$this->attach = Trim ($attach);
}

Public Function GetDomain () {
return $this->domain;
}

Public Function SetDomain ($domain) {
$this->domain = $domain;//The value entered is ' @domain. com '
}


/*
* Set line breaks based on system type
*/
Private Function Geteot () {
if (Strtoupper (substr (php_os, 0, 3) = = ' WIN ') {
$this->eol = "RN";
} elseif (Strtoupper (substr (php_os, 0, 3) = = ' MAC ')) {
$this->eol= "R";
} else {
$this->eol = "n";
}
}


Private Function Getboundary () {

$this->boundary= '--'. SUBSTR (MD5 (Time (). Rand (1000,2000)), 0,16);

}

Private Function getuniq_id () {

$this->uniqid= MD5 (Microtime (). Time (). Rand (1,100));

}

Private Function Outputcommonheader () {
$this->header. = ' from: '. $this->fromaddr. $this->eol;
$this->header. = ' to: '. $this->toaddr. $this->eol;
$this->header. = ' Subject: '. $this->topic. $this->eol;
$this->header. = ' Message-id: < '. $this->uniqid. $this->domain. ' > '. $this->eol;
$this->header. = ' mime-version:1.0 '. $this->eol;
$this->header. = ' reply-to: '. $this->fromaddr. $this->eol;
$this->header. = ' Return-path: '. $this->fromaddr. $this->eol;
$this->header. = ' X-mailer:xmail System '. $this->eol;
$this->header. = ' Content-disposition:inline '. $this->eol;
}

Private Function Mime_content_type ($f)
{
$temp = Trim (exec (' File-bi '. Escapeshellarg ($f)));
$temp = preg_replace ('/s+/', ', ', $temp);
$temp = Explode (", $temp);
return $temp [0];
}//to determine the MIME type of a file


/*
* With CC only
*/
Private Function MAILWITHCC () {
$this->header. = ' Cc: '. $this->cc. $this->eol;
$this->header. = ' content-type:text/html; Charset=utf-8 '. $this->eol;
$this->header. = ' content-transfer-encoding:8bit '. $this->eol;
$this->msg = $this->content;
if (Mail ($this->toaddr, $this->topic, $this->msg, $this->header)) {

return 1;
}else{
return 0;
}
}
/*
* $filedir need to be an absolute address
*/
Private Function AttachmentToBase64 ($filedir) {
$this->filename = basename ($filedir);
@ $fopen = fopen ($filedir, ' R ');
$str = Fread ($fopen, FileSize ($filedir));
$str = Base64_encode ($STR);
$this->filestr = $str;
}


/*
* with attachments only
*/
Private Function Mailwithattach () {
$this->attachmenttobase64 ($this->attach );
$this->header. = ' content-type:multipart/mixed boundary= '. Str_replace ('--', ', $this->boundary). $this->eol;
$this->msg. = $this->eol. $this->boundary. $this->eol;
$this->msg. = ' content-type:text/html; Charset=utf-8 '. $this->eol;
$this->msg. = ' Content-disposition:inline '. $this->eol;
$this->msg. = $this->eol. $this->content. $this->eol;
$this->msg. = $this->boundary. $this->eol;
$this->msg. = ' Content-type: '. $this->mime_content_type ($this->attach). $this->eol;
$this->msg. = ' Content-disposition:attachment filename= '. $this->filename. ' " $this->eol;
$this->msg. = ' content-transfer-encoding:base64 '. $this->eol;
$this->msg. = $this->eol. $this->filestr. $this->eol;
$this->msg. = $this->eol. $this->boundary. '--';

if (Mail ($this->toaddr, $this->topic, $this->msg, $this->header)) {

return 1;
}else{
return 0;
}
}

/*
* With accessories and CC
*/
Private Function Mailall () {

$this->attachmenttobase64 ($this->attach);
$this->header. = ' Cc: '. $this->cc. $this->eol;
$this->header. = ' content-type:multipart/mixed boundary= '. Str_replace ('--', ', $this->boundary). $this->eol;
$this->msg. = $this->eol. $this->boundary. $this->eol;
$this->msg. = ' content-type:text/html; Charset=utf-8 '. $this->eol;
$this->msg. = ' Content-disposition:inline '. $this->eol;
$this->msg. = $this->eol. $this->content. $this->eol;
$this->msg. = $this->boundary. $this->eol;
$this->msg. = ' Content-type: '. $this->mime_content_type ($this->attach). $this->eol;
$this->msg. = ' Content-disposition:attachment filename= '. $this->filename. ' " $this->eol;
$this->msg. = ' content-transfer-encoding:base64 '. $this->eol;
$this->msg. = $this->eol. $this->filestr. $this->eol;
$this->msg. = $this->eol. $this->boundary. '--';

if (Mail ($this->toaddr, $this->topic, $this->msg, $this->header)) {

return 1;
}else{
return 0;
}

}
/*
* Without CC and accessories
*/
Private Function Mailsimple () {
$this->header. = ' content-type:text/html; Charset=utf-8 '. $this->eol;
$this->header. = ' content-transfer-encoding:8bit '. $this->eol;
$this->msg = $this->content;
if (Mail ($this->toaddr, $this->topic, $this->msg, $this->header)) {

return 1;
}else{
return 0;
}
}

Public function Send () {

if (Empty ($this->attach) &&empty ($this->cc)) {
$this->outputcommonheader ();
return $this->mailsimple ();

}else if (empty ($this->attach)) {
$this->outputcommonheader ();
return $this->MAILWITHCC ();

}else if (empty ($this->cc)) {
$this->outputcommonheader ();
$this->getboundary (); There are attachments to generate boundary
return $this->mailwithattach ();

}else if (!empty ($this->toaddr) &&!empty ($this->topic) &&!empty ($this->cc) &&! Empty ($this->content) &&!empty ($this->attach)) {
$this->outputcommonheader ();
$this->getboundary (); There are attachments to generate boundary
return $this->mailall ();
}
}
}


Sample code, some variables require a context environment:

The code is as follows Copy Code

$m = new Mail ();
$m->settoaddr ($this->temp[' receipt_address '));
$m->settopic ($this->temp[' mail_title '));
$m->setcontent ($this->temp[' mail_content '));
$m->setfromaddr ($_session[' user '] [' name ']. ' < '. $_session[' user ' [' name ']. ' @'. Systemdomain. ' > ');
$m->setdomain (' @ '). Systemdomain);
$m->SETCC ($this->temp[' cc_address '));
$m->setattach (PATH. ') /temp/'. $this->temp[' attachment_file ']);
$m->send ();


Advantages: Easy to use just a simple function
Disadvantage: Need php.ini support this function, if some servers do not support and can not change the environment that is not and always unstable, hair sometimes can be received sometimes not

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.