插件:批量发邮件程序

优质
小牛编辑
139浏览
2023-12-01
此脚本需要nsMailer.dll插件。

引用 SendMail.nsi 的内容:

!AddPluginDir .
!define PRODUCT_NAME "批量发邮件程序"
!define /date BUILD_DATE "%Y-%m-%d"

SetCompressor /SOLID lzma
SetCompressorDictSize 32

!include "MUI2.nsh"
!include "FileFunc.nsh"

!define PBM_GETRANGE 0x0407

!define MUI_CUSTOMFUNCTION_GUIINIT GUIInit

!define MUI_PAGE_HEADER_TEXT "路径选择"
!define MUI_PAGE_HEADER_SUBTEXT "请选择你要发送的文件所在文件夹。"
!define MUI_DIRECTORYPAGE_TEXT_TOP "如果文件比较大,发送时间会较长"
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "附件文件夹"
!insertmacro MUI_PAGE_DIRECTORY

!define MUI_PAGE_CUSTOMFUNCTION_SHOW InstShow
!define MUI_PAGE_HEADER_TEXT "正在发送"
!define MUI_PAGE_HEADER_SUBTEXT "正在发送,请稍候..."
!define FINISHHEADER_TEXT "任务完成"
!define FINISHHEADER_SUBTEXT "可以发送的文件都已发送完毕。"
!define MUI_PAGE_CUSTOMFUNCTION_LEAVE InstLeave
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE "SimpChinese"

Name "${PRODUCT_NAME}"
Caption "${PRODUCT_NAME}"
OutFile "SendMail.exe"
InstallDir "D:\Upload\"
ShowInstDetails show
BrandingText "Build ${BUILD_DATE} by gfm688"
SpaceTexts none
InstallButtonText "发送(&S)"
MiscButtonText "返回(&B)"

Var Pos
Var AddPos
Var retry
!define MaxReTry 3  ;定义最大重试次数

# 不用插件检查文件是否小于50M
; 小于50M则返回1, 大于50M则返回0
!macro IsLessthan50 File Result
	Push $R0
	Push $R1
	FileOpen $R0 "${File}" r
	FileSeek $R0 "" END $R1
	FileClose $R0
	${If} $R1  52428800
	    Push 0
	${Else}
	    Push 1
	${EndIf}
	Exch 2
	Pop $R0
	Pop $R1
	Pop ${Result}
!macroend
# 用了${Locate}或${GetSize}就会将System.dll打包进去, 而这个脚本实际上并没使用System.dll, 所以System.dll总不会释放出来

Section
StrCmp $1 "" SecEnd
${Locate} "$INSTDIR" "/L=F /G=0" "SendMail"
SecEnd:
Call InstFinish
SectionEnd

Function SendMail
SetDetailsPrint both
	!insertmacro IsLessthan50 "$R9" $R0
	${If} $R0 = 0
		DetailPrint "$R7大于50M, 跳过..."
		Goto continue
	${EndIf}
	DetailPrint "正在发送: $R7..."
trysend:
	nsMailer::InitConfig "smtp.qq.com" "Username" "Password"
	Pop $R0
	nsMailer::From "xxx@qq.com"
	nsMailer::To "xxx@qq.com"
	nsMailer::Subject "$R7"
	nsMailer::TextBody ""
	nsMailer::AddAttachment "$R9"
	nsMailer::Send
	Pop $R0
	${If} $R0 != 0
	    ${If} $retry = ${MaxReTry}
	   		DetailPrint "超过重试次数, $R7 发送失败!"
	        Goto continue
	    ${EndIf}
        DetailPrint "$R7 发送失败, 正在重试..."
        IntOp $retry $retry + 1
        Goto trysend
	${EndIf}
	DetailPrint "$R7 发送成功!"
continue:
    IntOp $Pos $Pos + $AddPos
	SendMessage $mui.InstFilesPage.ProgressBar ${PBM_SETPOS} $Pos 0
	Push $0
FunctionEnd

Function InstShow
	EnableWindow $mui.Button.Cancel 1
	${GetSize} "$INSTDIR" "/G=0" $0 $1 $2
	SendMessage $mui.InstFilesPage.ProgressBar ${PBM_GETRANGE} 0 0 $2
	IntOp $AddPos $2 / $1
	StrCpy $Pos 0
	StrCpy $retry 0
FunctionEnd

Function InstFinish
    SendMessage $mui.Button.Next ${WM_SETTEXT} 0 "STR:完成(&F)"
    EnableWindow $mui.Button.Back 1
    EnableWindow $mui.Button.Next 1
    EnableWindow $mui.Button.Cancel 0
    DetailPrint "$(^Completed)"
    !insertmacro MUI_HEADER_TEXT "${FINISHHEADER_TEXT}" "${FINISHHEADER_SUBTEXT}"
    NotifyIcon::Icon /NOUNLOAD "b" "${FINISHHEADER_TEXT}" "${FINISHHEADER_SUBTEXT}"
    SendMessage $mui.InstFilesPage.ProgressBar ${PBM_GETRANGE} 0 0 $0
    SendMessage $mui.InstFilesPage.ProgressBar ${PBM_SETPOS} $0 0
    Sleep 0xFFFFFF
FunctionEnd

Function InstLeave
    IfAbort +2
    Quit
FunctionEnd

Function GUIInit
 NotifyIcon::Icon /NOUNLOAD "iy" 103
FunctionEnd

Function .onGUIEnd
 NotifyIcon::Icon "r"
FunctionEnd

# 加个命令行参数,集成到右键菜单,再加个自定义页面配置就帅呆了^_^