建网站 发信息 做推广,wordpress创始人赚钱吗,企业所得税怎么算一般纳税人,如何在门户网站做搜索引擎需求很简单#xff0c;就是用户输入自己的邮箱后#xff0c;使用官方邮箱给用户发送替邮件模版 目录 前置邮件模版邮箱开启SMTP服务163邮箱163企业邮箱谷歌gmail邮箱腾讯企业邮箱-失败其他邮箱-未操作 邮件发送核心代码config.yaml配置读取邮件相关配置发送邮件 附录 前置
邮… 需求很简单就是用户输入自己的邮箱后使用官方邮箱给用户发送替邮件模版 目录 前置邮件模版邮箱开启SMTP服务163邮箱163企业邮箱谷歌gmail邮箱腾讯企业邮箱-失败其他邮箱-未操作 邮件发送核心代码config.yaml配置读取邮件相关配置发送邮件 附录 前置
邮件模版
邮件模版类似如下图发来的欢迎加入的模版
这里我们使用html格式存储模版上图源码如下
html langen-US xmlns:thhttp://www.w3.org/1999/xhtml
headmeta charsetUTF-8titleemail/titlestylea{color: #ECC94B !important;}.foot-text{color: rgba(0, 0, 0, 0.50) !important;}.im{color:rgba(0,0,0,0.86) !important;}.center {display: flex;justify-content: center;align-items: center;}/style
/head
body stylebackground-color: white;
div stylewidth: 640px;height: auto;border-radius: 5px;margin: 0 auto;position: relative; padding-bottom: 80px; padding-top: 20pxdivimg width640 srcfile:///Users/yanzhixing/Downloads/My/文档/HatchfulExport-All/linkedin_banner_image_2.png alt/divdiv stylecolor: rgba(0,0,0,0.86);font-family: Roboto,Century Gothic, Trebuchet MS, Hiragino Sans GB, 微软雅黑, Microsoft Yahei, Tahoma, Helvetica, Arial, SimSun, sans-serif;font-size: 16px;margin: 10px auto 0pxh2 stylefont-size: 20px;font-weight: 700; color: #000Dear KunZhi Fans,/h2divp stylemargin: 16px 0; Welcome to the family! We are glad to have you on board as a subscriber to my blog./p p stylefont-weight: 600; color: #000;Get the latest on a stylecolor: #000 hrefhttps://kunzhi.blog.csdn.net/CSDN/a and give me a follow if you havent already./pp stylemargin: 16px 0p stylefont-weight: 600; color: #000 About Me/pp stylemargin-bottom: 8pxFormer Alibaba programmer, now working for a blockchain company. He is a blogger in the domestic Internet circle, an expert blogger on Alibaba Cloud, and the manager of CSDN Hangzhou City Community. Welcome to communicate and win-win cooperation./p/p div stylemargin: 16px 0p stylefont-weight: 600; color: #000; Contact Me/pp style margin-bottom: 8pxShould you have any questions, require assistance, or want to dive deeper into any aspect, you can reach out to me through various channels:/pdivp stylemargin-bottom: 0; display: flex; margin-bottom: 4pxspan stylemargin: 0 8px●/spanspanWechat: Follow me a stylecolor: #ECC94B;text-decoration: none target_blankkunzhi96/a to communicate and learn together./spandiv classcenterimg width200 srcfile:///Users/yanzhixing/Downloads/My/私人/微信二维码.png alt/div/pp stylemargin: 0; display: flex; margin-bottom: 4pxspan stylemargin: 0 8px●/spanspanCSDN Blog: Visit my blog -a stylecolor: #ECC94B; text-decoration: none target_blank hrefhttps://kunzhi.blog.csdn.net/鲲志说/a for in-depth information about my learning content and technology sharing./span/pp stylemargin: 0; display: flex; margin-bottom: 4pxspan stylemargin: 0 8px●/spanspan Email: Feel free to contact us via email at span stylecolor: #ECC94B; opacity: 1;XXX163.com/span for any inquiries, feedback, or support needs./span/p/divp stylemargin-bottom: 0pxIm excited to embark on this journey with you./p/div pWarm regards,/ppKun Zhi/pdiv stylebackground-color: #F7F7F7; padding: 40px; text-align:center;p stylefont-size: 24px;font-weight: 700; color: #000; margin-bottom: 24px; margin-top: 0KunZhi/pp stylemargin-bottom: 8px; margin-top: 0This email was send to span stylecolor: #ECC94B; opacity: 1;xxxgmail.com/span/pp stylemargin-bottom: 24px; margin-top: 0You’ve received it because you’ve subscribed to our newsletter./pdiva stylecolor: rgba(0, 0, 0, 0.50); text-decoration: underline hrefunsubscribe_url/unsubscribe?emailunsubscribe_email classfoot-textUnsubscribe/a/div/div/div/div
/div
/body
/html邮箱开启SMTP服务
选择好官方邮箱来开启smtp服务这里我测试验证了163邮箱、163企业邮箱、谷歌gmail邮箱和腾讯企业邮箱但腾讯企业邮箱未成功后续会说明
163邮箱 登录163邮箱一定不要登录到简易版啊 登录后如下图所示点击上面导航栏的设置 按照下图顺序点击 POP3/SMTP/IMAP 开启SMTP服务
如下图我已经是开启状态了 5. 新增授权密码 如上图我们如果需要使用SMTP服务就一定要使用授权码而且这个密码只出现一次一定要记得保存好保存好先留着我们后面会用到
6.服务器地址及端口
我们使用的是SMTP服务则对应的服务器地址是smtp.163.com端口为25后面都会用到
自此163邮箱的smtp服务开启完毕可直接跳到邮件发送源码处继续即可 163企业邮箱
登录163企业邮箱
可以看到登录的是网易灵犀办公界面
登录后如下图所示点击上面导航栏的设置-账号与安全-客户端设置 进入客户端设置
可看到如下图
选择IMAP协议开启并设置客户端授权密码
新增授权密码 如上图生成授权码而且这个密码只出现一次一定要记得保存好保存好先留着我们后面会用到。 可以看到163企业邮箱不同的是有一个到期时间也就意味着到期了我们需要更换授权码这一点需要谨记 服务器地址及端口
我们使用的是SMTP服务则对应的服务器地址是smtphz.qiye.163.com端口为25后面都会用到
自此163企业邮箱的smtp服务开启完毕可直接跳到邮件发送源码处继续即可 谷歌gmail邮箱
登录 谷歌gmail邮箱点击设置-查看所有设置 点击转发和 POP/IMAP 标签页如下图进行启用设置并保存更改 创建和使用应用专用密码 前往您的 Google 帐号-选择安全性 在“登录 Google”下方选择两步验证 在页面底部选择应用专用密码进行点击 输入一个名称以帮助您记住在什么情况下使用应用专用密码如我的kunzhi-test 选择创建后会出现如下弹窗。设备的应用专用密码即生成了是指在设备上生成的 16 个字符的代码。也就是授权码而且这个密码只出现一次一定要记得保存好保存好先留着我们后面会用到。 选择完成记得应用名称对应的密码即可
服务器地址及端口
我们使用的是SMTP服务则对应的服务器地址是smtp.gmail.com端口为587后面都会用到 自此谷歌gmail邮箱的smtp服务开启完毕可直接跳到邮件发送源码处继续即可 腾讯企业邮箱-失败
腾讯企业邮箱失败的原因是因为一直没有找到授权码在哪里进行设置同时使用邮箱的密码作为授权码也无法发送邮件所以一直没有实现但是前面的操作步骤可以放出来供大家参考如果有人找到了腾讯企业邮箱的操作方法请一定要评论区给出解法。 企业微信管理后台 开启腾讯企业邮箱的POP/SMTP/IMAP服务 企业邮箱服务器及端口号设置
其他邮箱-未操作
这个链接的操作我并未实际执行需要的可以参考
发件服务设置-包含阿里云邮箱设置、Outlook邮箱设置 邮件发送核心代码
config.yaml配置
这里是发送邮件的邮箱相关配置这里就使用到之前只出现一次的授权码了以及对应的服务器地址和端口
email:
# 163邮箱template_dir: /test.htmlsmtp_server: smtp.163.comsmtp_port: 25smtp_user: KunZhismtp_password: 授权码from_email: XXXX163.com# 腾讯企业邮箱# smtp_server: smtp.exmail.qq.com# smtp_port: 465# smtp_user: KunZhi# smtp_password: 授权码# from_email: XXXX# 163企业邮箱# smtp_server: smtphz.qiye.163.com# smtp_port: 25# smtp_user: KunZhi# smtp_password: 授权码# from_email: XXXX# gmail
# smtp_server: smtp.gmail.com
# smtp_port: 587
# smtp_user: KunZhi
# smtp_password: 授权码
# from_email: XXXXgmail.com这里的smtp_user如下图就是展示的发件人名称
读取邮件相关配置 //读取邮件相关配置err sendWelcomeEmail(Cfg{SenderAddress: viper.GetString(email.from_email),SenderName: viper.GetString(email.smtp_user),SenderPwd: viper.GetString(email.smtp_password),MailServerHost: viper.GetString(email.smtp_server),MailServerPort: viper.GetString(email.smtp_port),}, viper.GetString(email.from_email), toEmail, unsubscribeUrl)if err ! nil {errors.Errorf(Failed to send email: , toEmail, err)responseData : gin.H{message: Failed to send email: toEmail,code: -1,}c.JSON(http.StatusOK, responseData)return err}发送邮件 func sendWelcomeEmail(cfg Cfg, template string, recipientEmail, unsubscribeUrl string) error {currentDir, err2 : os.Getwd()if err2 ! nil {log.Fatalf(Failed to get current working directory: %v, err2)}//获取邮件模版htmlContent, err3 : ioutil.ReadFile(currentDir viper.GetString(email.template_dir))if err3 ! nil {fmt.Println(Failed to read HTML file:, err3)return err3}// 邮件标题subject : Your Journey Begins Heremessage : string(htmlContent)// 需要替换邮件模版中的字符串映射replacements : map[string]string{xxxgmail.com: recipientEmail,header_img: viper.GetString(img.header_img),}// 替换字符串for oldStr, newStr : range replacements {message strings.Replace(message, oldStr, newStr, -1) // -1 表示替换所有匹配项}// 连接到 SMTP 服务器并发送电子邮件auth : smtp.PlainAuth(, cfg.SenderAddress, cfg.SenderPwd, cfg.MailServerHost)to : []string{recipientEmail}msg : []byte(To: recipientEmail \r\n Subject: subject \r\n From: cfg.SenderName cfg.SenderAddress \r\n MIME-Version: 1.0\r\n Content-Type: text/html; charsetutf-8\r\n // 将内容类型设置为 HTML\r\n message)//发送邮件err : smtp.SendMail(cfg.MailServerHost:cfg.MailServerPort, auth, cfg.SenderAddress, to, msg)if err ! nil {fmt.Println(failed to send email:, err)return err}return nil
}如上代码可以实现SMTP邮件发送订阅功能如有任何问题欢迎私聊或评论区留言 附录
163帮助中心163邮箱无法发送邮件发生退信问题 550 User has no permission以及554, DT:SPM的解决办法163企业邮箱帮助中心-灵犀办公客户端的设置方法谷歌邮箱-使用应用专用密码登录