Jump to content

Email automatically, how 2 configure it with my Data


meatsack
 Share

Recommended Posts

I would like to simply email automatically. I searched the "Example Scripts" section of the forum and found this code "Smtp Mailer" That Supports Html And Attachments. Also Secure SMTP mail (GMail) See link : http://www.autoitscript.com/forum/index.php?showtopic=23860&st=280&p=768773&hl=email&fromsearch=1&#entry768773

1. Is this code complete in that can I use it as a stand alone script?

2. Is this code so old that gmail no longer accepts mail messages from a script on someone's pc?

3. Where does my personal configuration data go, inside the quotation marks?

4. Can you suggest any other code that I, as a beginner, could play around with that copies a web page, opens an email message, paste what is on the clipboard, then sends the email.

I thank you in advanced.

; Setup email variables to send the merged email.
Func MailerSetup()
    $SmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
    $FromName = $cust_name ; name from who the email was sent
    $FromAddress = $cust_email ; address from where the mail should come
    $ToAddress = "";$merge_email ; destination address of the email - REQUIRED
    $Subject = $cust_subject ; subject from the email - can be anything you want it to be
    $Body = $template_import ; the messagebody from the mail - can be left blank but then you get a blank mail
    $AttachFiles = "" ; the file you want to attach- leave blank if not needed
    $CcAddress = "" ; address for cc - leave blank if not needed
    ; address for bcc - leave blank if not needed
    If $cust_bcc = "y" Then
        $BccAddress = $cust_email
    Else
        $BccAddress = ""
    EndIf
    $Importance = "Normal" ; send message priority: "High", "Normal", "Low"
    $Username = $cust_username ; username for the account used from where the mail gets sent - REQUIRED
    $Password = $cust_password ; password for the account used from where the mail gets sent - REQUIRED
    $IPPort = 465 ; port used for sending the mail. Use port 465 for GMAIL
    $ssl = 1 ; enables/disables secure socket layer sending - put to 1 if using https or GMAIL

;~  MsgBox(0, "Mail Settings", "$SmtpServer: |" & $SmtpServer & "|" & @CRLF & "$FromName: |" & $FromName & "|" & @CRLF & "$FromAddress: |" & $FromAddress & "|" & @CRLF & "$ToAddress: |" & $ToAddress & "|" & @CRLF & "$Subject: |" & $Subject & "|" & @CRLF & "$Body: |" & $Body & "|" & @CRLF & "$AttachFiles: |" & $AttachFiles & "|" & @CRLF & "$CcAddress: |" & $CcAddress & "|" & @CRLF & "$BccAddress: |" & $BccAddress & "|" & @CRLF & "$Importance: |" & $Importance & "|" & @CRLF & "$Username: |" & $Username & "|" & @CRLF & "$Password: |" & $Password & "|" & @CRLF & "$IPPort: |" & $IPPort & "|" & @CRLF & "$ssl: |" & $ssl & "|")

    ; Call the emailer
    $rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
    MsgBox(0, "Mailer Return", "Value: " & $rc & @CRLF & "@error: " & @error)
    If @error Then
        MsgBox(0, "Error sending message", "Error code: " & @error & @CRLF & "Description: " & $rc & @CRLF & @CRLF & "Subject: " & $Subject & @CRLF & "Message: " & $Body, 10)
        $error_pass = "Error sending message. Error code: " & @error & @CRLF & "Description: " & $rc & @CRLF & @CRLF & "Subject: " & $Subject & @CRLF & "Message: " & $Body
        Return $error_pass
    EndIf
    MsgBox(0, "", "Mail sent", 1)

    Return
EndFunc   ;==>MailerSetup

Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    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])
            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/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 Then $IPPort = 25
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    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
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Set email Importance
    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
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail = ""
EndFunc   ;==>_INetSmtpMailCom

; Com Error Handler for _INetSmtpMailCom
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
    SetError(1); something to check for when this function returns
    Return
EndFunc   ;==>MyErrFunc
Link to comment
Share on other sites

  • Developers

mmm ... looks familiar.

Why the questions without stating what your problem is? (Assuming you have porblems)

The posted code will not do much anyways.

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

mmm ... looks familiar.

Why the questions without stating what your problem is? (Assuming you have porblems)

The posted code will not do much anyways.

Ok, thanks for replying. I don't even know where my data goes so your script variables can operate.

I tried plugging in my email address and password but it didn't work.

Here is the code with my personal information all including the identifier 7 for ease of reading, is that where it should go?

; Setup email variables to send the merged email.
Func MailerSetup()
    $SmtpServer = "smtp.gmail.com" ; address for the smtp-server to use - REQUIRED
    $FromName = "Abel7" ; name from who the email was sent
    $FromAddress = $cust_email ; address from where the mail should come
    $ToAddress = "Bobby7";$merge_email ; destination address of the email - REQUIRED
    $Subject = $cust_subject ; subject from the email - can be anything you want it to be
    $Body = $template_import ; the messagebody from the mail - can be left blank but then you get a blank mail
    $AttachFiles = "" ; the file you want to attach- leave blank if not needed
    $CcAddress = "" ; address for cc - leave blank if not needed
    ; address for bcc - leave blank if not needed
    If $cust_bcc = "y" Then
        $BccAddress = $cust_email
    Else
        $BccAddress = ""
    EndIf
    $Importance = "Normal" ; send message priority: "High", "Normal", "Low"
    $Username = $cust_username ; username for the account used from where the mail gets sent - REQUIRED
    $Password = "passwordmine7" ; password for the account used from where the mail gets sent - REQUIRED
    $IPPort = 465 ; port used for sending the mail. Use port 465 for GMAIL
    $ssl = 1 ; enables/disables secure socket layer sending - put to 1 if using https or GMAIL

