找回密码
 加入
搜索
查看: 825|回复: 6

[AU3基础] function和function变量之间的通信如何转换

[复制链接]
发表于 2021-12-2 11:31:35 | 显示全部楼层 |阅读模式
悬赏30金钱已解决
如题,下面代码去掉function dog()之后就能正常收发邮件,小弟没学过其他编程,百度了半个月也搞不明白他们之间的关系。
我是想要把下面邮件的UDF转到其他function,然后加变量直接发送邮件。请问这样要怎么实现呀?
#include <SmtpMailer.au3>

$ID = @ComputerName
Func dog()
$MailTitle = "日志  " & $
$MailBody = $MailTitle
$recipients = "Example@qq.com"
$MailAtt = ""
$recipientCC = ""
SendMail($recipients, $MailTitle, $MailBody, $MailAtt, $recipientCC)
EndFunc

Func SendMail($recipients, $MailTitle, $MailBody, $MailAtt, $recipientCC)
$SmtpServer = "smtp.mxhichina.com" ; SMTP服务器address for the smtp-server to use - REQUIRED
$FromName = "" ; 发送人name from who the email was sent
$FromAddress = "Example@qq.com" ; 发送人地址address from where the mail should come - 139mail REQUIRED
$ToAddress = $recipients ; 收件人地址destination address of the email - REQUIRED
$Subject = $MailTitle ; 邮件标题subject from the email - can be anything you want it to be
$Body = $MailBody ; 邮件正文the messagebody from the mail - can be left blank but then you get a blank mail
$AttachFiles = $MailAtt ; 附件链接the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
$CcAddress = $recipientCC ; 抄送地址address for cc - leave blank if not needed
$BccAddress = "" ; 秘密抄送地址address for bcc - leave blank if not needed
$Importance = "Normal" ; 邮件重要性Send message priority: "High", "Normal", "Low"
$Username = "Example@qq.com" ; 用户名username for the account used from where the mail gets sent - REQUIRED
$Password = "########" ; 密码password for the account used from where the mail gets sent - REQUIRED
$IPPort = 25 ; port used for sending the mail
$ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS


Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
$rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password)
If @error Then
        MsgBox(0, "Error sending message", "Error code:" & @error & "  Description:" & $rc)
EndIf
Return $rc
EndFunc

最佳答案

查看完整内容

[au3]#include Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc") dog(@ComputerName) Func dog($ID) Local $MailTitle = "日志 " & $ID Local $MailBody = $MailTitle Local $recipients = "Example@qq.com" SendMail($recipients, $MailTitle, $MailBody, '', '') EndFunc ;==>dog Func SendMail($recipients, $MailTitle, $MailBody, $MailAtt, $recipientCC) Local $SmtpServer = "smtp.mxhichina.com" ...
发表于 2021-12-2 11:31:36 | 显示全部楼层
#include <SmtpMailer.au3>

Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

dog(@ComputerName)

Func dog($ID)
        Local $MailTitle = "日志  " & $ID
        Local $MailBody = $MailTitle
        Local $recipients = "Example@qq.com"
        SendMail($recipients, $MailTitle, $MailBody, '', '')
EndFunc   ;==>dog

Func SendMail($recipients, $MailTitle, $MailBody, $MailAtt, $recipientCC)
        Local $SmtpServer = "smtp.mxhichina.com" ; SMTP服务器address for the smtp-server to use - REQUIRED
        Local $FromName = "" ; 发送人name from who the email was sent
        Local $FromAddress = "Example@qq.com" ; 发送人地址address from where the mail should come - 139mail REQUIRED
        Local $ToAddress = $recipients ; 收件人地址destination address of the email - REQUIRED
        Local $Subject = $MailTitle ; 邮件标题subject from the email - can be anything you want it to be
        Local $Body = $MailBody ; 邮件正文the messagebody from the mail - can be left blank but then you get a blank mail
        Local $AttachFiles = $MailAtt ; 附件链接the file(s) you want to attach seperated with a ; (Semicolon) - leave blank if not needed
        Local $CcAddress = $recipientCC ; 抄送地址address for cc - leave blank if not needed
        Local $BccAddress = "" ; 秘密抄送地址address for bcc - leave blank if not needed
        Local $Importance = "Normal" ; 邮件重要性Send message priority: "High", "Normal", "Low"
        Local $Username = "Example@qq.com" ; 用户名username for the account used from where the mail gets sent - REQUIRED
        Local $Password = "########" ; 密码password for the account used from where the mail gets sent - REQUIRED
;~         $IPPort = 25 ; port used for sending the mail
;~         $ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS

        Local $rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password)
        If @error Then
                MsgBox(0, "Error sending message", "Error code:" & @error & "  Description:" & $rc)
                Return SetError(1, 0, 0)
        EndIf
        Return $rc
EndFunc   ;==>SendMail
Func MyErrFunc()
        Local $sErrInfo = "ErrSource: " & $oMyError.Source & @LF & "DESC: " & $oMyError.Description
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sErrInfo = ' & $sErrInfo & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
EndFunc   ;==>MyErrFunc
发表于 2021-12-2 12:05:00 | 显示全部楼层
这是自定义函数的语法问题,可以在帮助文档中索引输入 “Func” 就能找到相关知识
发表于 2021-12-2 16:35:12 | 显示全部楼层
这样来理解,一个完整的脚本结构,分为二个部分,一部分是func .... endfunc 及其之间的内容,我姑且叫它为一个udf函数,可能这个udf函数有多个.另一部份就是udf函数之外的内容,我姑且叫它为.

脚本代码运行从根开始逐行执行.执行过程中有call udf函数,那么执行这个udf函数之后返回到call后继续执行根内容,直到结束.如果脚本只有udf函数,没有根,那么不会执行任何代码.
咱们回到你发出的代码,当然,代码其实有错误,A版已经给你改正了.

因为你的脚本根代码中没有调用UDF函数,因此,声明的二个函数都不会调用.也不会达到预期的效果

执行流程看下图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 1金钱 +60 收起 理由
juno + 60 很给力!

查看全部评分

 楼主| 发表于 2021-12-3 18:02:31 | 显示全部楼层
感谢A神的帮助和回答,我查看了相关的帮助文档,FuncName ( $Functionvariable ) :A variable containing a Function whose name you want to retrieve.
我多练练这里面的关系和位置。万分感谢!




 楼主| 发表于 2021-12-3 18:03:14 | 显示全部楼层
感谢A神的帮助和回答,我查看了相关的帮助文档,FuncName ( $Functionvariable ) :A variable containing a Function whose name you want to retrieve.
我多练练这里面的关系和位置。万分感谢!




 楼主| 发表于 2021-12-3 18:35:06 | 显示全部楼层
tubaba 发表于 2021-12-2 16:35
这样来理解,一个完整的脚本结构,分为二个部分,一部分是func .... endfunc 及其之间的内容,我姑且叫它为 ...

感谢tubaba的耐心指导,原来我是一直想着如何把外面的变量输入到func里面去,不懂如何调用UDF函数。如果不是A版的正确代码和你的指引流程是真的不理解,当时我是想把变量放到func括号旁让后再在外面输入变量的值来输出,以为这样func就能知道这个变量&#128514;,现在才知道UDF函数是这样调用变量值的。Func(发件人,标题,文本)不输入的就单引号空开。调用出来就直接funcname(发件人,标题,‘’)输出。
万分感谢!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-12-22 22:31 , Processed in 0.127093 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表