Greetings!

I was trying to translate an example code snippets from curl which demonstrates how to sending an email via smtp using curl api.The orignal c code works fine,but my D code failed at the beginnig when it is running.

Appreicate for the help to figure me out what's wrong with the code.


dmd v2.106.0 , VS community 2019,Win10 64bit
'''
call dmd -m64 curl.lib sendmail.d && sendmail

* No URL set!
curl_easy_perform() failed:URL using bad/illegal format or missing URL

'''

D source:

'''
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
import etc.c.curl;
//import std.conv;
//import std.string;

pragma(lib,"curl.lib");


const PASSWORD="real_pass_word";
const FROM_ADDR="sender_em...@add.ress";
const TO_ADDR="receiver_em...@add.ress";
const CC_ADDR="cc_copy_em...@add.ress";

const FROM_MAIL="sender_em...@add.ress";
const TO_MAIL="receiver_em...@add.ress";
const CC_MAIL="cc_copy_em...@add.ress";

const SMTP_VALUE="smtp://smtp-mail.outlook.com:25";//M$ outlook.com mail SMTP...


//static const char* payload_text=cast(const char*)(

    enum contents=(
        "To: " ~ TO_MAIL ~ "\r\n" ~
        "From: " ~ FROM_MAIL ~ "\r\n" ~
        "Cc: " ~ CC_MAIL  ~ "\r\n" ~
        "\r\n" ~
        "Subject: SMTP example message" ~ "\r\n" ~
        "\r\n" ~
        "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\r\nGood Luck!\r\nGood Luck!" ~ "\r\n");
        

static const char* payload_text= cast(const char*)contents.ptr;

    struct upload_status
    {
        size_t bytes_read;
    }

static size_t payload_source(char* ptr,size_t size,size_t nmemb,void* userp)
    {
        upload_status* upload_ctx=cast(upload_status*)userp;
        //char* data;
        size_t room=size*nmemb;
        
        if((size==0)||(nmemb==0)||((size*nmemb)<1))
        {
                return 0;
        }
        
        const char* data=&payload_text[upload_ctx.bytes_read];
        //const char* data=null;
//memcpy(cast(void*)data,cast(void*)payload_text,upload_ctx.bytes_read);
        if(data)
        {
                size_t len=strlen(data);
                if(room<len)
                {
                        len=room;
                }
                memcpy(ptr,data,len);
                upload_ctx.bytes_read+=len;
                
                return len;
        }
        return 0;
        
    }
    //extern(C) int main(int argc,char** argv) ---> betterC ? no
    void main()
    {

        CURL* curl;
        CURLcode res=CurlError.ok;
        curl_slist* recipents=null;
        upload_status upload_ctx={0};
        
        curl=curl_easy_init();
        
        if(curl)
        {
                curl_easy_setopt(curl,CurlProto.smtp,SMTP_VALUE.ptr);
                
                curl_easy_setopt(curl,CurlOption.username,FROM_MAIL.ptr);
                curl_easy_setopt(curl,CurlOption.mail_from,FROM_ADDR.ptr);
                curl_easy_setopt(curl,CurlOption.password,PASSWORD.ptr);
                
                recipents=curl_slist_append(recipents,TO_ADDR.ptr);
                recipents=curl_slist_append(recipents,CC_ADDR.ptr);
                curl_easy_setopt(curl,CurlOption.mail_rcpt,recipents);
                
curl_easy_setopt(curl,CurlOption.readfunction,&payload_source);
                curl_easy_setopt(curl,CurlOption.readdata,&upload_ctx);
                curl_easy_setopt(curl,CurlOption.verbose,1);
                curl_easy_setopt(curl,CurlOption.upload,1);
                curl_easy_setopt(curl,CurlOption.use_ssl,CurlUseSSL.all);
                
                curl_easy_setopt(curl,CurlOption.ssl_verifypeer,0);
                
                res=curl_easy_perform(curl);
                
                if(res!=CurlError.ok)
                {
fprintf(stderr,"curl_easy_perform() failed:%s\n",curl_easy_strerror(res));
                }
                
                curl_slist_free_all(recipents);
                
                
                curl_easy_cleanup(curl);
        }
        
        getchar();
        
        //return 0;
        
    }

'''



Reply via email to