Jump to content

Smtp Mailer That Supports Html And Attachments.


Jos
 Share

Recommended Posts

$SMTPSERVER = "smtp.gmail.com"
$FROMNAME = "groundefiance"
$FROMADDRESS = "123@gmail.com"
$TOADDRESS = "123@gmail.com"
$SUBJECT = ("Test mail")
$Body = ClipGet()
$CCADDRESS = ""
$BCCADDRESS = ""
$IMPORTANCE = "High"
$USERNAME = "123@gmail.com"
$PASSWORD = "12345"
$IPPORT = 465
$SSL = 1
$REGEDIT = 1
_SENDMAIL()
Func _SENDMAIL()
If Not FileGetSize($ATTACH2) = 0 Then
  If _INETSMTPMAILCOM($SMTPSERVER, $FROMNAME, $FROMADDRESS, $TOADDRESS, $SUBJECT, $BODY, $ATTACH2, $CCADDRESS, $BCCADDRESS, $IMPORTANCE, $USERNAME, $PASSWORD, $IPPORT, $SSL) Then
   MsgBox(0,"","mail not sent!")
  Else
      MsgBox(0,"","mail sent!")
  EndIf
Else
      MsgBox(0,"","file size = 0 mail not sent")
EndIf
EndFunc   ;==>_SENDMAIL
Func _INETSMTPMAILCOM($S_SMTPSERVER, $S_FROMNAME, $S_FROMADDRESS, $S_TOADDRESS, $S_SUBJECT, $AS_BODY , $S_ATTACHFILES , $S_CCADDRESS , $S_BCCADDRESS , $S_IMPORTANCE , $S_USERNAME , $S_PASSWORD, $IPPORT, $SSL)
Local $OBJEMAIL = ObjCreate("CDO.Message")
$OBJEMAIL.From = '"' & $S_FROMNAME & '" <' & $S_FROMADDRESS & ">"
$OBJEMAIL.To=$S_TOADDRESS
Local $I_ERROR = 0
Local $I_ERROR_DESCIPTION = ""
If $S_CCADDRESS <> "" Then $OBJEMAIL.Cc = $S_CCADDRESS
If $S_BCCADDRESS <> "" Then $OBJEMAIL.Bcc = $S_BCCADDRESS
$OBJEMAIL.Subject = $S_SUBJECT
If StringInStr($AS_BODY, "<") And StringInStr($AS_BODY, ">") Then
  $OBJEMAIL.HTMLBody = $AS_BODY
Else
  $OBJEMAIL.Textbody = $AS_BODY & @CRLF
EndIf
If $S_ATTACHFILES <> "" Then
  Local $S_FILES2ATTACH = StringSplit($S_ATTACHFILES, ";")
  For $X = 1 To $S_FILES2ATTACH[0]
   $S_FILES2ATTACH[$X] = _PATHFULL($S_FILES2ATTACH[$X])
   ConsoleWrite("@@ Debug(62) : $S_Files2Attach = " & $S_FILES2ATTACH & @LF & ">Error code: " & @error & @LF)
   If FileExists($S_FILES2ATTACH[$X]) Then
    $OBJEMAIL.AddAttachment($S_FILES2ATTACH[$X])
   Else
    ConsoleWrite("!> File not found to attach: " & $S_FILES2ATTACH[$X] & @LF)
    SetError(1)
    Return 0
   EndIf
  Next
EndIf
;~  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/sendusing" ) = 2
$ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ) = "smtp.gmail.com"
If Number($IPPORT) = 0 Then $IPPORT = 465
$ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = 465
If $S_USERNAME <> "" Then
  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $S_USERNAME
  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $S_PASSWORD
EndIf
If $SSL Then
  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
EndIf
$OBJEMAIL.Configuration.Fields.Update
Switch $S_IMPORTANCE
  Case "High"
   $OBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "High"
  Case "Normal"
   $OBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
  Case "Low"
   $OBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
EndSwitch
$OBJEMAIL.Fields.Update
$OBJEMAIL.Send
If @error Then
  SetError(2)
  Return $OMYRET[1]
