Use php mail () to send emails

Source: Internet
Author: User
Tags php tutorial

Use the php Tutorial mail () to send emails

The mail () function allows you to send emails directly from scripts.

If the mail is successfully received, true is returned; otherwise, false is returned.

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

Parameters Description
To Required. Specifies the recipient of the email.
Subject Required. Specifies the subject of the email. This parameter cannot contain any line breaks.
Message Required. Specifies the message to be sent.
Headers Required. Specify additional headers, such as From, Cc, and Bcc.
Parameters Required. Specify additional parameters for the sendmail program.

Simply send html content

<Html>
<Head>
<Title> Simple Send Mail Form </title>
</Head>
<Body>
<H1> Mail Form <Form name = "form1" method = "post" action = "SimpleEmail. php">
<Table>
<Tr> <td> <B> To </B> </td> <input type = "text" name = "mailto" size = "35"> </td> </tr>
<Tr> <td> <B> Subject </B> </td>
<Td> <input type = "text" name = "mailsubject" size = "35"> </td> </tr>
<Tr> <td> <B> Message </B> </td>
<Td> <textarea name = "mailbody" cols = "50" rows = "7"> </textarea> </td>
</Tr>
<Tr> <td colspan = "2">
<Input type = "submit" name = "Submit" value = "Send">
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>
 
 
<! -- SimpleEmail. php
<? Php
If (empty ($ mailto )){
Die ("Recipient is blank! ");
    }

If (empty ($ mailsubject )){
$ Mailsubject = "";
    }

If (empty ($ mailbody )){
$ Mailbody = "";
    }

$ Result = mail ($ mailto, $ mailsubject, $ mailbody );

If ($ result ){
Echo "Email sent successfully! ";
} Else {
Echo "Email cocould not be sent .";
    }
?>

CC and BCC functions

HTML>
<HEAD>
<TITLE> Send email with CC and BCC </TITLE>
</HEAD>
<BODY>
<FORM action = "sendemailWithCC_BCC.php" method = post name = form1>
<TABLE>
<TBODY>
<TR>
<TD>
<DIV align = right> <B> To </B> </DIV> </TD>
<TD>
<P> Name <INPUT name = mailtoname size = 35> <BR> Email
<INPUT name = mailtomail size = 35> </p> </TD> </TR>
<TR>
<TD>
<DIV align = right> <B> CC </B> </DIV> </TD>
<TD> <INPUT name = mailcc size = 35> </TD> </TR>
<TR>
<TD>
<DIV align = right> <B> BCC </B> </DIV> </TD>
<TD> <INPUT name = mailbcc size = 35> </TD> </TR>
<TR>
<TD>
<DIV align = right> <B> Priority </B> </DIV> </TD>
<TD> <SELECT name = mailpriority>
<OPTION value = 1> Highest </OPTION>
<OPTION value = 2> High </OPTION>
<OPTION selected value = 3> Normal </OPTION>
<OPTION value = 4> Low </OPTION>
<OPTION value = 5> Lowest </OPTION>
</SELECT>
</TD> </TR>
<TR>
<TD> <DIV align = right> <B> Subject </B> </DIV> </TD>
<TD> <INPUT name = mailsubject size = 35> </TD> </TR>
<TR>
<TD>
<DIV align = right> <B> Message </B> </DIV> </TD>
<TD> <TEXTAREA cols = 50 name = mailbody rows = 7> </TEXTAREA> </TD> </TR>
<TR>
<TD colSpan = 2>
<DIV align = center> <INPUT name = Submit type = submit value = Submit> </DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</FORM>
</BODY>
</HTML>
 
 
 
 
<! -- SendemailWithCC_BCC.php

<Html>
<Head>
<Title> Mail Sent </title>
</Head>
<Body>
<? Php
 
$ Message = "";
If (empty ($ mailtoname) | empty ($ mailtomail )){
Die ("Recipient is blank! ");
} Else {
$ To = $ mailtoname. "<". $ mailtomail. "> ";
    }
   
If (empty ($ mailsubject )){
$ Mailsubject = "";
    }

If ($ mailpriority> 0) & ($ mailpriority <6 )){
$ Mailheader = "X-Priority:". $ mailpriority. "n ";
    }

$ Mailheader. = "From:". "Sales Team <sales@yourdomain.com> n ";
$ Mailheader. = "X-Sender:". "support@yourdomain.comn ";
$ Mailheader. = "Return-Path:". "support@yourdomain.comn ";

If (! Empty ($ mailcc )){
$ Mailheader. = "Cc:". $ mailcc. "n ";
    }

If (! Empty ($ mailbcc )){
$ Mailheader. = "Bcc:". $ mailbcc. "n ";
    }
   
If (empty ($ mailbody )){
$ Mailbody = "";
    }
 
$ Result = mail ($ to, $ mailsubject, $ mailbody, $ mailheader );
Echo "<center> <B> Mail sent to". "$ to". "<br> ";
Echo $ mailsubject. "<br> ";
Echo $ mailbody. "<br> ";
Echo $ mailheader. "<br> ";
If ($ result ){
Echo "<p> <B> Email sent successfully! </B> </p> ";
} Else {
Echo "<p> <B> Email cocould not be sent. </B> </p> ";
    }
?>
<Div align = "center">
<Table> <tr> <td width = "66"> <div align = "right"> <B> To </B> </div> </td>
<Td width = "308"> <B> <? Php echo $ mailtoname. "[". $ mailtomail. "]";?> </B> </td> </tr>
        
<Tr> <td width = "66"> <div align = "right"> <B> CC </B> </div> </td>
<Td width = "308"> <B> <? Php echo $ mailcc;?> </B> </td> </tr>
<Tr> <td width = "66"> <div align = "right"> <B> BCC </B> </div> </td>
<Td width = "308"> <B> <? Php echo $ mailbcc;?> </B> </td> </tr>
<Tr> <td width = "66"> <div align = "right"> <B> Priority </B> </div> </td>
<Td width = "308"> <B> <? Php echo $ mailpriority;?> </B> </td> </tr>
<Tr> <td width = "66"> <div align = "right"> <B> Subject </B> </div> </td>
<Td width = "308"> <B> <? Php echo $ mailsubject;?> </B> </td> </tr>
<Tr> <td width = "66"> <div align = "right"> <B> Message </B> </div> </td>
<Td width = "308"> <B> <? Php echo $ mailbody;?> </B> </td> </tr>
</Table>
</Div>
</Body>
</Html>


In messages specified by the message parameter, rows must be separated by an LF (n. Each line cannot exceed 70 characters.

(In Windows) When PHP is directly connected to the SMTP server, if a full stop is found at the beginning of a line, it will be deleted. To avoid this problem, replace a single period with two periods.

 

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.