How to send email using phpmaill

小云云
Release: 2023-03-17 12:26:02
Original
2534 people have browsed it

As one of the most popular languages ​​in the world, PHP can develop many programs, which are inseparable from our Internet life. So how to use PHP to send emails? This is also a question that some PHP novices want to explore. Now I will share with you an example of using phpmaill to send emails.

<?php
include "mail.php";
if (!empty($_POST[&#39;to&#39;])  && !empty($_POST[&#39;fromname&#39;]) && !empty($_POST[&#39;title&#39;]) && !empty($_POST[&#39;content&#39;])) {send_mail($_POST[&#39;to&#39;],$_POST[&#39;fromname&#39;],$_POST[&#39;title&#39;],$_POST[&#39;content&#39;]);
}
?>
<form action="#" method="post">
接收人:<input type="text" name="to" /><br>
发件人昵称:<input type="text" name="fromname" /><br>
标题:<input type="text" name="title" /><br>
内容:<input type="text" name="content" style="width:400;height:100;" /><br>
<input type="submit" value="提交" />
注:默认是用作者的QQ邮箱发送 请注意改成自己的数据
</form>
Copy after login
<?php 
header("content-type:text/html;charset=utf-8"); 
ini_set("magic_quotes_runtime",0); 
require &#39;php_mail/class.phpmailer.php&#39;; 
require &#39;php_mail/class.smtp.php&#39;; 
function send_mail($to,$fromname,$title,$content){
try { 
$mail = new PHPMailer(true); 
$mail->IsSMTP(); 
$mail->CharSet=&#39;UTF-8&#39;; //设置邮件的字符编码,这很重要,不然中文乱码 
$mail->SMTPAuth = true; //开启认证 
$mail->Port = 25; //端口请保持默认
$mail->Host = "smtp.qq.com"; //使用QQ邮箱发送
$mail->Username = "903215283@qq.com"; //这个可以替换成自己的邮箱
$mail->Password = "nelfobtugzckbdae"; //注意 这里是写smtp的授权码 写的不是QQ密码,此授权码不可用
//$mail->IsSendmail(); //如果没有sendmail组件就注释掉,否则出现“Could not execute: /var/qmail/bin/sendmail ”的错误提示 
$mail->AddReplyTo("903215283@qq.com","mckee");//回复地址 
$mail->From = "903215283@qq.com"; 
$mail->FromName = $fromname; 
$to = $to; 
$mail->AddAddress($to); 
$mail->Subject = $title; 
$mail->Body = $content;
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; //当邮件不支持html时备用显示,可以省略 
$mail->WordWrap = 80; // 设置每行字符串的长度 
//$mail->AddAttachment("f:/test.png"); //可以添加附件 
$mail->IsHTML(true); 
$mail->Send(); 
echo &#39;邮件已发送&#39;;
} catch (phpmailerException $e) { 
echo "邮件发送失败:".$e->errorMessage(); 
} 
return true;
}
//环境 PHP5.3亲测可用
?>
Copy after login

The effect is displayed:

How to send email using phpmaill

How about now that everyone has learned how to send emails with phpmaill? So congratulations on mastering another basic skill.

Related recommendations:

Powerful PHP email sending class

PHP simple email sending class

PHP email problem


The above is the detailed content of How to send email using phpmaill. 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