EndIf
$OBJEMAIL = ""
EndFunc   ;==>_INETSMTPMAILCOM

Edited by squidol
Link to comment
Share on other sites

There is no line number 101 in the script you posted.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

All that means is that your PC doesn't know what type of object you are trying to create. That could mean that your PC doesn't support that type of object, or that you typed the name wrong. For example,

$OBJEMAIL = ObjCreate("xyz")

gives that error.

So either CDO is not installed on your machine or it's broken.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Moderators

You have to have CDO installed on whatever computer is going to email from.

http://www.microsoft.com/downloads/detai...yID=2714320D-C997-4DE1-986F-24

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Hi,

I'm curious : why we find urls as "http://schemas.microsoft.com/cdo/configuration/" ?

Also, thank you very much for that UDF, it really helps me :graduated:

The udf takes advantage of the microsoft "CDO" api.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • 2 weeks later...

$SMTPSERVER = "smtp.gmail.com"
$FROMNAME = "groundefiance"
$FROMADDRESS = "123@gmail.com"
$TOADDRESS = "123@gmail.com"
$SUBJECT = ("Test mail")
$Body = ClipGet()
$CCADDRESS = ""
$BCCADDRESS = ""
$IMPORTANCE = "High"
$USERNAME = "123@gmail.com"
$PASSWORD = "12345"
$IPPORT = 465
$SSL = 1
$REGEDIT = 1
_SENDMAIL()
Func _SENDMAIL()
If Not FileGetSize($ATTACH2) = 0 Then
  If _INETSMTPMAILCOM($SMTPSERVER, $FROMNAME, $FROMADDRESS, $TOADDRESS, $SUBJECT, $BODY, $ATTACH2, $CCADDRESS, $BCCADDRESS, $IMPORTANCE, $USERNAME, $PASSWORD, $IPPORT, $SSL) Then
   MsgBox(0,"","mail not sent!")
  Else
      MsgBox(0,"","mail sent!")
  EndIf
Else
      MsgBox(0,"","file size = 0 mail not sent")
EndIf
EndFunc   ;==>_SENDMAIL
Func _INETSMTPMAILCOM($S_SMTPSERVER, $S_FROMNAME, $S_FROMADDRESS, $S_TOADDRESS, $S_SUBJECT, $AS_BODY , $S_ATTACHFILES , $S_CCADDRESS , $S_BCCADDRESS , $S_IMPORTANCE , $S_USERNAME , $S_PASSWORD, $IPPORT, $SSL)
Local $OBJEMAIL = ObjCreate("CDO.Message")
$OBJEMAIL.From = '"' & $S_FROMNAME & '" <' & $S_FROMADDRESS & ">"
$OBJEMAIL.To=$S_TOADDRESS
Local $I_ERROR = 0
Local $I_ERROR_DESCIPTION = ""
If $S_CCADDRESS <> "" Then $OBJEMAIL.Cc = $S_CCADDRESS
If $S_BCCADDRESS <> "" Then $OBJEMAIL.Bcc = $S_BCCADDRESS
$OBJEMAIL.Subject = $S_SUBJECT
If StringInStr($AS_BODY, "<") And StringInStr($AS_BODY, ">") Then
  $OBJEMAIL.HTMLBody = $AS_BODY
Else
  $OBJEMAIL.Textbody = $AS_BODY & @CRLF
EndIf
If $S_ATTACHFILES <> "" Then
  Local $S_FILES2ATTACH = StringSplit($S_ATTACHFILES, ";")
  For $X = 1 To $S_FILES2ATTACH[0]
   $S_FILES2ATTACH[$X] = _PATHFULL($S_FILES2ATTACH[$X])
   ConsoleWrite("@@ Debug(62) : $S_Files2Attach = " & $S_FILES2ATTACH & @LF & ">Error code: " & @error & @LF)
   If FileExists($S_FILES2ATTACH[$X]) Then
    $OBJEMAIL.AddAttachment($S_FILES2ATTACH[$X])
   Else
    ConsoleWrite("!> File not found to attach: " & $S_FILES2ATTACH[$X] & @LF)
    SetError(1)
    Return 0
   EndIf
  Next
