summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorLuzhenping <luzhenping@zthsoft.net>2015-06-11 01:59:30 (GMT)
committer Luzhenping <luzhenping@zthsoft.net>2015-06-11 01:59:30 (GMT)
commitc2733b869c68fbc681fc31be0849d2bb56caafb9 (patch)
tree41679abb599b6ca8410ea2ed0604a1b340677290
downloadlibxemail-c2733b869c68fbc681fc31be0849d2bb56caafb9.zip
libxemail-c2733b869c68fbc681fc31be0849d2bb56caafb9.tar.gz
libxemail-c2733b869c68fbc681fc31be0849d2bb56caafb9.tar.bz2
libxemail first push.
-rw-r--r--.cproject61
-rw-r--r--.project32
-rw-r--r--.rcsetting15
-rw-r--r--.settings/org.eclipse.cdt.core.prefs6
-rw-r--r--Makefile230
-rw-r--r--config.mk34
-rw-r--r--include/xemail.h223
-rw-r--r--lib/xemail.c1419
8 files changed, 2020 insertions, 0 deletions
diff --git a/.cproject b/.cproject
new file mode 100644
index 0000000..a22b383
--- /dev/null
+++ b/.cproject
@@ -0,0 +1,61 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
+ <storageModule moduleId="org.eclipse.cdt.core.settings">
+ <cconfiguration id="com.sylixos.realcoder.toolChain.armsylixeabi.1307549435">
+ <storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="com.sylixos.realcoder.toolChain.armsylixeabi.1307549435" moduleId="org.eclipse.cdt.core.settings" name="Default">
+ <macros>
+ <stringMacro name="SYLIXOS_TOOLCHAIN_VER" type="VALUE_TEXT" value="4.9.3"/>
+ <stringMacro name="SYLIXOS_TOOLCHAIN_PATH" type="VALUE_PATH_DIR" value="D:\ZTHSOFT\arm-sylixos-toolchain"/>
+ <stringMacro name="SYLIXOS_TOOLCHAIN_PREFIX" type="VALUE_TEXT" value="arm-sylixos-eabi"/>
+ <stringMacro name="SYLIXOS_BASE_PATH" type="VALUE_PATH_DIR" value="E:/workspace/sylixos_base"/>
+ </macros>
+ <externalSettings/>
+ <extensions>
+ <extension id="org.eclipse.cdt.core.GNU_ELF" point="org.eclipse.cdt.core.BinaryParser"/>
+ <extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+ <extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+ <extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+ <extension id="org.eclipse.cdt.core.MakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+ <extension id="org.eclipse.cdt.core.VCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+ <extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
+ <extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
+ </extensions>
+ </storageModule>
+ <storageModule moduleId="cdtBuildSystem" version="4.0.0">
+ <configuration artifactName="${ProjName}" buildProperties="" description="" id="com.sylixos.realcoder.toolChain.armsylixeabi.1307549435" name="Default" parent="org.eclipse.cdt.build.core.emptycfg">
+ <folderInfo id="com.sylixos.realcoder.toolChain.armsylixeabi.1307549435.616393721" name="/" resourcePath="">
+ <toolChain id="com.sylixos.realcoder.toolChain.armsylixeabi.1879518038" name="arm-sylixos-toolchain" superClass="com.sylixos.realcoder.toolChain.armsylixeabi">
+ <targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.GNU_ELF" id="com.sylixos.realcoder.targetPlatform.armsylixoseabi.1502637610" isAbstract="false" name="SylixOS TagetPlagform" osList="win32" superClass="com.sylixos.realcoder.targetPlatform.armsylixoseabi"/>
+ <builder id="com.sylixos.realcoder.builder.armsylixeabi.738030869" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="SylixOS Builder" superClass="com.sylixos.realcoder.builder.armsylixeabi"/>
+ <tool id="com.sylixos.realcoder.tool.armsylixeabi.255610179" name="arm-sylixos-eabi-gcc" superClass="com.sylixos.realcoder.tool.armsylixeabi">
+ <option id="com.sylixos.realcoder.option.armsylixoseabi.include.1309924073" name="Include paths (-I)" superClass="com.sylixos.realcoder.option.armsylixoseabi.include" valueType="includePath">
+ <listOptionValue builtIn="false" value="&quot;${SYLIXOS_BASE_PATH}/libsylixos/SylixOS&quot;"/>
+ <listOptionValue builtIn="false" value="&quot;${SYLIXOS_BASE_PATH}/libsylixos/SylixOS/include&quot;"/>
+ <listOptionValue builtIn="false" value="&quot;${SYLIXOS_BASE_PATH}/libsylixos/SylixOS/include/inet&quot;"/>
+ <listOptionValue builtIn="false" value="&quot;${SYLIXOS_TOOLCHAIN_PATH}/${SYLIXOS_TOOLCHAIN_PREFIX}/include&quot;"/>
+ <listOptionValue builtIn="false" value="&quot;${SYLIXOS_TOOLCHAIN_PATH}/lib/gcc/${SYLIXOS_TOOLCHAIN_PREFIX}/${SYLIXOS_TOOLCHAIN_VER}/include&quot;"/>
+ <listOptionValue builtIn="false" value="&quot;${workspace_loc:/libcurl/curl-7.40.0/include}&quot;"/>
+ </option>
+ <option id="com.sylixos.realcoder.option.armsylixoseabi.DefinedSymbols.929438738" name="Defined symbols(-D)" superClass="com.sylixos.realcoder.option.armsylixoseabi.DefinedSymbols" valueType="definedSymbols">
+ <listOptionValue builtIn="false" value="__GNUC__=4"/>
+ <listOptionValue builtIn="false" value="SYLIXOS"/>
+ </option>
+ <inputType id="com.sylixos.realcoder.inputType.armnsylixeabic.1963019236" name="SylixOS Input C" superClass="com.sylixos.realcoder.inputType.armnsylixeabic"/>
+ <inputType id="com.sylixos.realcoder.inputType.armnsylixeabicpp.1772359768" name="SylixOS Input CPP" superClass="com.sylixos.realcoder.inputType.armnsylixeabicpp"/>
+ </tool>
+ </toolChain>
+ </folderInfo>
+ </configuration>
+ </storageModule>
+ <storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
+ </cconfiguration>
+ </storageModule>
+ <storageModule moduleId="cdtBuildSystem" version="4.0.0">
+ <project id="libxemail.null.1950022481" name="libxemail"/>
+ </storageModule>
+ <storageModule moduleId="scannerConfiguration">
+ <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
+ </storageModule>
+ <storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
+ <storageModule moduleId="refreshScope"/>
+</cproject>
diff --git a/.project b/.project
new file mode 100644
index 0000000..ce53203
--- /dev/null
+++ b/.project
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>libxemail</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
+ <triggers>clean,full,incremental,</triggers>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
+ <triggers>full,incremental,</triggers>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>com.sylixos.realcoder.SylixOSBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.cdt.core.cnature</nature>
+ <nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
+ <nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
+ <nature>com.sylixos.realcoder.SylixOSProject</nature>
+ </natures>
+</projectDescription>
diff --git a/.rcsetting b/.rcsetting
new file mode 100644
index 0000000..92fd97b
--- /dev/null
+++ b/.rcsetting
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="GB2312" standalone="no"?>
+<SylixOSSetting>
+<UploadSetting HostName="192.168.1.33" Password="root" UserName="root">
+<PathPair LocalPath="E:\workspace\libxemail\Debug\libxemail.so" RemotePath="/lib/libxemail.so"/>
+</UploadSetting>
+<BaseSetting ProjectType="DynamicLibProject">
+<BaseCfgPair Macro="SylixOS_Base_Path" Value="E:/workspace/sylixos_base"/>
+<BaseCfgPair Macro="SylixOS_Toolchain" Value="arm-sylixos-eabi"/>
+<BaseCfgPair Macro="SylixOS_Toolchain_Path" Value="D:\ZTHSOFT\arm-sylixos-toolchain"/>
+<BaseCfgPair Macro="SylixOS_Toolchain_Ver" Value="4.9.3"/>
+<BaseCfgPair Macro="SylixOS_Debug_Level" Value="debug"/>
+<BaseCfgPair Macro="Create_Date" Value="2015 年06 月06 日"/>
+</BaseSetting>
+<BuildConfigration/>
+</SylixOSSetting>
diff --git a/.settings/org.eclipse.cdt.core.prefs b/.settings/org.eclipse.cdt.core.prefs
new file mode 100644
index 0000000..856433f
--- /dev/null
+++ b/.settings/org.eclipse.cdt.core.prefs
@@ -0,0 +1,6 @@
+eclipse.preferences.version=1
+environment/project/com.sylixos.realcoder.toolChain.armsylixeabi.1307549435/PATH/delimiter=;
+environment/project/com.sylixos.realcoder.toolChain.armsylixeabi.1307549435/PATH/operation=replace
+environment/project/com.sylixos.realcoder.toolChain.armsylixeabi.1307549435/PATH/value=D\:\\ZTHSOFT\\arm-sylixos-toolchain\\bin;D\:\\ZTHSOFT\\arm-sylixos-toolchain\\yagarto-tools\\bin;D\:\\ZTHSOFT\\arm-sylixos-toolchain\\yagarto-tools\\bin;D\:\\ZTHSOFT\\arm-sylixos-toolchain\\bin;C\:\\ProgramData\\Oracle\\Java\\javapath;C\:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C\:\\Windows\\system32;C\:\\Windows;C\:\\Windows\\System32\\Wbem;C\:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C\:\\Program Files (x86)\\Windows7Master;C\:\\Program Files (x86)\\ATI Technologies\\ATI.ACE\\Core-Static;C\:\\Program Files\\TortoiseGit\\bin;C\:\\Program Files (x86)\\ATMEL Corporation\\AT91-ISP v1.13\\Library\\;C\:\\Program Files (x86)\\ATMEL Corporation\\AT91-ISP v1.13\\sam-ba 2.9
+environment/project/com.sylixos.realcoder.toolChain.armsylixeabi.1307549435/append=true
+environment/project/com.sylixos.realcoder.toolChain.armsylixeabi.1307549435/appendContributed=true
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..0866dcd
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,230 @@
+#*********************************************************************************************************
+# libxemail Makefile
+# target -> libxemail.a
+# libxemail.so
+#*********************************************************************************************************
+
+#*********************************************************************************************************
+# include config.mk
+#*********************************************************************************************************
+CONFIG_MK_EXIST = $(shell if [ -f ../config.mk ]; then echo exist; else echo notexist; fi;)
+ifeq ($(CONFIG_MK_EXIST), exist)
+include ../config.mk
+else
+CONFIG_MK_EXIST = $(shell if [ -f config.mk ]; then echo exist; else echo notexist; fi;)
+ifeq ($(CONFIG_MK_EXIST), exist)
+include config.mk
+else
+CONFIG_MK_EXIST =
+endif
+endif
+
+#*********************************************************************************************************
+# check configure
+#*********************************************************************************************************
+check_defined = \
+ $(foreach 1,$1,$(__check_defined))
+__check_defined = \
+ $(if $(value $1),, \
+ $(error Undefined $1$(if $(value 2), ($(strip $2)))))
+
+$(call check_defined, CONFIG_MK_EXIST, Please configure this project in RealCoder or \
+create a config.mk file!)
+$(call check_defined, SYLIXOS_BASE_PATH, SylixOS base project path)
+$(call check_defined, TOOLCHAIN_PREFIX, the prefix name of toolchain)
+$(call check_defined, DEBUG_LEVEL, debug level(debug or release))
+
+#*********************************************************************************************************
+# configure area you can set the following config to you own system
+# FPUFLAGS (-mfloat-abi=softfp -mfpu=vfpv3 ...)
+# CPUFLAGS (-mcpu=arm920t ...)
+# NOTICE: libsylixos, BSP and other kernel modules projects CAN NOT use vfp!
+#*********************************************************************************************************
+FPUFLAGS =
+CPUFLAGS = -mcpu=arm920t $(FPUFLAGS)
+
+#*********************************************************************************************************
+# toolchain select
+#*********************************************************************************************************
+CC = $(TOOLCHAIN_PREFIX)gcc
+CXX = $(TOOLCHAIN_PREFIX)g++
+AS = $(TOOLCHAIN_PREFIX)gcc
+AR = $(TOOLCHAIN_PREFIX)ar
+LD = $(TOOLCHAIN_PREFIX)g++
+
+#*********************************************************************************************************
+# do not change the following code
+# buildin internal application source
+#*********************************************************************************************************
+#*********************************************************************************************************
+# src(s) file
+#*********************************************************************************************************
+SRCS = \
+lib/xemail.c
+
+#*********************************************************************************************************
+# build path
+#*********************************************************************************************************
+ifeq ($(DEBUG_LEVEL), debug)
+OUTDIR = Debug
+else
+OUTDIR = Release
+endif
+
+OUTPATH = ./$(OUTDIR)
+OBJPATH = $(OUTPATH)/obj
+DEPPATH = $(OUTPATH)/dep
+
+#*********************************************************************************************************
+# target
+#*********************************************************************************************************
+LIB = $(OUTPATH)/libxemail.a
+DLL = $(OUTPATH)/libxemail.so
+
+#*********************************************************************************************************
+# objects
+#*********************************************************************************************************
+OBJS = $(addprefix $(OBJPATH)/, $(addsuffix .o, $(basename $(SRCS))))
+DEPS = $(addprefix $(DEPPATH)/, $(addsuffix .d, $(basename $(SRCS))))
+
+#*********************************************************************************************************
+# include path
+#*********************************************************************************************************
+INCDIR = -I"$(SYLIXOS_BASE_PATH)/libsylixos/SylixOS"
+INCDIR += -I"$(SYLIXOS_BASE_PATH)/libsylixos/SylixOS/include"
+INCDIR += -I"$(SYLIXOS_BASE_PATH)/libsylixos/SylixOS/include/inet"
+
+INCDIR += -I"include"
+INCDIR += -I"../libcurl/curl-7.40.0/include"
+
+#*********************************************************************************************************
+# compiler preprocess
+#*********************************************************************************************************
+DSYMBOL = -DSYLIXOS
+DSYMBOL += -DSYLIXOS_LIB
+
+#*********************************************************************************************************
+# depend dynamic library
+#*********************************************************************************************************
+DEPEND_DLL = -lcurl
+
+#*********************************************************************************************************
+# depend dynamic library search path
+#*********************************************************************************************************
+DEPEND_DLL_PATH = -L"../libcurl/$(OUTDIR)"
+
+#*********************************************************************************************************
+# compiler optimize
+#*********************************************************************************************************
+ifeq ($(DEBUG_LEVEL), debug)
+OPTIMIZE = -O0 -g3 -gdwarf-2
+else
+OPTIMIZE = -O2 -g1 -gdwarf-2 # Do NOT use -O3 and -Os
+endif # -Os is not align for function
+ # loop and jump.
+#*********************************************************************************************************
+# depends and compiler parameter (cplusplus in kernel MUST NOT use exceptions and rtti)
+#*********************************************************************************************************
+DEPENDFLAG = -MM
+CXX_EXCEPT = -fno-exceptions -fno-rtti
+COMMONFLAGS = $(CPUFLAGS) $(OPTIMIZE) -Wall -fmessage-length=0 -fsigned-char -fno-short-enums
+ASFLAGS = -x assembler-with-cpp $(DSYMBOL) $(INCDIR) $(COMMONFLAGS) -c
+CFLAGS = $(DSYMBOL) $(INCDIR) $(COMMONFLAGS) -fPIC -c
+CXXFLAGS = $(DSYMBOL) $(INCDIR) $(CXX_EXCEPT) $(COMMONFLAGS) -fPIC -c
+ARFLAGS = -r
+
+#*********************************************************************************************************
+# define some useful variable
+#*********************************************************************************************************
+DEPEND = $(CC) $(DEPENDFLAG) $(CFLAGS)
+DEPEND.d = $(subst -g ,,$(DEPEND))
+COMPILE.S = $(AS) $(ASFLAGS)
+COMPILE_VFP.S = $(AS) $(ASFLAGS)
+COMPILE.c = $(CC) $(CFLAGS)
+COMPILE.cxx = $(CXX) $(CXXFLAGS)
+
+#*********************************************************************************************************
+# target
+#*********************************************************************************************************
+all: $(LIB) $(DLL)
+ @echo create "$(LIB) $(DLL)" success.
+
+#*********************************************************************************************************
+# include depends
+#*********************************************************************************************************
+ifneq ($(MAKECMDGOALS), clean)
+ifneq ($(MAKECMDGOALS), clean_project)
+sinclude $(DEPS)
+endif
+endif
+
+#*********************************************************************************************************
+# create depends files
+#*********************************************************************************************************
+$(DEPPATH)/%.d: %.c
+ @echo creating $@
+ @if [ ! -d "$(dir $@)" ]; then mkdir -p "$(dir $@)"; fi
+ @rm -f $@; \
+ echo -n '$@ $(addprefix $(OBJPATH)/, $(dir $<))' > $@; \
+ $(DEPEND.d) $< >> $@ || rm -f $@; exit;
+
+$(DEPPATH)/%.d: %.cpp
+ @echo creating $@
+ @if [ ! -d "$(dir $@)" ]; then mkdir -p "$(dir $@)"; fi
+ @rm -f $@; \
+ echo -n '$@ $(addprefix $(OBJPATH)/, $(dir $<))' > $@; \
+ $(DEPEND.d) $< >> $@ || rm -f $@; exit;
+
+#*********************************************************************************************************
+# compile source files
+#*********************************************************************************************************
+$(OBJPATH)/%.o: %.S
+ @if [ ! -d "$(dir $@)" ]; then mkdir -p "$(dir $@)"; fi
+ $(COMPILE.S) $< -o $@
+
+$(OBJPATH)/%.o: %.c
+ @if [ ! -d "$(dir $@)" ]; then mkdir -p "$(dir $@)"; fi
+ $(COMPILE.c) $< -o $@
+
+$(OBJPATH)/%.o: %.cpp
+ @if [ ! -d "$(dir $@)" ]; then mkdir -p "$(dir $@)"; fi
+ $(COMPILE.cxx) $< -o $@
+
+#*********************************************************************************************************
+# link libxemail.a object files
+#*********************************************************************************************************
+$(LIB): $(OBJS)
+ $(AR) $(ARFLAGS) $(LIB) $(OBJS)
+
+#*********************************************************************************************************
+# link libxemail.so object files
+#*********************************************************************************************************
+$(DLL): $(OBJS)
+ $(LD) $(CPUFLAGS) -nostdlib -fPIC -shared -o $(DLL) $(OBJS) \
+ $(DEPEND_DLL_PATH) $(DEPEND_DLL) -lm -lgcc
+
+#*********************************************************************************************************
+# clean
+#*********************************************************************************************************
+.PHONY: clean
+.PHONY: clean_project
+
+#*********************************************************************************************************
+# clean objects
+#*********************************************************************************************************
+clean:
+ -rm -rf $(LIB)
+ -rm -rf $(DLL)
+ -rm -rf $(OBJS)
+ -rm -rf $(OBJPATH)
+ -rm -rf $(DEPPATH)
+
+#*********************************************************************************************************
+# clean project
+#*********************************************************************************************************
+clean_project:
+ -rm -rf $(OUTPATH)
+
+#*********************************************************************************************************
+# END
+#*********************************************************************************************************
diff --git a/config.mk b/config.mk
new file mode 100644
index 0000000..4fd2261
--- /dev/null
+++ b/config.mk
@@ -0,0 +1,34 @@
+#*********************************************************************************************************
+#
+# 中国软件开源组织
+#
+# 嵌入式实时操作系统
+#
+# SylixOS(TM) LW : long wing
+#
+# Copyright All Rights Reserved
+#
+#--------------文件信息--------------------------------------------------------------------------------
+#
+# 文 件 名: config.mk
+#
+# 创 建 人: RealCoder
+#
+# 文件创建日期: 2015 年06 月06 日
+#
+# 描 述: 本文件由RealCoder生成,用于配置Makefile功能,请勿手动修改
+#*********************************************************************************************************
+#*********************************************************************************************************
+# SylixOS Base Project path
+#*********************************************************************************************************
+SYLIXOS_BASE_PATH = E:/workspace/sylixos_base
+
+#*********************************************************************************************************
+# Toolchain prefix
+#*********************************************************************************************************
+TOOLCHAIN_PREFIX = arm-sylixos-eabi-
+
+#*********************************************************************************************************
+# Debug options (debug or release)
+#*********************************************************************************************************
+DEBUG_LEVEL = debug \ No newline at end of file
diff --git a/include/xemail.h b/include/xemail.h
new file mode 100644
index 0000000..0281759
--- /dev/null
+++ b/include/xemail.h
@@ -0,0 +1,223 @@
+/*********************************************************************************************************
+**
+** 中国软件开源组织
+**
+** 嵌入式实时操作系统
+**
+** SylixOS(TM) LW : long wing
+**
+** Copyright All Rights Reserved
+**
+**--------------文件信息--------------------------------------------------------------------------------
+**
+** 文 件 名: z-email.h
+**
+** 创 建 人: Lu.Zhenping (卢振平)
+**
+** 文件创建日期: 2015 年 05 月 12 日
+**
+** 描 述: zthsoft email 库
+*********************************************************************************************************/
+#ifndef __XEMAIL_H
+#define __XEMAIL_H
+/*********************************************************************************************************
+ 宏定义
+*********************************************************************************************************/
+#define Z_DBG 0
+/*********************************************************************************************************
+ 外部指针声明
+*********************************************************************************************************/
+typedef struct zthsoft_email *PZEMAIL;
+
+/*********************************************************************************************************
+ 外部指针声明
+*********************************************************************************************************/
+typedef PVOID (*zemailOpenFn)(PVOID pvData);
+typedef size_t (*zemailReadFn)(PVOID pvHandle, PVOID pvBuf, size_t stLen);
+typedef VOID (*zemailCloseFn)(PVOID pvHandle);
+typedef VOID (*zemailFreeDataFn)(PVOID pvData);
+
+/*********************************************************************************************************
+** 函数名称: xemailSetFrom
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailSetFrom (PZEMAIL pzemail, PCHAR pcFrom);
+
+/*********************************************************************************************************
+** 函数名称: xemailGetFrom
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PCHAR xemailGetFrom (PZEMAIL pzemail);
+
+/*********************************************************************************************************
+** 函数名称: xemailAddBCC
+** 功能描述: email 增加隐秘抄送者 (BCC: )
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddBCC(PZEMAIL pzemail, PCHAR pcBCC);
+
+/*********************************************************************************************************
+** 函数名称: xemailAddCC
+** 功能描述: email 增加抄送者 (CC: )
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddCC(PZEMAIL pzemail, PCHAR pcCC);
+
+/*********************************************************************************************************
+** 函数名称: xemailAddTO
+** 功能描述: email 增加接收者 (TO: )
+** 输 入 : pzemail : email 结构
+** pcTo : 接收者字符串
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddTO(PZEMAIL pzemail, PCHAR pcTo);
+
+/*********************************************************************************************************
+** 函数名称: xemailSetSubject
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailSetSubject(PZEMAIL pzemail, PCHAR pcSubject);
+
+/*********************************************************************************************************
+** 函数名称: xemailGetSubject
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PCHAR xemailGetSubject(PZEMAIL pzemail);
+
+/*********************************************************************************************************
+** 函数名称: xemailAddHeader
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddHeader(PZEMAIL pzemail, PCHAR pcHeader);
+
+/*********************************************************************************************************
+** 函数名称: xemailSetMailBody
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailSetMailBody(PZEMAIL pzemail, PCHAR pcBody);
+
+/*********************************************************************************************************
+** 函数名称: xemailAddMailBodyByType
+** 功能描述: 设置指定类型的邮件内容(如: "text/html" 类型的内容)
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddMailBodyByType(PZEMAIL pzemail,
+ PCHAR pcMimeType,
+ PCHAR pcData,
+ size_t stLen,
+ INT iMustFree);
+
+/*********************************************************************************************************
+** 函数名称: xemailAddAttachmentFile
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddAttachmentFile(PZEMAIL pzemail, PCHAR pcPath, PCHAR pcMime);
+
+/*********************************************************************************************************
+** 函数名称: xemailDelAttachmentFile
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailDelAttachmentFile(PZEMAIL pzemail, PCHAR pcPath);
+
+/*********************************************************************************************************
+** 函数名称: xemailDestroy
+** 功能描述: email 销毁主函数
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailDestroy(PZEMAIL pzemail);
+
+/*********************************************************************************************************
+** 函数名称: xemailCreate
+** 功能描述: email 创建主函数
+** 输 入 : pcFrom : 发送者
+** pcSubject : 邮件主题名
+** 输 出 : 创建成功的 email 结构
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PZEMAIL xemailCreate(PCHAR pcFrom, PCHAR pcSubject);
+
+/*********************************************************************************************************
+** 函数名称: xemailSend
+** 功能描述: email 发送主函数
+** 输 入 : pzemail : EMAIL 主结构
+** pcSmtpServer : smtp server
+** pcUserName : 用户名
+** pcPassword : 密码
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块: CURL 库
+*********************************************************************************************************/
+INT xemailSend(PZEMAIL pzemail, PCHAR pcSmtpServer, PCHAR pcUserName, PCHAR pcPassword);
+
+/*********************************************************************************************************
+** 函数名称: xemailDebugMode
+** 功能描述: 开启 DEBUG 模式
+** 输 入 : pzemail : EMAIL 结构
+** fp : log 输出文件
+** 输 出 : 版本
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailDebugMode(PZEMAIL pzemail, FILE *fp);
+
+/*********************************************************************************************************
+** 函数名称: xemailVersion
+** 功能描述: 获得 xemail 版本
+** 输 入 : NONE
+** 输 出 : 版本
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PCHAR xemailVersion(VOID);
+
+#endif
+/*********************************************************************************************************
+ END
+*********************************************************************************************************/
diff --git a/lib/xemail.c b/lib/xemail.c
new file mode 100644
index 0000000..09069ad
--- /dev/null
+++ b/lib/xemail.c
@@ -0,0 +1,1419 @@
+/*********************************************************************************************************
+**
+** 中国软件开源组织
+**
+** 嵌入式实时操作系统
+**
+** SylixOS(TM) LW : long wing
+**
+** Copyright All Rights Reserved
+**
+**--------------文件信息--------------------------------------------------------------------------------
+**
+** 文 件 名: xemail.c
+**
+** 创 建 人: Lu.Zhenping (卢振平)
+**
+** 文件创建日期: 2015 年 05 月 12 日
+**
+** 描 述: zthsoft email 库
+*********************************************************************************************************/
+#include <SylixOS.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libgen.h>
+#include <time.h>
+#include <curl/curl.h>
+
+#include "xemail.h"
+/*********************************************************************************************************
+ 宏定义
+*********************************************************************************************************/
+#define ZEMAIL_VERSION "xemail-0.1.0rc"
+#define ZEMAIL_NEWLINE "\r\n"
+#define ZEMAIL_NEWLINE_LEN (2)
+#define ZEMAIL_MIME_LINE_WIDTH (72)
+#define ZEMAIL_BODY_BUFFER_SIZE (256)
+#define ZEMAIL_DEFAULT_MIME_TYPE ("text/plain")
+
+#define ZEMAIL_LOCK()
+#define ZEMAIL_UNLOCK()
+#define ZEMAIL_SAFTFREE(a) \
+ if (a) {\
+ free(a); \
+ }
+
+#if Z_DBG > 0
+#define ZEMAIL_DBG(fmt, arg...) \
+ fprintf(stdout, "%s:%d "fmt, __func__, __LINE__, ##arg)
+#endif
+
+#define ZEMAIL_ERR(fmt, arg...) \
+ fprintf(stderr, "%s:%d"fmt, __func__, __LINE__, ##arg)
+
+/*********************************************************************************************************
+ ZEMAIL 错误号定义
+*********************************************************************************************************/
+enum {
+ ZEMAIL_NO_ERR = ERROR_NONE,
+ ZEMAIL_MEMORY_ALLOC_ERR,
+ ZEMAIL_VAL_INVALID,
+};
+/*********************************************************************************************************
+ 状态定义
+*********************************************************************************************************/
+typedef enum {
+ ZEMAIL_INIT, /* email 初始化状态 */
+ ZEMAIL_HEADER, /* 读取邮件头 */
+ ZEMAIL_BODY, /* 读取邮件体 */
+ ZEMAIL_BODY_DONE,
+ ZEMAIL_ATTACHMENT, /* 读取附件        */
+ ZEMAIL_END,
+ ZEMAIL_DONE,
+} ZEMAIL_STATE;
+/*********************************************************************************************************
+ LIST 结构体定义
+*********************************************************************************************************/
+typedef struct zthsoft_info {
+ struct zthsoft_info *ZEMAILINFO_next;
+ PCHAR ZEMAILINFO_pcData;
+} ZEMAILINFO, *PZEMAILINFO;
+/*********************************************************************************************************
+ 附件结构体定义
+*********************************************************************************************************/
+typedef struct zthsoft_attachment {
+ struct zthsoft_attachment *ZEMAILATTACH_next;
+ PCHAR ZEMAILATTACH_pcFileName;
+ PCHAR ZEMAILATTACH_pcMimeType;
+ PVOID ZEMAILATTACH_pvFileData;
+
+ PVOID ZEMAILATTACH_pvHandle;
+ zemailOpenFn ZEMAILATTACH_open;
+ zemailReadFn ZEMAILATTACH_read;
+ zemailCloseFn ZEMAILATTACH_close;
+ zemailFreeDataFn ZEMAILATTACH_free;
+} ZEMAILATTACH, *PZEMAILATTACH;
+/*********************************************************************************************************
+ 附件结构体定义
+*********************************************************************************************************/
+typedef struct zthsoft_memory {
+ PCHAR ZEMAILM_pcData;
+ size_t ZEMAILM_stLen;
+ INT ZEMAILM_iMustFree;
+} ZEMAILM;
+
+typedef struct zthsoft_handle {
+ PCHAR ZEMAILH_pcData;
+ size_t ZEMAILH_stLen; /* 数据总长度 */
+ size_t ZEMAILH_stPos; /* 当前读写的位置 */
+} ZEMAILH;
+/*********************************************************************************************************
+ 主结构体定义
+*********************************************************************************************************/
+typedef struct zthsoft_email {
+ ZEMAIL_STATE ZEMAIL_state; /* 状态 */
+ time_t ZEMAIL_tTimestamp; /* 时间戳 */
+ PCHAR ZEMAIL_pcFrom; /* 邮件发送者 */
+
+ PZEMAILINFO ZEMAIL_plineHeaderto; /* 接受者链头 */
+ PZEMAILINFO ZEMAIL_plineHeadercc; /* 抄送者链头 */
+ PZEMAILINFO ZEMAIL_plineHeaderbcc; /* 密送者链头 */
+ PZEMAILATTACH ZEMAIL_plineHeaderBody; /* 消息体链头 */
+ PZEMAILATTACH ZEMAIL_plineHeaderAttach; /* 附件链头 */
+ PZEMAILATTACH ZEMAIL_plineHeaderCurrAttach; /* 当前要发送的链头 */
+
+ PCHAR ZEMAIL_pcSubject; /* 主题 */
+ PCHAR ZEMAIL_pcHeader; /* 邮件头 */
+ PCHAR ZEMAIL_pcMimeBoundaryPart; /* mime 边界分割符 */
+ PCHAR ZEMAIL_pcMimeBoundaryBody;
+ PCHAR ZEMAIL_pcMailBuff; /* 临时缓冲区         */
+ INT ZEMAIL_iMailBuffLen; /* 缓冲区长度 */
+ CHAR ZEMAIL_cDtable[64];
+ FILE *ZEMAIL_fp; /* DEBUG 输出 */
+} ZEMAIL;
+/*********************************************************************************************************
+** 函数名称: __randZeros
+** 功能描述: 0字符产生随机数
+** 输 入 : pcData : 数据
+** 输 出 : 数据指针
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static PCHAR __randZeros (PCHAR pcData)
+{
+ PCHAR p = pcData;
+
+ while (*p) {
+ if (*p == '0') {
+ *p = '0' + rand() % 10;
+ }
+ p++;
+ }
+
+ return (pcData);
+}
+/*********************************************************************************************************
+** 函数名称: __stringAppend
+** 功能描述: 字符串追加
+** 输 入 : ppcDest : 目的字符串
+** pcSrc : 需要追加的字符串
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static PCHAR __stringAppend (PCHAR *ppcDest, PCHAR pcSrc)
+{
+ PCHAR pcTemp;
+ INT iLen = *ppcDest ? strlen(*ppcDest) : 0;
+
+ pcTemp = (PCHAR)realloc(*ppcDest, iLen + strlen(pcSrc) + 1);
+ if (!pcTemp) {
+ free(*ppcDest);
+ return (LW_NULL);
+ }
+
+ *ppcDest = pcTemp;
+ strcpy(*ppcDest + iLen, pcSrc);
+
+ return (*ppcDest);
+}
+/*********************************************************************************************************
+** 函数名称: __stringFree
+** 功能描述: 字符串释放
+** 输 入 : pcStr : 字符串
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID __stringFree (PCHAR pcStr)
+{
+ ZEMAIL_SAFTFREE(pcStr);
+}
+/*********************************************************************************************************
+** 函数名称: __zemailInfoListAdd
+** 功能描述: email信息增加
+** 输 入 : pplistlineHeader : 链表头
+** pcStr : 需要追加的字符串
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+INT __zemailInfoListAdd (PZEMAILINFO *pplistlineHeader, PCHAR pcStr)
+{
+ PZEMAILINFO *pzemailinfo = pplistlineHeader;
+
+ while (*pzemailinfo) {
+ pzemailinfo = &(*pzemailinfo)->ZEMAILINFO_next;
+ }
+
+ *pzemailinfo = (PZEMAILINFO)malloc(sizeof(ZEMAILINFO));
+ if (!*pzemailinfo) {
+ ZEMAIL_ERR("Add %s failed.\n", pcStr);
+ return (PX_ERROR);
+ }
+
+ (*pzemailinfo)->ZEMAILINFO_next = LW_NULL;
+ (*pzemailinfo)->ZEMAILINFO_pcData = pcStr ? strdup(pcStr) : LW_NULL;
+
+ return (ERROR_NONE);
+}
+/*********************************************************************************************************
+** 函数名称: __zemailInfoListDel
+** 功能描述: 删除信息链
+** 输 入 : plistlineHeader : 链头
+** pcInfo : 邮件信息
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID __zemailInfoListDel (PZEMAILINFO *pplistlineHeader, PCHAR pcInfo)
+{
+ PZEMAILINFO pzemailinfo = *pplistlineHeader;
+ PZEMAILINFO q;
+ INT iFind = 0;
+
+ while (pzemailinfo) {
+ q = pzemailinfo;
+ pzemailinfo = q->ZEMAILINFO_next;
+
+ if (pcInfo) {
+ if (strcmp(q->ZEMAILINFO_pcData, pcInfo)) {
+ continue;
+ } else {
+ iFind = 1;
+ }
+ }
+
+ ZEMAIL_SAFTFREE(q->ZEMAILINFO_pcData);
+ free(q);
+
+ if (iFind) {
+ break;
+ }
+ }
+}
+/*********************************************************************************************************
+** 函数名称: __zemailInfoListConcatenate
+** 功能描述: 组装邮件信息(如:<example@163.com> )
+** 输 入 : plistlineHeader : 链头
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PCHAR __zemailInfoListConcatenate (PZEMAILINFO plistlineHeader)
+{
+ PZEMAILINFO pzemailinfo = plistlineHeader;
+ PCHAR pcResult = LW_NULL;
+
+ while (pzemailinfo) {
+ if (pzemailinfo->ZEMAILINFO_pcData && *(pzemailinfo->ZEMAILINFO_pcData)) {
+ if (pcResult) {
+ __stringAppend(&pcResult, "."ZEMAIL_NEWLINE"\t");
+ }
+ __stringAppend(&pcResult, "<");
+ __stringAppend(&pcResult, pzemailinfo->ZEMAILINFO_pcData);
+ __stringAppend(&pcResult, ">");
+ }
+ pzemailinfo = pzemailinfo->ZEMAILINFO_next;
+ }
+
+ return (pcResult);
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentListAdd
+** 功能描述: 组装附件链
+** 输 入 : pplistlineHeader : 链头头
+** pcFileName : 文件名
+** pcMimeType : MIME 类型
+** pvFileData : 数据
+** pfnOpen : open
+** pfnRead : read
+** pfnClose : close
+** pfnFree : free
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+INT __attachmentListAdd (PZEMAILATTACH *pplistlineHeader,
+ PCHAR pcFileName,
+ PCHAR pcMimeType,
+ PVOID pvFileData,
+ zemailOpenFn pfnOpen,
+ zemailReadFn pfnRead,
+ zemailCloseFn pfnClose,
+ zemailFreeDataFn pfnFree)
+{
+ PZEMAILATTACH *ppzemailattach = pplistlineHeader;
+
+ while (*ppzemailattach) {
+ ppzemailattach = &((*ppzemailattach)->ZEMAILATTACH_next);
+ }
+
+ *ppzemailattach = (PZEMAILATTACH)malloc(sizeof(ZEMAILATTACH));
+ if (!*ppzemailattach) {
+ ZEMAIL_ERR("Alloc attachment memory failed.\n");
+ return (PX_ERROR);
+ }
+
+ (*ppzemailattach)->ZEMAILATTACH_pcFileName = strdup(pcFileName ? pcFileName : "UNNAME");
+ (*ppzemailattach)->ZEMAILATTACH_pcMimeType = pcMimeType ? strdup(pcMimeType) : LW_NULL;
+ (*ppzemailattach)->ZEMAILATTACH_pvFileData = pvFileData;
+ (*ppzemailattach)->ZEMAILATTACH_pvHandle = LW_NULL;
+ (*ppzemailattach)->ZEMAILATTACH_open = pfnOpen;
+ (*ppzemailattach)->ZEMAILATTACH_read = pfnRead;
+ (*ppzemailattach)->ZEMAILATTACH_close = pfnClose;
+ (*ppzemailattach)->ZEMAILATTACH_free = pfnFree;
+ (*ppzemailattach)->ZEMAILATTACH_next = LW_NULL;
+
+ return (ERROR_NONE);
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentListDel
+** 功能描述: 删除附件链
+** 输 入 : plistlineHeader : 链头
+** pcFileName : 文件名
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __attachmentListDel (PZEMAILATTACH *pplistlineHeader, PCHAR pcFileName)
+{
+ PZEMAILATTACH pzemailattach = *pplistlineHeader;
+ PZEMAILATTACH q;
+ INT iFind = 0;
+
+ while (pzemailattach) {
+ q = pzemailattach;
+ pzemailattach = pzemailattach->ZEMAILATTACH_next;
+
+ if (pcFileName) {
+ if (strcmp(q->ZEMAILATTACH_pcFileName, pcFileName)) {
+ continue;
+ } else {
+ iFind = 1;
+ }
+ }
+
+ if (q->ZEMAILATTACH_pvHandle) {
+ if (q->ZEMAILATTACH_close) {
+ q->ZEMAILATTACH_close(q->ZEMAILATTACH_pvHandle);
+ }
+
+ q->ZEMAILATTACH_pvHandle = LW_NULL;
+ }
+ if (q->ZEMAILATTACH_pvFileData) {
+ if (q->ZEMAILATTACH_free) {
+ q->ZEMAILATTACH_free(q->ZEMAILATTACH_pvFileData);
+ } else {
+ free(q->ZEMAILATTACH_pvFileData);
+ }
+ }
+
+ ZEMAIL_SAFTFREE(q->ZEMAILATTACH_pcMimeType);
+ ZEMAIL_SAFTFREE(q->ZEMAILATTACH_pcFileName);
+ free(q);
+
+ if (iFind) {
+ break;
+ }
+ }
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentOpenFile
+** 功能描述: email 打开文件
+** 输 入 : pcFileName : 文件名
+** 输 出 : 文件句柄
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static PVOID __attachmentOpenFile (PVOID pcFileName)
+{
+ if (!pcFileName) {
+ return (LW_NULL);
+ }
+
+ return (PVOID)fopen((PCHAR)pcFileName, "rb");
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentReadFile
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 字节
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static size_t __attachmentReadFile (PVOID pvHandle, PVOID pvBuf, size_t stLen)
+{
+ return fread(pvBuf, 1, stLen, (FILE *)pvHandle);
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentCloseFile
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __attachmentCloseFile (PVOID pvHandle)
+{
+ if (pvHandle) {
+ fclose((FILE *)pvHandle);
+ }
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentAddFile
+** 功能描述: email 添加附件
+** 输 入 : pplistlineHeader : 链头
+** pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __attachmentAddFile (PZEMAILATTACH *pplistlineHeader,
+ PCHAR pcPath,
+ PCHAR pcMimeType)
+{
+ PCHAR pcBasename;
+
+ pcBasename = basename(pcPath);
+ ZEMAIL_DBG("basename:%s\n", pcBasename);
+
+ return __attachmentListAdd(pplistlineHeader,
+ pcBasename,
+ pcMimeType,
+ strdup(pcPath),
+ __attachmentOpenFile,
+ __attachmentReadFile,
+ __attachmentCloseFile,
+ LW_NULL);
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentOpenMemory
+** 功能描述: 打开内存区
+** 输 入 : pvData : 数据
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static PVOID __attachmentOpenMemory (PVOID pvData)
+{
+ ZEMAILM *pzemailData;
+ ZEMAILH *pzemailhandle;
+
+ if (!pvData) {
+ return (LW_NULL);
+ }
+
+ pzemailData = (ZEMAILM *)pvData;
+ if (!pzemailData->ZEMAILM_pcData) {
+ return (LW_NULL);
+ }
+
+ pzemailhandle = (ZEMAILH *)malloc(sizeof(ZEMAILH));
+ if (!pzemailhandle) {
+ return (LW_NULL);
+ }
+
+ pzemailhandle->ZEMAILH_pcData = pzemailData->ZEMAILM_pcData;
+ pzemailhandle->ZEMAILH_stLen = pzemailData->ZEMAILM_stLen;
+ pzemailhandle->ZEMAILH_stPos = 0;
+
+ return (pzemailhandle);
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentReadMemory
+** 功能描述: 读内存区
+** 输 入 : pvHandle : 文件handle
+** pvBuf : BUFFER
+** stLen : BUFFER长度
+** 输 出 : 长度
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static size_t __attachmentReadMemory (PVOID pvHandle, PVOID pvBuf, size_t stLen)
+{
+ ZEMAILH *pzemailhandle = (ZEMAILH *)pvHandle;
+ size_t stReadLen = (pzemailhandle->ZEMAILH_stLen - pzemailhandle->ZEMAILH_stPos) > stLen ?
+ stLen : pzemailhandle->ZEMAILH_stLen - pzemailhandle->ZEMAILH_stPos;
+
+ memcpy(pvBuf, pzemailhandle->ZEMAILH_pcData + pzemailhandle->ZEMAILH_stPos, stReadLen);
+
+ pzemailhandle->ZEMAILH_stPos += stReadLen;
+
+ return (stReadLen);
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentCloseMemory
+** 功能描述: 关内存区
+** 输 入 : pvHandle : 文件handle
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __attachmentCloseMemory (PVOID pvHandle)
+{
+ ZEMAIL_SAFTFREE(pvHandle);
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentFreeMemory
+** 功能描述: 释放内存
+** 输 入 : pvData : 数据
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __attachmentFreeMemory (PVOID pvData)
+{
+ ZEMAILM *pzemailData;
+
+ pzemailData = (ZEMAILM *)pvData;
+ if (pzemailData) {
+ if (pzemailData->ZEMAILM_iMustFree) {
+ ZEMAIL_SAFTFREE(pzemailData->ZEMAILM_pcData);
+ }
+ free(pzemailData);
+ }
+}
+/*********************************************************************************************************
+** 函数名称: __mailAddTextBody
+** 功能描述: email 添加附件
+** 输 入 : pplistlineHeader : 链头
+** pcFileName : 文件名
+** pcMimeType : MIME类型
+** pcFileData : 数据
+** stLen : 数据长度
+** iMustFree : 是否要FREE数据
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static INT __mailAddTextBody (PZEMAILATTACH *pplistlineHeader,
+ PCHAR pcFileName,
+ PCHAR pcMimeType,
+ PVOID pcFileData,
+ size_t stLen,
+ INT iMustFree)
+{
+ ZEMAILM *pzemailData;
+
+ pzemailData = (ZEMAILM *)malloc(sizeof(ZEMAILM));
+ if (!pzemailData) {
+ return (PX_ERROR);
+ }
+
+ pzemailData->ZEMAILM_pcData = pcFileData;
+ pzemailData->ZEMAILM_stLen = stLen;
+ pzemailData->ZEMAILM_iMustFree = iMustFree;
+
+ return (__attachmentListAdd(pplistlineHeader,
+ pcFileName,
+ pcMimeType,
+ (PVOID)pzemailData,
+ __attachmentOpenMemory,
+ __attachmentReadMemory,
+ __attachmentCloseMemory,
+ __attachmentFreeMemory));
+}
+/*********************************************************************************************************
+** 函数名称: __GetEmailContent
+** 功能描述: email 发送主函数
+** 输 入 : pvPtr : CURL缓冲区
+** stSize : block size
+** stBlock : block num
+** pvUsrptr : 用户缓冲区
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static size_t __GetEmailContent (PVOID pvPtr, size_t stSize, size_t stBlock, PVOID pvUsrptr)
+{
+ PZEMAIL pzemail = (PZEMAIL)pvUsrptr;
+
+ if (!(stSize * stBlock)) {
+ return (0);
+ }
+
+ if (pzemail->ZEMAIL_state == ZEMAIL_INIT) {
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcMailBuff);
+ pzemail->ZEMAIL_pcMailBuff = LW_NULL;
+ pzemail->ZEMAIL_iMailBuffLen = 0;
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcMimeBoundaryBody);
+ pzemail->ZEMAIL_pcMimeBoundaryBody = LW_NULL;
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcMimeBoundaryPart);
+ pzemail->ZEMAIL_pcMimeBoundaryPart = LW_NULL;
+ pzemail->ZEMAIL_plineHeaderCurrAttach = pzemail->ZEMAIL_plineHeaderBody;
+ pzemail->ZEMAIL_state++;
+ }
+
+ while (pzemail->ZEMAIL_iMailBuffLen == 0) {
+ if ((!pzemail->ZEMAIL_iMailBuffLen) && pzemail->ZEMAIL_state == ZEMAIL_HEADER) {
+ PCHAR *ppc;
+ PCHAR ppcT;
+ ppc = &pzemail->ZEMAIL_pcMailBuff;
+ __stringAppend(ppc, "Zthsoft email "ZEMAIL_VERSION ZEMAIL_NEWLINE);
+ if (pzemail->ZEMAIL_tTimestamp != 0) {
+ CHAR cTime[64];
+ if (strftime(cTime, sizeof(cTime), "%a, %d %b %Y %H:%M:%S",
+ localtime(&pzemail->ZEMAIL_tTimestamp))) {
+ __stringAppend(ppc, "Date: ");
+ __stringAppend(ppc, cTime);
+ __stringAppend(ppc, ZEMAIL_NEWLINE);
+ }
+ }
+
+ /*
+ * From:
+ */
+ if (pzemail->ZEMAIL_pcFrom) {
+ __stringAppend(ppc, "From: <");
+ __stringAppend(ppc, pzemail->ZEMAIL_pcFrom);
+ __stringAppend(ppc, ">"ZEMAIL_NEWLINE);
+ }
+
+ /*
+ * To:
+ */
+ if ((ppcT = __zemailInfoListConcatenate(pzemail->ZEMAIL_plineHeaderto))) {
+ __stringAppend(ppc, "To: ");
+ __stringAppend(ppc, ppcT);
+ __stringAppend(ppc, ZEMAIL_NEWLINE);
+ ZEMAIL_SAFTFREE(ppcT);
+ }
+
+ /*
+ * Cc:
+ */
+ if ((ppcT = __zemailInfoListConcatenate(pzemail->ZEMAIL_plineHeadercc))) {
+ __stringAppend(ppc, "Cc: ");
+ __stringAppend(ppc, ppcT);
+ __stringAppend(ppc, ZEMAIL_NEWLINE);
+ ZEMAIL_SAFTFREE(ppcT);
+ }
+
+ /*
+ * Subject:
+ */
+ if (pzemail->ZEMAIL_pcSubject) {
+ __stringAppend(ppc, "Subject: ");
+ __stringAppend(ppc, pzemail->ZEMAIL_pcSubject);
+ __stringAppend(ppc, ZEMAIL_NEWLINE);
+ }
+
+ /*
+ * Header
+ */
+ if (pzemail->ZEMAIL_pcHeader) {
+ __stringAppend(ppc, pzemail->ZEMAIL_pcHeader);
+ }
+
+ /*
+ * 存在附件
+ */
+ if (pzemail->ZEMAIL_plineHeaderAttach) {
+ __stringAppend(ppc, "MIME-Version: 1.0"ZEMAIL_NEWLINE);
+ pzemail->ZEMAIL_pcMimeBoundaryPart =
+ __randZeros(strdup("=PART=SEPARATOR=_0000_0000_0000_0000_0000_0000_="));
+
+ __stringAppend(ppc, "Content-Type: multipart/mixed; boundary=\"");
+ __stringAppend(ppc, pzemail->ZEMAIL_pcMimeBoundaryPart);
+ __stringAppend(ppc, "\"" ZEMAIL_NEWLINE ZEMAIL_NEWLINE
+ "This is a multipart message in MIME format."
+ ZEMAIL_NEWLINE ZEMAIL_NEWLINE "--");
+ __stringAppend(ppc, pzemail->ZEMAIL_pcMimeBoundaryPart);
+ __stringAppend(ppc, ZEMAIL_NEWLINE);
+ }
+
+ /*
+ * 存在邮件体
+ */
+ if (pzemail->ZEMAIL_plineHeaderBody &&
+ pzemail->ZEMAIL_plineHeaderBody->ZEMAILATTACH_next) {
+ pzemail->ZEMAIL_pcMimeBoundaryBody =
+ __randZeros(strdup("=BODY=SEPARATOR=_0000_0000_0000_0000_0000_0000_="));
+ __stringAppend(ppc, "Content-Type: multipart/alternative; boundary=\"");
+ __stringAppend(ppc, pzemail->ZEMAIL_pcMimeBoundaryBody);
+ __stringAppend(ppc, ZEMAIL_NEWLINE);
+ }
+
+ pzemail->ZEMAIL_iMailBuffLen = strlen(pzemail->ZEMAIL_pcMailBuff);
+ pzemail->ZEMAIL_state++;
+ }
+
+ if ((!pzemail->ZEMAIL_iMailBuffLen) && pzemail->ZEMAIL_state == ZEMAIL_BODY) {
+ /*
+ * 首先解析 BODY 如果存在.
+ */
+ if (pzemail->ZEMAIL_plineHeaderCurrAttach) {
+ if (!pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle) {
+ while (pzemail->ZEMAIL_plineHeaderCurrAttach) {
+ if ((pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle =
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_open
+ (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvFileData))) {
+ break;
+ }
+ pzemail->ZEMAIL_plineHeaderCurrAttach =
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_next;
+ }
+
+ if (!pzemail->ZEMAIL_plineHeaderCurrAttach) {
+ pzemail->ZEMAIL_plineHeaderCurrAttach = pzemail->ZEMAIL_plineHeaderAttach;
+ pzemail->ZEMAIL_state++;
+ }
+
+ if (pzemail->ZEMAIL_plineHeaderCurrAttach &&
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle) {
+ pzemail->ZEMAIL_pcMailBuff = LW_NULL;
+ if (pzemail->ZEMAIL_pcMimeBoundaryBody) {
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, ZEMAIL_NEWLINE "--");
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff,
+ pzemail->ZEMAIL_pcMimeBoundaryBody);
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, ZEMAIL_NEWLINE);
+ }
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, "Content-Type: ");
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff,
+ (pzemail->ZEMAIL_plineHeaderBody &&
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pcFileName ?
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pcFileName :
+ ZEMAIL_DEFAULT_MIME_TYPE));
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff,
+ ZEMAIL_NEWLINE "Content-Transfer-Encoding: 8bit"
+ ZEMAIL_NEWLINE "Content-Disposition: inline"
+ ZEMAIL_NEWLINE ZEMAIL_NEWLINE);
+ pzemail->ZEMAIL_iMailBuffLen = strlen(pzemail->ZEMAIL_pcMailBuff);
+ }
+
+ }
+
+ if (!pzemail->ZEMAIL_iMailBuffLen && pzemail->ZEMAIL_plineHeaderCurrAttach &&
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle) {
+ pzemail->ZEMAIL_pcMailBuff = malloc(ZEMAIL_BODY_BUFFER_SIZE);
+ pzemail->ZEMAIL_iMailBuffLen =
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_read
+ (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle,
+ pzemail->ZEMAIL_pcMailBuff, ZEMAIL_BODY_BUFFER_SIZE);
+ if (!pzemail->ZEMAIL_pcMailBuff || pzemail->ZEMAIL_iMailBuffLen <= 0) {
+ pzemail->ZEMAIL_iMailBuffLen = 0;
+ if (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_close) {
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_close
+ (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle);
+ }
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle = LW_NULL;
+ pzemail->ZEMAIL_plineHeaderCurrAttach =
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_next;
+ }
+ }
+ } else {
+ /*
+ * 否则解析附件.
+ */
+ pzemail->ZEMAIL_plineHeaderCurrAttach = pzemail->ZEMAIL_plineHeaderAttach;
+ pzemail->ZEMAIL_state++;
+ }
+ }
+
+ if ((!pzemail->ZEMAIL_iMailBuffLen) && pzemail->ZEMAIL_state == ZEMAIL_BODY_DONE) {
+ pzemail->ZEMAIL_pcMailBuff = NULL;
+ if (pzemail->ZEMAIL_pcMimeBoundaryBody) {
+ pzemail->ZEMAIL_pcMailBuff = __stringAppend(&pzemail->ZEMAIL_pcMailBuff, ZEMAIL_NEWLINE "--");
+ pzemail->ZEMAIL_pcMailBuff = __stringAppend(&pzemail->ZEMAIL_pcMailBuff, pzemail->ZEMAIL_pcMimeBoundaryBody);
+ pzemail->ZEMAIL_pcMailBuff = __stringAppend(&pzemail->ZEMAIL_pcMailBuff, "--" ZEMAIL_NEWLINE);
+ pzemail->ZEMAIL_iMailBuffLen = strlen(pzemail->ZEMAIL_pcMailBuff);
+ free(pzemail->ZEMAIL_pcMimeBoundaryBody);
+ pzemail->ZEMAIL_pcMimeBoundaryBody = NULL;
+ }
+ pzemail->ZEMAIL_state++;
+ }
+
+ if ((!pzemail->ZEMAIL_iMailBuffLen) && pzemail->ZEMAIL_state == ZEMAIL_ATTACHMENT) {
+ if (pzemail->ZEMAIL_plineHeaderCurrAttach) {
+ if (!pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle) {
+ while (pzemail->ZEMAIL_plineHeaderCurrAttach) {
+ if ((pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle =
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_open
+ (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvFileData))) {
+ break;
+ }
+ pzemail->ZEMAIL_plineHeaderCurrAttach =
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_next;
+ }
+
+ if (pzemail->ZEMAIL_plineHeaderCurrAttach &&
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle) {
+ pzemail->ZEMAIL_pcMailBuff = LW_NULL;
+ if (pzemail->ZEMAIL_pcMimeBoundaryPart) {
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, ZEMAIL_NEWLINE "--");
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, pzemail->ZEMAIL_pcMimeBoundaryPart);
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, ZEMAIL_NEWLINE);
+ }
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, "Content-Type: ");
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff,
+ (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pcMimeType ?
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pcMimeType :
+ "application/octet-stream"));
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, "; Name=\"");
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff,
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pcFileName);
+ pzemail->ZEMAIL_pcMailBuff =
+ __stringAppend(&pzemail->ZEMAIL_pcMailBuff, "\""
+ ZEMAIL_NEWLINE "Content-Transfer-Encoding: base64"
+ ZEMAIL_NEWLINE ZEMAIL_NEWLINE);
+ pzemail->ZEMAIL_iMailBuffLen = strlen(pzemail->ZEMAIL_pcMailBuff);
+ }
+ } else {
+ size_t n = 0;
+ int mimelinepos = 0;
+ unsigned char igroup[3] = {0, 0, 0};
+ unsigned char ogroup[4];
+
+ pzemail->ZEMAIL_iMailBuffLen = 0;
+ if ((pzemail->ZEMAIL_pcMailBuff =
+ (char*)malloc(ZEMAIL_MIME_LINE_WIDTH + ZEMAIL_NEWLINE_LEN + 1)) ==
+ NULL) {
+ n = 0;
+ } else {
+ while (mimelinepos < ZEMAIL_MIME_LINE_WIDTH &&
+ (n = pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_read
+ (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle,
+ igroup, 3)) > 0) {
+ /*
+ * code data
+ */
+ ogroup[0] = pzemail->ZEMAIL_cDtable[igroup[0] >> 2];
+ ogroup[1] = pzemail->ZEMAIL_cDtable[((igroup[0] & 3) << 4) | (igroup[1] >> 4)];
+ ogroup[2] = pzemail->ZEMAIL_cDtable[((igroup[1] & 0xF) << 2) | (igroup[2] >> 6)];
+ ogroup[3] = pzemail->ZEMAIL_cDtable[igroup[2] & 0x3F];
+ /*
+ * padd with "=" characters if less than 3 characters were read
+ */
+ if (n < 3) {
+ ogroup[3] = '=';
+ if (n < 2)
+ ogroup[2] = '=';
+ }
+
+ memcpy(pzemail->ZEMAIL_pcMailBuff + mimelinepos, ogroup, 4);
+ pzemail->ZEMAIL_iMailBuffLen += 4;
+ mimelinepos += 4;
+ }
+ if (mimelinepos > 0) {
+ memcpy(pzemail->ZEMAIL_pcMailBuff + mimelinepos, ZEMAIL_NEWLINE, ZEMAIL_NEWLINE_LEN);
+ pzemail->ZEMAIL_iMailBuffLen += ZEMAIL_NEWLINE_LEN;
+ }
+ }
+ if (n <= 0) {
+ /*
+ * end of file
+ */
+ if (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_close) {
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_close
+ (pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle);
+ } else {
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle);
+ }
+
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_pvHandle = NULL;
+ pzemail->ZEMAIL_plineHeaderCurrAttach =
+ pzemail->ZEMAIL_plineHeaderCurrAttach->ZEMAILATTACH_next;
+ }
+ }
+ } else {
+ pzemail->ZEMAIL_state++;
+ }
+ }
+
+ if ((!pzemail->ZEMAIL_iMailBuffLen) && pzemail->ZEMAIL_state == ZEMAIL_END) {
+ pzemail->ZEMAIL_pcMailBuff = NULL;
+ pzemail->ZEMAIL_iMailBuffLen = 0;
+ if (pzemail->ZEMAIL_pcMimeBoundaryPart) {
+ pzemail->ZEMAIL_pcMailBuff = __stringAppend(&pzemail->ZEMAIL_pcMailBuff, ZEMAIL_NEWLINE "--");
+ pzemail->ZEMAIL_pcMailBuff = __stringAppend(&pzemail->ZEMAIL_pcMailBuff, pzemail->ZEMAIL_pcMimeBoundaryPart);
+ pzemail->ZEMAIL_pcMailBuff = __stringAppend(&pzemail->ZEMAIL_pcMailBuff, "--" ZEMAIL_NEWLINE);
+ pzemail->ZEMAIL_iMailBuffLen = strlen(pzemail->ZEMAIL_pcMailBuff);
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcMimeBoundaryPart);
+ pzemail->ZEMAIL_pcMimeBoundaryPart = NULL;
+ }
+ pzemail->ZEMAIL_state++;
+ }
+
+ if ((!pzemail->ZEMAIL_iMailBuffLen) && pzemail->ZEMAIL_state == ZEMAIL_DONE) {
+ break;
+ }
+ }
+
+ if (pzemail->ZEMAIL_iMailBuffLen > 0) {
+ INT iLen = (pzemail->ZEMAIL_iMailBuffLen >= stSize * stBlock) ?
+ (stSize * stBlock) : pzemail->ZEMAIL_iMailBuffLen;
+
+ memcpy(pvPtr, pzemail->ZEMAIL_pcMailBuff, iLen);
+
+ if (iLen < pzemail->ZEMAIL_iMailBuffLen) {
+ pzemail->ZEMAIL_pcMailBuff = memmove(pzemail->ZEMAIL_pcMailBuff,
+ pzemail->ZEMAIL_pcMailBuff + iLen,
+ pzemail->ZEMAIL_iMailBuffLen - iLen);
+ pzemail->ZEMAIL_iMailBuffLen -= iLen;
+ } else {
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcMailBuff);
+ pzemail->ZEMAIL_pcMailBuff = LW_NULL;
+ pzemail->ZEMAIL_iMailBuffLen = 0;
+ }
+ return (iLen);
+ }
+
+ pzemail->ZEMAIL_state = ZEMAIL_INIT;
+ return (0);
+}
+/*********************************************************************************************************
+** 函数名称: __attachmentDelFile
+** 功能描述: 删除附件
+** 输 入 : plistlineHeader : 链头
+** pcPath : 附件
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __attachmentDelFile (PZEMAILATTACH *plistlineHeader, PCHAR pcPath)
+{
+ PCHAR pcBasename;
+
+ pcBasename = basename(pcPath);
+ ZEMAIL_DBG("basename:%s\n", pcBasename);
+
+ __attachmentListDel(plistlineHeader, pcBasename);
+}
+/*********************************************************************************************************
+** 函数名称: __stringAddAngleBrackets
+** 功能描述: 组装字符串
+** 输 入 : pcStr : 字符串
+** 输 出 : 新字符串
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static PCHAR __stringAddAngleBrackets (PCHAR pcStr)
+{
+ size_t stLen = strlen(pcStr);
+ PCHAR pcResult;
+
+ pcResult = malloc(stLen + 3);
+
+ /*
+ * 组装字符串如: "<abc>"
+ */
+ pcResult[0] = '<';
+ memcpy(pcResult + 1, pcStr, stLen);
+ pcResult[stLen + 1] = '>';
+ pcResult[stLen + 2] = '\0';
+
+ return (pcResult);
+}
+/*********************************************************************************************************
+** 函数名称: __zemailAppendInfoToCurl
+** 功能描述: email 添加附件
+** 输 入 : plineHeader : 链头
+** pprecpt : CURL 链
+** 输 出 : NONE
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+static VOID __zemailAppendInfoToCurl (PZEMAILINFO *plineHeader,
+ struct curl_slist **pprecpt)
+{
+ PZEMAILINFO pzemailinfo = *plineHeader;
+ PZEMAILINFO q;
+ PCHAR pcAddr;
+
+ while (pzemailinfo) {
+ q = pzemailinfo;
+ pzemailinfo = pzemailinfo->ZEMAILINFO_next;
+
+ if (q->ZEMAILINFO_pcData) {
+ pcAddr = __stringAddAngleBrackets(q->ZEMAILINFO_pcData);
+ *pprecpt = curl_slist_append(*pprecpt, pcAddr);
+ ZEMAIL_SAFTFREE(pcAddr);
+ }
+ }
+}
+/*********************************************************************************************************
+ API 函数
+*********************************************************************************************************/
+/*********************************************************************************************************
+** 函数名称: xemailSetFrom
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailSetFrom (PZEMAIL pzemail, PCHAR pcFrom)
+{
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcFrom);
+ pzemail->ZEMAIL_pcFrom = pcFrom ? strdup(pcFrom) : LW_NULL;
+}
+/*********************************************************************************************************
+** 函数名称: xemailGetFrom
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PCHAR xemailGetFrom (PZEMAIL pzemail)
+{
+ if (!pzemail) {
+ return (LW_NULL);
+ }
+
+ return (pzemail->ZEMAIL_pcFrom);
+}
+/*********************************************************************************************************
+** 函数名称: xemailAddBCC
+** 功能描述: email 增加隐秘抄送者 (BCC: )
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddBCC (PZEMAIL pzemail, PCHAR pcBCC)
+{
+ __zemailInfoListAdd(&pzemail->ZEMAIL_plineHeaderbcc, pcBCC);
+}
+/*********************************************************************************************************
+** 函数名称: xemailAddCC
+** 功能描述: email 增加抄送者 (CC: )
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddCC (PZEMAIL pzemail, PCHAR pcCC)
+{
+ __zemailInfoListAdd(&pzemail->ZEMAIL_plineHeadercc, pcCC);
+}
+/*********************************************************************************************************
+** 函数名称: xemailAddTO
+** 功能描述: email 增加接收者 (TO: )
+** 输 入 : pzemail : email 结构
+** pcTo : 接收者字符串
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddTO (PZEMAIL pzemail, PCHAR pcTo)
+{
+ __zemailInfoListAdd(&pzemail->ZEMAIL_plineHeaderto, pcTo);
+}
+/*********************************************************************************************************
+** 函数名称: xemailSetSubject
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailSetSubject (PZEMAIL pzemail, PCHAR pcSubject)
+{
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcSubject);
+ pzemail->ZEMAIL_pcSubject = pcSubject ? strdup(pcSubject) : LW_NULL;
+}
+/*********************************************************************************************************
+** 函数名称: xemailGetSubject
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PCHAR xemailGetSubject (PZEMAIL pzemail)
+{
+ if (!pzemail) {
+ return (LW_NULL);
+ }
+
+ return (pzemail->ZEMAIL_pcSubject);
+}
+/*********************************************************************************************************
+** 函数名称: xemailAddHeader
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddHeader (PZEMAIL pzemail, PCHAR pcHeader)
+{
+ if (!pzemail) {
+ return;
+ }
+
+ __stringAppend(&pzemail->ZEMAIL_pcHeader, pcHeader);
+ __stringAppend(&pzemail->ZEMAIL_pcHeader, ZEMAIL_NEWLINE);
+}
+/*********************************************************************************************************
+** 函数名称: xemailSetMailBody
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailSetMailBody (PZEMAIL pzemail, PCHAR pcBody)
+{
+ if (!pzemail) {
+ return;
+ }
+
+ if (pzemail->ZEMAIL_plineHeaderBody) {
+ __attachmentListDel(&pzemail->ZEMAIL_plineHeaderBody, LW_NULL);
+ }
+
+ if (pcBody) {
+ __mailAddTextBody(&pzemail->ZEMAIL_plineHeaderBody,
+ ZEMAIL_DEFAULT_MIME_TYPE,
+ ZEMAIL_DEFAULT_MIME_TYPE,
+ (PVOID)strdup(pcBody), strlen(pcBody), 1);
+ }
+}
+/*********************************************************************************************************
+** 函数名称: xemailAddMailBodyByType
+** 功能描述: 设置指定类型的邮件内容(如: "text/html" 类型的内容)
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddMailBodyByType (PZEMAIL pzemail,
+ PCHAR pcMimeType,
+ PCHAR pcData,
+ size_t stLen,
+ INT iMustFree)
+{
+ __mailAddTextBody(&pzemail->ZEMAIL_plineHeaderBody,
+ (pcMimeType ? pcMimeType : ZEMAIL_DEFAULT_MIME_TYPE),
+ (pcMimeType ? pcMimeType : ZEMAIL_DEFAULT_MIME_TYPE),
+ (PVOID)strdup(pcData), stLen, iMustFree);
+}
+/*********************************************************************************************************
+** 函数名称: xemailAddAttachmentFile
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailAddAttachmentFile (PZEMAIL pzemail, PCHAR pcPath, PCHAR pcMime)
+{
+ __attachmentAddFile(&pzemail->ZEMAIL_plineHeaderAttach, pcPath, pcMime);
+}
+/*********************************************************************************************************
+** 函数名称: xemailDelAttachmentFile
+** 功能描述: email 添加附件
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailDelAttachmentFile (PZEMAIL pzemail, PCHAR pcPath)
+{
+ __attachmentDelFile(&pzemail->ZEMAIL_plineHeaderAttach, pcPath);
+}
+/*********************************************************************************************************
+** 函数名称: xemailDestroy
+** 功能描述: email 销毁主函数
+** 输 入 : pzemail : email 结构
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailDestroy (PZEMAIL pzemail)
+{
+ if (!pzemail) {
+ return;
+ }
+
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcFrom);
+ __zemailInfoListDel(&pzemail->ZEMAIL_plineHeaderto, LW_NULL);
+ __zemailInfoListDel(&pzemail->ZEMAIL_plineHeadercc, LW_NULL);
+ __zemailInfoListDel(&pzemail->ZEMAIL_plineHeaderbcc, LW_NULL);
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcSubject);
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcHeader);
+ __attachmentListDel(&pzemail->ZEMAIL_plineHeaderBody, LW_NULL);
+ __attachmentListDel(&pzemail->ZEMAIL_plineHeaderAttach, LW_NULL);
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcMailBuff);
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcMimeBoundaryPart);
+ ZEMAIL_SAFTFREE(pzemail->ZEMAIL_pcMimeBoundaryBody);
+ ZEMAIL_SAFTFREE(pzemail);
+}
+/*********************************************************************************************************
+** 函数名称: xemailCreate
+** 功能描述: email 创建主函数
+** 输 入 : pcFrom : 发送者
+** pcSubject : 邮件主题名
+** 输 出 : 创建成功的 email 结构
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PZEMAIL xemailCreate (PCHAR pcFrom, PCHAR pcSubject)
+{
+ PZEMAIL pzemail;
+ INT i;
+
+ pzemail = (PZEMAIL)malloc(sizeof(ZEMAIL));
+ if (!pzemail) {
+ ZEMAIL_ERR("Z-email alloc memory failed.\n");
+ return (LW_NULL);
+ }
+
+ pzemail->ZEMAIL_state = ZEMAIL_INIT;
+ pzemail->ZEMAIL_tTimestamp = time(LW_NULL);
+ pzemail->ZEMAIL_pcFrom = pcFrom ? strdup(pcFrom) : LW_NULL;
+ pzemail->ZEMAIL_pcSubject = pcSubject ? strdup(pcSubject) : LW_NULL;
+ pzemail->ZEMAIL_plineHeaderto = LW_NULL;
+ pzemail->ZEMAIL_plineHeadercc = LW_NULL;
+ pzemail->ZEMAIL_plineHeaderbcc = LW_NULL;
+ pzemail->ZEMAIL_plineHeaderCurrAttach = LW_NULL;
+ pzemail->ZEMAIL_plineHeaderBody = LW_NULL;
+ pzemail->ZEMAIL_plineHeaderAttach = LW_NULL;
+ pzemail->ZEMAIL_pcMailBuff = LW_NULL;
+ pzemail->ZEMAIL_iMailBuffLen = 0;
+ pzemail->ZEMAIL_pcMimeBoundaryBody = LW_NULL;
+ pzemail->ZEMAIL_pcMimeBoundaryPart = LW_NULL;
+ pzemail->ZEMAIL_pcHeader = LW_NULL;
+ pzemail->ZEMAIL_fp = LW_NULL;
+
+ for (i = 0; i < 26; i++) {
+ pzemail->ZEMAIL_cDtable[i] = 'A' + i;
+ pzemail->ZEMAIL_cDtable[i + 26] = 'a' + i;
+ }
+
+ for (i = 0; i < 10; i++) {
+ pzemail->ZEMAIL_cDtable[52 + i] = '0' + i;
+ }
+
+ pzemail->ZEMAIL_cDtable[62] = '+';
+ pzemail->ZEMAIL_cDtable[63] = '/';
+
+ srand(time(LW_NULL));
+
+ return (pzemail);
+}
+
+#if 1
+static const char *payload_text[] = {
+ "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
+ "To: " "\r\n",
+ "From: " "(Example User)\r\n",
+ "Cc: " "(Another example User)\r\n",
+ "Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@rfcpedant.example.org>\r\n",
+ "Subject: SMTP example message\r\n",
+ "Importance: Low""X-Priority: 5""X-MSMail-Priority: Low",
+ "\r\n", /* empty line to divide headers from body, see RFC5322 */
+ "The body of the message starts here.\r\n",
+ "\r\n",
+ "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
+ "Check RFC5322.\r\n",
+ NULL
+};
+
+struct upload_status {
+ int lines_read;
+};
+
+static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
+{
+ struct upload_status *upload_ctx = (struct upload_status *)userp;
+ const char *data;
+
+ if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
+ return 0;
+ }
+
+ data = payload_text[upload_ctx->lines_read];
+
+ if(data) {
+ size_t len = strlen(data);
+ memcpy(ptr, data, len);
+ upload_ctx->lines_read++;
+
+ printf("message: %s", (char *)ptr);
+ return len;
+ }
+
+ return 0;
+}
+#endif
+/*********************************************************************************************************
+** 函数名称: xemailSend
+** 功能描述: email 发送主函数
+** 输 入 : pzemail : EMAIL 主结构
+** pcSmtpServer : smtp server
+** pcUserName : 用户名
+** pcPassword : 密码
+** 输 出 : 错误号
+** 全局变量:
+** 调用模块: CURL 库
+*********************************************************************************************************/
+INT xemailSend (PZEMAIL pzemail, PCHAR pcSmtpServer, PCHAR pcUserName, PCHAR pcPassword)
+{
+ CURL *curl;
+ CURLcode result = CURLE_FAILED_INIT;
+ struct upload_status upload_ctx;
+
+ upload_ctx.lines_read = 0;
+
+ curl = curl_easy_init();
+ if (curl) {
+ struct curl_slist *recipients = NULL;
+ PCHAR pcAddr;
+ INT iLen = strlen(pcSmtpServer) + 14;
+
+ if ((pcAddr = (PCHAR)malloc(iLen)) == LW_NULL) {
+ return (PX_ERROR);
+ }
+
+ snprintf(pcAddr, iLen, "smtp://%s", pcSmtpServer);
+
+ curl_easy_setopt(curl, CURLOPT_URL, pcAddr);
+ ZEMAIL_SAFTFREE(pcAddr);
+
+ if (pzemail->ZEMAIL_pcFrom) {
+ pcAddr = __stringAddAngleBrackets(pzemail->ZEMAIL_pcFrom);
+ curl_easy_setopt(curl, CURLOPT_MAIL_FROM, pcAddr);
+ ZEMAIL_SAFTFREE(pcAddr);
+ }
+
+ if (pcUserName) {
+ curl_easy_setopt(curl, CURLOPT_USERNAME, pcUserName);
+ }
+
+ if (pcPassword) {
+ curl_easy_setopt(curl, CURLOPT_PASSWORD, pcPassword);
+ }
+
+ __zemailAppendInfoToCurl(&pzemail->ZEMAIL_plineHeaderto, &recipients);
+ __zemailAppendInfoToCurl(&pzemail->ZEMAIL_plineHeadercc, &recipients);
+ __zemailAppendInfoToCurl(&pzemail->ZEMAIL_plineHeaderbcc, &recipients);
+
+ curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
+ curl_easy_setopt(curl, CURLOPT_READFUNCTION, __GetEmailContent);
+ curl_easy_setopt(curl, CURLOPT_READDATA, pzemail);
+// curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
+// curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
+ curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
+
+ if (pzemail->ZEMAIL_fp) {
+ curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
+ curl_easy_setopt(curl, CURLOPT_STDERR, pzemail->ZEMAIL_fp);
+ }
+
+ result = curl_easy_perform(curl);
+ curl_slist_free_all(recipients);
+ curl_easy_cleanup(curl);
+ }
+
+ return (result == CURLE_OK ? ERROR_NONE : PX_ERROR);
+}
+/*********************************************************************************************************
+** 函数名称: xemailDebugMode
+** 功能描述: 开启 DEBUG 模式
+** 输 入 : pzemail : EMAIL 结构
+** fp : log 输出文件
+** 输 出 : 版本
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+VOID xemailDebugMode (PZEMAIL pzemail, FILE *fp)
+{
+ if (!pzemail) {
+ return;
+ }
+
+ pzemail->ZEMAIL_fp = fp;
+}
+/*********************************************************************************************************
+** 函数名称: xemailVersion
+** 功能描述: 获得 Z-email 版本
+** 输 入 : NONE
+** 输 出 : 版本
+** 全局变量:
+** 调用模块:
+*********************************************************************************************************/
+PCHAR xemailVersion (VOID)
+{
+ return (ZEMAIL_VERSION);
+}
+/*********************************************************************************************************
+ END
+*********************************************************************************************************/