# Img src while sending email ruby cgi

**URL:** <https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427>\
**Category:** ruby-talk\
**Created:** [5 October 2012 22:10 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427 "2012-10-05T22:10:23Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 October 2012 22:10 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/1 "2012-10-05T22:10:23Z")

</div>

Hi  
I am sending email by ruby cgi.  
Sending is fine by html

but when i am trying to insert a image , it does not work.. wondering  
what i am doing wrong

here is the code :

message = \<\<MESSAGE\_END

> **···**
>
> From:test user \<[test@test.com](mailto:test@test.com)\>  
> To: testuser \<[test@test.com](mailto:test@test.com)\>  
> MIME-Version: 1.0  
> Content-type: text/html  
> Subject: SMTP e-mail test
> 
> This is an e-mail message to be sent in HTML format
> 
> \<p\>\<img src="bay1.gif" alt="bay1.gif" width="32" height="32"\>\</p\>
> 
> MESSAGE\_END
> 
> Net::SMTP.start('[smtp.myemail.com](http://smtp.myemail.com)') do |smtp|  
> &nbsp;&nbsp;smtp.send\_message message, 'test@test.com',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'test1@test1.com  
> end
> 
> email is going. but image is not attaching ..
> 
> Can any one please help me .
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [5 October 2012 22:38 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/2 "2012-10-05T22:38:14Z")

</div>

You are \*not\* sending an email with an attached image,  
you are only sending an email in HTML format,  
which happens to contain some link.

I have never used Net::SMTP, so I can not tell you  
how you would actually attach an image to an email.  
Try searching for 'ruby net smtp attachment' or so...

> **···**
>
> Am 06.10.2012 00:10, schrieb Ferdous ara:
> 
> > Hi  
> > I am sending email by ruby cgi.  
> > Sending is fine by html
> > 
> > but when i am trying to insert a image , it does not work.. wondering  
> > what i am doing wrong
> > 
> > here is the code :
> > 
> > message = \<\<MESSAGE\_END  
> > From:test user \<test@test.com\>  
> > To: testuser \<test@test.com\>  
> > MIME-Version: 1.0  
> > Content-type: text/html  
> > Subject: SMTP e-mail test
> > 
> > This is an e-mail message to be sent in HTML format
> > 
> > \<p\>\<img src="bay1.gif" alt="bay1.gif" width="32" height="32"\>\</p\>
> > 
> > MESSAGE\_END
> > 
> > Net::SMTP.start('smtp.myemail.com') do |smtp|  
> > &nbsp;&nbsp;&nbsp;smtp.send\_message message, 'test@test.com',  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'test1@test1.com  
> > end
> > 
> > email is going. but image is not attaching ..
> > 
> > Can any one please help me .
> 
> --  
> \<[https://github.com/stomar/&gt](https://github.com/stomar/&gt);

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 October 2012 22:45 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/3 "2012-10-05T22:45:59Z")

</div>

Hi  
Instead of attachment, Can i not imbedded the image ??

if i do attachment, Then it will sent as Attachment

but what i am trying to do is, the image will be seen in body of the  
Email rather then attachment.

How will i do that ??

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [6 October 2012 09:37 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/4 "2012-10-06T09:37:44Z")

</div>

Hi  
I am found this from this link

[http://www.ruby-forum.com/topic/1159192](http://www.ruby-forum.com/topic/1159192)

So i have done this

require 'net/smtp'  
require 'fileutils'  
require 'rubygems'  
require 'base64'

fn = 'bay1.gif'  
&nbsp;&nbsp;src = "#{Base64.encode64(File.read(fn))}"  
&nbsp;&nbsp;#new\_tag = "\<img src='#{src}'\>"

message = \<\<MESSAGE\_END

> **···**
>
> From:test user \<[test@test.com](mailto:test@test.com)\>  
> To: testuser \<[test@test.com](mailto:test@test.com)\>  
> MIME-Version: 1.0  
> Content-type: text/html  
> Subject: SMTP e-mail test
> 
> This is an e-mail message to be sent in HTML format
> 
> \<p\>\<img src="#{src}" alt="bay1.gif" width="32" height="32"\>\</p\>
> 
> MESSAGE\_END
> 
> Net::SMTP.start('[smtp.myemail.com](http://smtp.myemail.com)') do |smtp|  
> &nbsp;&nbsp;smtp.send\_message message, 'test@test.com',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'test1@test1.com  
> end
> 
> but still now luck ..  
> Please give me little bit more light
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [6 October 2012 10:04 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/5 "2012-10-06T10:04:47Z")

</div>

ok to make this same as that link  
i have done this

require 'net/smtp'  
require 'fileutils'  
require 'rubygems'  
require 'base64'

fn = 'test.png'  
&nbsp;&nbsp;src = "data:image/png;base64,#{Base64.encode64(File.read(fn))}"

message = \<\<MESSAGE\_END

> **···**
>
> From:test user \<[test@test.com](mailto:test@test.com)\>  
> To: testuser \<[test@test.com](mailto:test@test.com)\>  
> MIME-Version: 1.0  
> Content-type: text/html  
> Subject: SMTP e-mail test
> 
> This is an e-mail message to be sent in HTML format
> 
> \<p\>\<img src="#{src}" alt="test.png" width="32" height="32"\>\</p\>
> 
> MESSAGE\_END
> 
> Net::SMTP.start('[smtp.myemail.com](http://smtp.myemail.com)') do |smtp|  
> &nbsp;&nbsp;smtp.send\_message message, 'test@test.com',  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'test1@test1.com  
> end
> 
> still now luck
> 
> Please help me with this
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [6 October 2012 12:14 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/6 "2012-10-06T12:14:58Z")

</div>

also i found this

[http://rubydoc.info/gems/mime/frames](http://rubydoc.info/gems/mime/frames)

i have install gem install mime

but dont understand. how to call this

require 'rubygems'  
require 'mime'

i am trying "Simple text/plain RFC822 email"

and running the script  
it saying

uninitialized constant Message (NameError)

question is, how will call this ?? (require 'mime') it does not work  
this way ..

i am looking in net for long time .. please some one help me

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [6 October 2012 13:09 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/7 "2012-10-06T13:09:08Z")

</div>

HI yes  
i can see in html format

[http://fosiul.com/test.html](http://fosiul.com/test.html)

cat test.html

> **···**
>
> From: Fosiul alam \<[test@test.com](mailto:test@test.com)\>  
> To: Fosiul Alam \<[test@gmail.com](mailto:test@gmail.com)\>  
> MIME-Version: 1.0  
> Content-type: text/html  
> Subject: SMTP e-mail test
> 
> This is an e-mail message to be sent in HTML format
> 
> \<img  
> src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAAAAABVicqIAAAACXBIWXMAAAsT  
> AAALEwEAmpwYAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAAB6JQAAgIMAAPn/  
> AACA6QAAdTAAAOpgAAA6mAAAF2+SX8VGAAABc0lEQVR42uyY4U3DMBCFPxAL  
> eIWs4BWOEbxCVggjuCPQEcoI8QjxCHSEeITHjxRBgSJaghDi7l+iyJ/uvXsn  
> K1fi5+sah5xRV+6Jy/W35XKIQzzxDvmLkJs3z7WFePrrVonhAoqOyzCdrhFG  
> nV+/IFerjVZiAB5qtS4tr7cNLEKtUD+T80tyjQCM0mwA9JL0uJyaJQM+lfNE  
> HUMmCwSbpARDHiBLMuiHDh41RIjDdyHPxo+wk5TpJMFOmgN5ZeMLXQIS+wqw  
> b4RxTKvl5JAW2u1hFCA93N2ZmX1jvD6U6+XALM09ADZdLNfNCXZaTqaDcJ9L  
> LbVsdut20hNfTVwvSRlWNj5RN0DdbCqtbMtzU6t1EmySDOLQB+IsdZCycRjh  
> NXKyO0q8zZKmZe8OkhQvSvy7i0Sp9AGoZdlXQCt7SB1A29Kdnxe/rTjEIf6z  
> wOVyTxzixjvEd5cb7xDfwt6JQxziOfFOfLrcE/fEp8s9+QdyPQ0AKbnfw2X3  
> XB8AAAAASUVORK5CYII=  
> '\>
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [6 October 2012 14:46 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/8 "2012-10-06T14:46:34Z")

</div>

no  
i was checking in gmail .

but i dont see the 64 bit code ..

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [5 October 2012 23:06 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/9 "2012-10-05T23:06:25Z")

</div>

you could for example  
- link to an image that is available through the internet  
- embed the image into the html (there are tutorials on how to do this)

> **···**
>
> Am 06.10.2012 00:45, schrieb Ferdous ara:
> 
> > Hi  
> > Instead of attachment, Can i not imbedded the image ??
> > 
> > if i do attachment, Then it will sent as Attachment
> > 
> > but what i am trying to do is, the image will be seen in body of the  
> > Email rather then attachment.
> > 
> > How will i do that ??
> > 
> > Thanks
> 
> --  
> \<[https://github.com/stomar/&gt](https://github.com/stomar/&gt);

---

<div class="post-metadata">

**Author:** ![Henry\_Maddocks1](https://avatars.discourse-cdn.com/v4/letter/h/ba9def/32.png) [@Henry\_Maddocks1](https://rubytalk.org/u/Henry_Maddocks1)\
**Post date:** [6 October 2012 00:22 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/10 "2012-10-06T00:22:35Z")

</div>

Sending images, or other non-text data, in an email has to be done as an attachment. The difference is whether it is an \_inline\_ attachment or not. You want an inline attachment.

Sending emails with attachments is way more complicated than what you have tried. Do a search for multipart email format and inline attachments.

Henry

> **···**
>
> On 6/10/2012, at 11:45 AM, Ferdous ara wrote:
> 
> > Hi  
> > Instead of attachment, Can i not imbedded the image ??
> > 
> > if i do attachment, Then it will sent as Attachment
> > 
> > but what i am trying to do is, the image will be seen in body of the  
> > Email rather then attachment.

---

<div class="post-metadata">

**Author:** ![11142](https://avatars.discourse-cdn.com/v4/letter/1/e0b2c6/32.png) [@11142](https://rubytalk.org/u/11142)\
**Post date:** [6 October 2012 10:19 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/11 "2012-10-06T10:19:44Z")

</div>

What do you mean "no luck"? Is the message sent and received, but  
without the image? Does the code crash? Does it not arrive at all?  
Maybe it's rejected by a spam filter?

-- Matma Rex

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [6 October 2012 14:26 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/12 "2012-10-06T14:26:23Z")

</div>

Do you see the same in the source code of your received email?

> **···**
>
> Am 06.10.2012 15:09, schrieb Ferdous ara:
> 
> > HI yes  
> > i can see in html format
> 
> --  
> \<[https://github.com/stomar/&gt](https://github.com/stomar/&gt);

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [6 October 2012 10:28 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/13 "2012-10-06T10:28:34Z")

</div>

Bartosz Dziewoński wrote in post #1078806:

> What do you mean "no luck"? Is the message sent and received, but

Hi the email is coming, no error in code  
but there is not image,

i can see the box, with alt 'test.png' in the email

but i dont see the image ..

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [6 October 2012 10:38 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/14 "2012-10-06T10:38:15Z")

</div>

you could try `puts message` instead of sending it.  
is the image included in the output string?

did you try saving the HTML part of the message  
into a file and open it with a browser?

> **···**
>
> Am 06.10.2012 12:28, schrieb Ferdous ara:
> 
> > Bartosz Dziewoński wrote in post #1078806:
> > 
> > > What do you mean "no luck"? Is the message sent and received, but
> > 
> > Hi the email is coming, no error in code  
> > but there is not image,
> > 
> > i can see the box, with alt 'test.png' in the email
> > 
> > but i dont see the image ..
> 
> --  
> \<[https://github.com/stomar/&gt](https://github.com/stomar/&gt);

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [6 October 2012 10:45 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/15 "2012-10-06T10:45:21Z")

</div>

unknown wrote in post #1078810:

> > but i dont see the image ..
> 
> you could try `puts message` instead of sending it.  
> is the image included in the output string?

Sorry did not understand what you meant..

> did you try saving the HTML part of the message  
> into a file and open it with a browser?

how you want me to save this in to html ?? by using ruby cgi ??

> **···**
>
> > Am 06.10.2012 12:28, schrieb Ferdous ara:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [6 October 2012 12:51 UTC](https://rubytalk.org/t/img-src-while-sending-email-ruby-cgi/66427/16 "2012-10-06T12:51:27Z")

</div>

> unknown wrote in post #1078810:
> 
> > > but i dont see the image ..
> > 
> > you could try `puts message` instead of sending it.  
> > is the image included in the output string?
> 
> Sorry did not understand what you meant..

First, look at the source code of the email you received.  
If the image is embedded but not displayed, it could be  
a problem with your email program.

If the image is not embedded in the first place you have to  
poke into the code and try to figure out what is going on...

The simplest way to debug some code is including puts statements  
to display the values of your variables, like src, message.

> > did you try saving the HTML part of the message  
> > into a file and open it with a browser?
> 
> how you want me to save this in to html ?? by using ruby cgi ??

This has nothing to do with cgi. Just change your program like shown  
below and run it. This saves the HTML snippet of your message to a file  
(instead of sending it). Then open the created HTML file in your  
browser. Does it display your image? (In my case it does.)

&nbsp;&nbsp;&nbsp;...

&nbsp;&nbsp;&nbsp;message = \<\<MESSAGE\_END  
&nbsp;&nbsp;&nbsp;\<p\>\<img src="#{src}" alt="test.png" width="32" height="32"\>\</p\>  
&nbsp;&nbsp;&nbsp;MESSAGE\_END

&nbsp;&nbsp;&nbsp;File.open('test.html', 'w') {|f| f.puts message }

> **···**
>
> Am 06.10.2012 12:45, schrieb Ferdous ara:
> 
> > > Am 06.10.2012 12:28, schrieb Ferdous ara:
> 
> --  
> \<[https://github.com/stomar/&gt](https://github.com/stomar/&gt);
