Home > Article > Backend Development > Detailed explanation of sending emails in php
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='#'>点击激活</a> "; $mail->AltBody ="text/html"; if(!$mail->Send()) { echo "邮件发送有误 <p>"; echo "邮件错误信息: " . $mail->ErrorInfo; exit; } else { echo "邮件发送成功!<br />"; }
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('protocol'=>'smtp', 'smtp_host' => 'smtp.163.com', 'smtp_user' => 'yourname', 'smtp_pass' => '*****', 'smtp_port' => '25', '_smtp_auth' => TRUE, 'wordwrap' => TRUE, 'charset' => 'iso-8859-1' ); $this->load->library('email', $config); $this->email->from('m13323262052@163.com', 'Your Name'); $this->email->to('m13323262052@163.com'); $this->email->subject('Email Test'); $this->email->message('Testing the email class.'); $this->email->send(); }
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!