EndIf
;~  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/sendusing" ) = 2
$ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpserver" ) = "smtp.gmail.com"
If Number($IPPORT) = 0 Then $IPPORT = 465
$ObjEmail.Configuration.Fields.Item ( "http://schemas.microsoft.com/cdo/configuration/smtpserverport" ) = 465
If $S_USERNAME <> "" Then
  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = $S_USERNAME
  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $S_PASSWORD
EndIf
If $SSL Then
  $OBJEMAIL.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
EndIf
$OBJEMAIL.Configuration.Fields.Update
Switch $S_IMPORTANCE
  Case "High"
   $OBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "High"
  Case "Normal"
   $OBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "Normal"
  Case "Low"
   $OBJEMAIL.Fields.Item("urn:schemas:mailheader:Importance") = "Low"
EndSwitch
$OBJEMAIL.Fields.Update
$OBJEMAIL.Send
If @error Then
  SetError(2)
  Return $OMYRET[1]
EndIf
$OBJEMAIL = ""
EndFunc   ;==>_INETSMTPMAILCOM

Variables and function names written in capitals, what does this mean to you?

João Carlos.

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

  • 2 months later...

I'm new to using pieces of scripts like this, Can someone tell me how to use this I have included the script into mine and can call the function no problem but when I run my script it instantly sends the mail from the included file.

How do I edit the script so it will only run when I call the function rather then on script start?

Thanks.

Link to comment
Share on other sites

  • Developers

I'm new to using pieces of scripts like this, Can someone tell me how to use this I have included the script into mine and can call the function no problem but when I run my script it instantly sends the mail from the included file.

How do I edit the script so it will only run when I call the function rather then on script start?

Thanks.

Not sure what miracle you are expecting here, but could you post your script you have problems with since it's a little hard this way.

The posted example in the first is pretty self explanatory.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

My script is same as yours but with my values it sends and I can call it to send but soon as I start the script it sends the email.

I want to be able to start the script then it waits until I call the function then sends the email, at the moment soon as I start the script it sends the email so everytime I start the script I will get a email then I will have recall it to send when I want it to send.

Basicly I don't want it to send the email when I start the script only when I call the function.

Thanks.

P.S. Good work on script I tried to find something like this a very long time ago when I started playing with autoit functions but default Inets never seem to work since free smtp servers without auth don't seem to exist.

Link to comment
Share on other sites

  • Developers

Looks like you need to learn the language first here since this is easy once you understand basic scripting.

There are some tutorial available that can teach you that basic. just look in our Wiki for information.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I guess but I'm never one for doing that :) Iv worked out a dumb way of doing it anyway by simple putting a switch so no code in the function will activate until I change the parameter of the switch. I guess it will do but can you help me with one thing where is your function being called from since Iv never had a function that activates without calling but I don't see why your function gets called on start up.

Thanks.

Link to comment
Share on other sites

  • 1 month later...

Needed this UDF again so searched via Google, but I also came across this page http://www.go4expert.com/forums/showthread.php?t=23226

You on another forum or did they steal your code? o.o

MsgBox(0x40040, "", "Hello Forum!")
Link to comment
Share on other sites

  • 4 weeks later...
  • 2 weeks later...

It seems the CDO is out dated and doesn't work on 2008 natively. Apparently you need to use System.Net.Mail

I found a vbscript but don't quite know how to convert it to work with this script.

[i]<[/i][i]%@ Import Namespace="System.Net.Mail"%>[/i]
[i]<script language="VBScript" Debug="true" runat="server">
    sub Page_Load()
        Dim oMsg As New MailMessage()
        oMsg.From = New MailAddress("mailbox@yourdomain.com")
        oMsg.To.Add("recipient@theirdomain.com")
        oMsg.Subject = "This is an email"
        oMsg.Body = "<HTML><BODY><B>Hello World!</B></BODY></HTML>"
        Dim smtp As New SmtpClient("scriptmail.intermedia.net")
        smtp.Send(oMsg)
        oMsg = Nothing
    end sub
</script>[/i]

Anybody got any ideas or care to assist?

EndFuncAutoIt is the shiznit. I love it.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...