Home > Backend Development > PHP Tutorial > Detailed explanation of sending emails in php

Detailed explanation of sending emails in php

小云云
Release: 2023-03-22 20:38:02
Original
1880 people have browsed it

This article mainly shares with you the detailed explanation of how to send emails in PHP. It is mainly shared with you in the form of code. I hope it can help you.

1.phpmailer is very easy to use, no matter whether it is native or placed in a framework, there is no problem

    <?php  
    header("content-type:text/html;charset=utf-8");  
    include("class.phpmailer.php");  
    include("class.smtp.php");  
    $mail = new PHPMailer();   
    $mail->IsSMTP(); // send via SMTP   
    $mail->Host = "smtp.163.com"; // SMTP servers   
    $mail->SMTPAuth = true; // turn on SMTP authentication   
    $mail->Username = "*********"; // SMTP username 注意:普通邮件认证不需要加 @域名   
    $mail->Password = "*****"; // SMTP password   
    $mail->From = "******@163.com"; // 发件人邮箱   
    $mail->FromName = "zph"; // 发件人   
    $mail->CharSet = "utf-8"; // 这里指定字符集!   
    $mail->Encoding = "base64";   
    $mail->AddAddress("******@163.com","toyou"); // 收件人邮箱和姓名   
    $mail->AddReplyTo("******@163.com","wo");   
    //$mail->WordWrap = 50; // set word wrap 换行字数   
    //$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment 附件   
    //$mail->AddAttachment("/tmp/image.jpg", "new.jpg");   
    $mail->IsHTML(true); // send as HTML   
    // 邮件主题   
    $subject="测试";  
    $mail->Subject = $subject;   
    // 邮件内容   
    $mail->Body = "   
    <p>点击激活</p>   
    <a href=&#39;#&#39;>点击激活</a>  
    ";   
    $mail->AltBody ="text/html";   
    if(!$mail->Send())   
    {   
    echo "邮件发送有误 <p>";   
    echo "邮件错误信息: " . $mail->ErrorInfo;   
    exit;   
    }   
    else {   
    echo "邮件发送成功!<br />";   
    }
Copy after login

2. When using the php native mail() function, you need to install sendmail and linux Generally, sendmail has been installed and you can use mail() directly. If you are using Windows, you need to install it first.

3. When using the ci framework, the following

    public function sendemail()  
    {     
        $config = array(&#39;protocol&#39;=>&#39;smtp&#39;,  
            &#39;smtp_host&#39; => &#39;smtp.163.com&#39;,  
            &#39;smtp_user&#39; => &#39;yourname&#39;,  
            &#39;smtp_pass&#39; => &#39;*****&#39;,  
            &#39;smtp_port&#39; => &#39;25&#39;,  
            &#39;_smtp_auth&#39; => TRUE,  
            &#39;wordwrap&#39; => TRUE,  
            &#39;charset&#39; => &#39;iso-8859-1&#39;  
      
        );  
        $this->load->library(&#39;email&#39;, $config);  
        $this->email->from(&#39;m13323262052@163.com&#39;, &#39;Your Name&#39;);  
        $this->email->to(&#39;m13323262052@163.com&#39;);  
        $this->email->subject(&#39;Email Test&#39;);  
        $this->email->message(&#39;Testing the email class.&#39;);  
        $this->email->send();  
    }
Copy after login

Related recommendations:

Share how PHP uses PHPMailer to send emails

php implements the method of sending emails

smtp implements the function of sending emails in php

The above is the detailed content of Detailed explanation of sending emails in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template