;~  MsgBox(0, "Mail Settings", "$SmtpServer: |" & $SmtpServer & "|" & @CRLF & "$FromName: |" & $FromName & "|" & @CRLF & "$FromAddress: |" & $FromAddress & "|" & @CRLF & "$ToAddress: |" & $ToAddress & "|" & @CRLF & "$Subject: |" & $Subject & "|" & @CRLF & "$Body: |" & $Body & "|" & @CRLF & "$AttachFiles: |" & $AttachFiles & "|" & @CRLF & "$CcAddress: |" & $CcAddress & "|" & @CRLF & "$BccAddress: |" & $BccAddress & "|" & @CRLF & "$Importance: |" & $Importance & "|" & @CRLF & "$Username: |" & $Username & "|" & @CRLF & "$Password: |" & $Password & "|" & @CRLF & "$IPPort: |" & $IPPort & "|" & @CRLF & "$ssl: |" & $ssl & "|")

    ; Call the emailer
    $rc = _INetSmtpMailCom($SmtpServer, $FromName, $FromAddress, $ToAddress, $Subject, $Body, $AttachFiles, $CcAddress, $BccAddress, $Importance, $Username, $Password, $IPPort, $ssl)
    MsgBox(0, "Mailer Return", "Value: " & $rc & @CRLF & "@error: " & @error)
    If @error Then
        MsgBox(0, "Error sending message", "Error code: " & @error & @CRLF & "Description: " & $rc & @CRLF & @CRLF & "Subject: " & $Subject & @CRLF & "Message: " & $Body, 10)
        $error_pass = "Error sending message. Error code: " & @error & @CRLF & "Description: " & $rc & @CRLF & @CRLF & "Subject: " & $Subject & @CRLF & "Message: " & $Body
        Return $error_pass
    EndIf
    MsgBox(0, "", "Mail sent", 1)

    Return
EndFunc   ;==>MailerSetup

Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Importance = "Normal", $s_Username = "", $s_Password = "", $IPPort = 25, $ssl = 0)
    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])
            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/smtpserver") = $s_SmtpServer
    If Number($IPPort) = 0 Then $IPPort = 25
    $objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
    ;Authenticated SMTP
    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
    ;Update settings
    $objEmail.Configuration.Fields.Update
    ; Set email Importance
    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
    ; Sent the Message
    $objEmail.Send
    If @error Then
        SetError(2)
        Return $oMyRet[1]
    EndIf
    $objEmail = ""
EndFunc   ;==>_INetSmtpMailCom

; Com Error Handler for _INetSmtpMailCom
Func MyErrFunc()
    $HexNumber = Hex($oMyError.number, 8)
    $oMyRet[0] = $HexNumber
    $oMyRet[1] = StringStripWS($oMyError.description, 3)
    ConsoleWrite("### COM Error !  Number: " & $HexNumber & "   ScriptLine: " & $oMyError.scriptline & "   Description:" & $oMyRet[1] & @LF)
    SetError(1); something to check for when this function returns
    Return
EndFunc   ;==>MyErrFunc
Link to comment
Share on other sites

  • Developers

Why not simply use the script example I posted in the original scrip in stead of trying the change things and moving it into a FUNC?

your MailerSetup() is never called and its not just the setup portion but also the "send email" part.

You do set the from email address ?

The to email address doesn't look valid.

Edited by 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

Why not simply use the script example I posted in the original scrip in stead of trying the change things and moving it into a FUNC?

Whoops, I apologize I did not use your clean example from your first post. I have since fixed that and tried to use your sample script but here is what happened:

Error code:2 Description:Theserver rejected the sender address. The server response was: 530 5.7.0 Must issuea a STARTTLS command first. 38sm14915930vws.14

I used this info for my configuration:

Incoming Mail (POP3) Server - requires SSL: pop.gmail.com

Outgoing Mail (SMTP) Server - requires TLS or SSL: smtp.gmail.com (use authentication)

Account Name: your full email address (including @gmail.com or @your_domain.com)

Email Address: your email address (username@gmail.com or username@your_domain.com)

Password: your Gmail password

I didn't make any changes to the configuration in these areas, those I left to your default as stated in the script:

Use SSL: Yes

Port: 995

Use Authentication: Yes

Port for TLS/STARTTLS: 587

Port for SSL: 465

Edited by meatsack
Link to comment
Share on other sites

  • Developers

Whoops, I apologize I did not use your clean example from your first post. I have since fixed that and tried to use your sample script but here is what happened:

"ERROR SENDING MESSAGE

Error code:2 Description:The transport failed to connect to the server."

It works fine for me with my GMail account so you must be doing something wrong in your script.

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

Sorry I gave you the wrong error code:

This is the current actual error code I am having trouble with:

Error code:2 Description:Theserver rejected the sender address. The server response was: 530 5.7.0 Must issuea a STARTTLS command first. 38sm14915930vws.14

This one I fixed after I followed your original code example:

"ERROR SENDING MESSAGE

Error code:2 Description:The transport failed to connect to the server."

Link to comment
Share on other sites

  • Developers

Did you set the $IPPort to 465 and $ssl to 1 ?

Edited by 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

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...