{"id":29586638,"url":"https://github.com/basecamp/mail","last_synced_at":"2025-07-20T03:31:14.359Z","repository":{"id":65987460,"uuid":"85353153","full_name":"basecamp/mail","owner":"basecamp","description":null,"archived":false,"fork":false,"pushed_at":"2017-03-17T20:41:55.000Z","size":0,"stargazers_count":5,"open_issues_count":0,"forks_count":5,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-07-18T05:34:47.836Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/basecamp.png","metadata":{"files":{"readme":"README.mkd","changelog":"CHANGELOG.rdoc","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-17T20:44:09.000Z","updated_at":"2025-06-01T03:39:35.000Z","dependencies_parsed_at":"2023-02-19T20:45:55.635Z","dependency_job_id":null,"html_url":"https://github.com/basecamp/mail","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/basecamp/mail","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fmail","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fmail/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fmail/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fmail/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/basecamp","download_url":"https://codeload.github.com/basecamp/mail/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basecamp%2Fmail/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266063095,"owners_count":23870716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-07-20T03:30:53.876Z","updated_at":"2025-07-20T03:31:14.343Z","avatar_url":"https://github.com/basecamp.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"Mail\n====\n\nIntroduction\n------------\n\nMail is an internet library for Ruby that is designed to handle emails\ngeneration, parsing and sending in a simple, rubyesque manner.\n\nThe purpose of this library is to provide a single point of access to handle\nall email functions, including sending and receiving emails.  All network\ntype actions are done through proxy methods to Net::SMTP, Net::POP3 etc.\n\nBuilt from my experience with TMail, it is designed to be a pure ruby\nimplementation that makes generating, sending and parsing emails a no\nbrainer.\n\nIt is also designed form the ground up to work with Ruby 1.9.  This is because\nRuby 1.9 handles text encodings much more magically than Ruby 1.8.x and so\nthese features have been taken full advantage of in this library allowing\nMail to handle a lot more messages more cleanly than TMail.  Mail does run on\nRuby 1.8.x... it's just not as fun to code.\n\nFinally, Mail has been designed with a very simple object oriented system\nthat really opens up the email messages you are parsing, if you know what\nyou are doing, you can fiddle with every last bit of your email directly.\n\nCompatibility\n-------------\n\nMail is tested and works on the following platforms:\n\n* jruby-1.5.2 [ x86\\_64-java ]\n* ree-1.8.7-2010.02 [ x86\\_64 ]\n* ruby-1.8.6-p399 [ x86\\_64 ]\n* ruby-1.8.7-p302 [ x86\\_64 ]\n* ruby-1.9.2-p0 [ x86\\_64 ]\n\nDiscussion\n----------\n\nIf you want to discuss mail with like minded individuals, please subscribe to\nthe [Google Group](http://groups.google.com/group/mail-ruby).\n\nCurrent Capabilities of Mail\n----------------------------\n\n* RFC2822 Support, Reading and Writing\n* RFC2045-2049 Support for multipart emails\n* Support for creating multipart alternate emails\n* Support for reading multipart/report emails \u0026amp; getting details from such\n* Support for multibyte emails - needs quite a lot of work and testing\n* Wrappers for File, Net/POP3, Net/SMTP\n* Auto encoding of non US-ASCII header fields\n* Auto encoding of non US-ASCII bodies\n\nMail is RFC2822 compliant now, that is, it can parse and generate valid US-ASCII\nemails.  There are a few obsoleted syntax emails that it will have problems with, but\nit also is quite robust, meaning, if it finds something it doesn't understand it will\nnot crash, instead, it will skip the problem and keep parsing.  In the case of a header\nit doesn't understand, it will initialise the header as an optional unstructured\nfield and continue parsing.\n\nThis means Mail won't (ever) crunch your data (I think).\n\nYou can also create MIME emails.  There are helper methods for making a\nmultipart/alternate email for text/plain and text/html (the most common pair)\nand you can manually create any other type of MIME email.\n\nRoadmap\n-------\n\nNext TODO:\n\n* Improve MIME support for character sets in headers, currently works, mostly, needs\n  refinement.\n\nTesting Policy\n--------------\n\nBasically... we do BDD on Mail.  No method gets written in Mail without a\ncorresponding or covering spec.  We expect as a minimum 100% coverage\nmeasured by RCov.  While this is not perfect by any measure, it is pretty\ngood.  Additionally, all functional tests from TMail are to be passing before\nthe gem gets released.\n\nIt also means you can be sure Mail will behave correctly.\n\nAPI Policy\n----------\n\nNo API removals within a single point release.  All removals to be depreciated with\nwarnings for at least one MINOR point release before removal.\n\nAlso, all private or protected methods to be declared as such - though this is still I/P.\n\nInstallation\n------------\n\nInstallation is fairly simple, I host mail on rubygems, so you can just do:\n\n    # gem install mail\n\nEncodings\n---------\n\nIf you didn't know, handling encodings in Emails is not as straight forward as you\nwould hope.\n\nI have tried to simplify it some:\n\n1. All objects that can render into an email, have an `#encoded` method.  Encoded will\n   return the object as a complete string ready to send in the mail system, that is,\n   it will include the header field and value and CRLF at the end and wrapped as\n   needed.\n\n2. All objects that can render into an email, have a :decoded method.  Decoded will\n   return the object's \"value\" only as a string.  This means it will not include\n   the header fields (like 'To:' or 'Subject:').\n\n3. By default, calling \u003ccode\u003e#to_s\u003c/code\u003e on a container object will call its encoded\n   method, while \u003ccode\u003e#to_s\u003c/code\u003e on a field object will call it's decoded method.\n   So calling \u003ccode\u003e#to_s\u003c/code\u003e on a Mail object will return the mail, all encoded\n   ready to send, while calling \u003ccode\u003e#to_s\u003c/code\u003e on the From field or the body will\n   return the decoded value of the object. The header object of Mail is considered a\n   container. If you are in doubt, call \u003ccode\u003e#encoded\u003c/code\u003e, or \u003ccode\u003e#decoded\u003c/code\u003e\n   explicitly, this is safer if you are not sure.\n\n4. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will\n   provide decoded parameter values when you call the parameter names as methods against\n   the object.\n\n5. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will\n   provide encoded parameter values when you call the parameter names through the\n   \u003ccode\u003eobject.parameters['\u003cparameter_name\u003e']\u003c/code\u003e method call.\n\nContributing\n------------\n\nPlease do!  Contributing is easy in Mail:\n\n1. Check the Reference RFCs, they are in the References directory, so no excuses.\n2. Open a ticket on GitHub, maybe someone else has the problem too\n3. Make a fork of my GitHub repository\n4. Make a spec driven change to the code base\n5. Make sure it works and all specs pass, on Ruby versions 1.8.6, 1.8.7 and 1.9\n6. Update the README if needed to reflect your change / addition\n7. With all specs passing push your changes back to your fork\n8. Send me a pull request\n\nUsage\n-----\n\nAll major mail functions should be able to happen from the Mail module.\nSo, you should be able to just \u003ccode\u003erequire 'mail'\u003c/code\u003e to get started.\n\n### Making an email\n\n```ruby\nmail = Mail.new do\n  from    'mikel@test.lindsaar.net'\n  to      'you@test.lindsaar.net'\n  subject 'This is a test email'\n  body    File.read('body.txt')\nend\n\nmail.to_s #=\u003e \"From: mikel@test.lindsaar.net\\r\\nTo: you@...\n```\n\n### Making an email, have it your way:\n\n```ruby\nmail = Mail.new do\n  body File.read('body.txt')\nend\n\nmail['from'] = 'mikel@test.lindsaar.net'\nmail[:to]    = 'you@test.lindsaar.net'\nmail.subject = 'This is a test email'\n\nmail.to_s #=\u003e \"From: mikel@test.lindsaar.net\\r\\nTo: you@...\n```\n\n### Don't Worry About Message IDs:\n\n```ruby\nmail = Mail.new do\n  to   'you@test.lindsaar.net'\n  body 'Some simple body'\nend\n\nmail.to_s =~ /Message\\-ID: \u003c[\\d\\w_]+@.+.mail/ #=\u003e 27\n```\n\nMail will automatically add a Message-ID field if it is missing and\ngive it a unique, random Message-ID along the lines of:\n\n    \u003c4a7ff76d7016_13a81ab802e1@local.fqdn.mail\u003e\n\n### Or do worry about Message-IDs:\n\n```ruby\nmail = Mail.new do\n  to         'you@test.lindsaar.net'\n  message_id '\u003cThisIsMyMessageId@some.domain.com\u003e'\n  body       'Some simple body'\nend\n\nmail.to_s =~ /Message\\-ID: \u003cThisIsMyMessageId@some.domain.com\u003e/ #=\u003e 27\n```\n\nMail will take the message_id you assign to it trusting that you know\nwhat you are doing.\n\n### Sending an email:\n\nMail defaults to sending via SMTP to local host port 25.  If you have a\nsendmail or postfix daemon running on on this port, sending email is as\neasy as:\n\n```ruby\nMail.deliver do\n   from    'me@test.lindsaar.net'\n   to      'you@test.lindsaar.net'\n   subject 'Here is the image you wanted'\n   body    File.read('body.txt')\n  add_file '/full/path/to/somefile.png'\nend\n```\n\nor\n\n```ruby\nmail = Mail.new do\n  from     'me@test.lindsaar.net'\n  to       'you@test.lindsaar.net'\n  subject  'Here is the image you wanted'\n  body     File.read('body.txt')\n  add_file :filename =\u003e 'somefile.png', :content =\u003e File.read('/somefile.png')\nend\n\nmail.deliver!\n```\n\nSending via sendmail can be done like so:\n\n```ruby\nmail = Mail.new do\n  from     'me@test.lindsaar.net'\n  to       'you@test.lindsaar.net'\n  subject  'Here is the image you wanted'\n  body     File.read('body.txt')\n  add_file :filename =\u003e 'somefile.png', :content =\u003e File.read('/somefile.png')\nend\n\nmail.delivery_method :sendmail\n\nmail.deliver\n```\n\n### Getting emails from a pop server:\n\nYou can configure Mail to receive email using \u003ccode\u003eretriever_method\u003c/code\u003e\nwithin \u003ccode\u003eMail.defaults\u003c/code\u003e:\n\n```ruby\nMail.defaults do\n  retriever_method :pop3, :address    =\u003e \"pop.gmail.com\",\n                          :port       =\u003e 995,\n                          :user_name  =\u003e '\u003cusername\u003e',\n                          :password   =\u003e '\u003cpassword\u003e',\n                          :enable_ssl =\u003e true\nend\n```\n\nYou can access incoming email in a number of ways.\n\nThe most recent email:\n\n```ruby\nMail.all    #=\u003e Returns an array of all emails\nMail.first  #=\u003e Returns the first unread email\nMail.last   #=\u003e Returns the first unread email\n```\n\nThe first 10 emails sorted by date in ascending order:\n\n```ruby\nemails = Mail.find(:what =\u003e :first, :count =\u003e 10, :order =\u003e :asc)\nemails.length #=\u003e 10\n```\n\nOr even all emails:\n\n```ruby\nemails = Mail.all\nemails.length #=\u003e LOTS!\n```\n\n\n### Reading an Email\n\n```ruby\nmail = Mail.read('/path/to/message.eml')\n\nmail.envelope.from   #=\u003e 'mikel@test.lindsaar.net'\nmail.from.addresses  #=\u003e ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']\nmail.sender.address  #=\u003e 'mikel@test.lindsaar.net'\nmail.to              #=\u003e 'bob@test.lindsaar.net'\nmail.cc              #=\u003e 'sam@test.lindsaar.net'\nmail.subject         #=\u003e \"This is the subject\"\nmail.date.to_s       #=\u003e '21 Nov 1997 09:55:06 -0600'\nmail.message_id      #=\u003e '\u003c4D6AA7EB.6490534@xxx.xxx\u003e'\nmail.body.decoded    #=\u003e 'This is the body of the email...\n```\n\nMany more methods available.\n\n### Reading a Multipart Email\n\n```ruby\nmail = Mail.read('multipart_email')\n\nmail.multipart?          #=\u003e true\nmail.parts.length        #=\u003e 2\nmail.preamble            #=\u003e \"Text before the first part\"\nmail.epilogue            #=\u003e \"Text after the last part\"\nmail.parts.map { |p| p.content_type }  #=\u003e ['text/plain', 'application/pdf']\nmail.parts.map { |p| p.class }         #=\u003e [Mail::Message, Mail::Message]\nmail.parts[0].content_type_parameters  #=\u003e {'charset' =\u003e 'ISO-8859-1'}\nmail.parts[1].content_type_parameters  #=\u003e {'name' =\u003e 'my.pdf'}\n```\n\nMail generates a tree of parts.  Each message has many or no parts.  Each part\nis another message which can have many or no parts.\n\nA message will only have parts if it is a multipart/mixed or related/mixed\ncontent type and has a boundary defined.\n\n### Writing and sending a multipart/alternative (html and text) email\n\nMail makes some basic assumptions and makes doing the common thing as\nsimple as possible.... (asking a lot from a mail library)\n\n```ruby\nmail = Mail.deliver do\n  to      'nicolas@test.lindsaar.net.au'\n  from    'Mikel Lindsaar \u003cmikel@test.lindsaar.net.au\u003e'\n  subject 'First multipart email sent with Mail'\n\n  text_part do\n    body 'This is plain text'\n  end\n\n  html_part do\n    content_type 'text/html; charset=UTF-8'\n    body '\u003ch1\u003eThis is HTML\u003c/h1\u003e'\n  end\nend\n```\n\nMail then delivers the email at the end of the block and returns the\nresulting Mail::Message object, which you can then inspect if you\nso desire...\n\n```\nputs mail.to_s #=\u003e\n\nTo: nicolas@test.lindsaar.net.au\nFrom: Mikel Lindsaar \u003cmikel@test.lindsaar.net.au\u003e\nSubject: First multipart email sent with Mail\nContent-Type: multipart/alternative;\n  boundary=--==_mimepart_4a914f0c911be_6f0f1ab8026659\nMessage-ID: \u003c4a914f12ac7e_6f0f1ab80267d1@baci.local.mail\u003e\nDate: Mon, 24 Aug 2009 00:15:46 +1000\nMime-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\n\n----==_mimepart_4a914f0c911be_6f0f1ab8026659\nContent-ID: \u003c4a914f12c8c4_6f0f1ab80268d6@baci.local.mail\u003e\nDate: Mon, 24 Aug 2009 00:15:46 +1000\nMime-Version: 1.0\nContent-Type: text/plain\nContent-Transfer-Encoding: 7bit\n\nThis is plain text\n----==_mimepart_4a914f0c911be_6f0f1ab8026659\nContent-Type: text/html; charset=UTF-8\nContent-ID: \u003c4a914f12cf86_6f0f1ab802692c@baci.local.mail\u003e\nDate: Mon, 24 Aug 2009 00:15:46 +1000\nMime-Version: 1.0\nContent-Transfer-Encoding: 7bit\n\n\u003ch1\u003eThis is HTML\u003c/h1\u003e\n----==_mimepart_4a914f0c911be_6f0f1ab8026659--\n```\n\nMail inserts the content transfer encoding, the mime version,\nthe content-id's and handles the content-type and boundary.\n\nMail assumes that if your text in the body is only us-ascii, that your\ntransfer encoding is 7bit and it is text/plain.  You can override this\nby explicitly declaring it.\n\n### Making Multipart/Alternate, without a block\n\nYou don't have to use a block with the text and html part included, you\ncan just do it declaratively.  However, you need to add Mail::Parts to\nan email, not Mail::Messages.\n\n```ruby\nmail = Mail.new do\n  to      'nicolas@test.lindsaar.net.au'\n  from    'Mikel Lindsaar \u003cmikel@test.lindsaar.net.au\u003e'\n  subject 'First multipart email sent with Mail'\nend\n\ntext_part = Mail::Part.new do\n  body 'This is plain text'\nend\n\nhtml_part = Mail::Part.new do\n  content_type 'text/html; charset=UTF-8'\n  body '\u003ch1\u003eThis is HTML\u003c/h1\u003e'\nend\n\nmail.text_part = text_part\nmail.html_part = html_part\n```\n\nResults in the same email as done using the block form\n\n### Getting error reports from an email:\n\n```ruby\n@mail = Mail.read('/path/to/bounce_message.eml')\n\n@mail.bounced?         #=\u003e true\n@mail.final_recipient  #=\u003e rfc822;mikel@dont.exist.com\n@mail.action           #=\u003e failed\n@mail.error_status     #=\u003e 5.5.0\n@mail.diagnostic_code  #=\u003e smtp;550 Requested action not taken: mailbox unavailable\n@mail.retryable?       #=\u003e false\n```\n\n### Attaching and Detaching Files\n\nYou can just read the file off an absolute path, Mail will try\nto guess the mime_type and will encode the file in Base64 for you.\n\n```ruby\n@mail = Mail.new\n@mail.add_file(\"/path/to/file.jpg\")\n@mail.parts.first.attachment? #=\u003e true\n@mail.parts.first.content_transfer_encoding.to_s #=\u003e 'base64'\n@mail.attachments.first.mime_type #=\u003e 'image/jpg'\n@mail.attachments.first.filename #=\u003e 'file.jpg'\n@mail.attachments.first.decoded == File.read('/path/to/file.jpg') #=\u003e true\n```\n\nOr You can pass in file_data and give it a filename, again, mail\nwill try and guess the mime_type for you.\n\n```ruby\n@mail = Mail.new\n@mail.attachments['myfile.pdf'] = File.read('path/to/myfile.pdf')\n@mail.parts.first.attachment? #=\u003e true\n@mail.attachments.first.mime_type #=\u003e 'application/pdf'\n@mail.attachments.first.decoded == File.read('path/to/myfile.pdf') #=\u003e true\n```\n\nYou can also override the guessed MIME media type if you really know better\nthan mail (this should be rarely needed)\n\n```ruby\n@mail = Mail.new\nfile_data = File.read('path/to/myfile.pdf')\n@mail.attachments['myfile.pdf'] = { :mime_type =\u003e 'application/x-pdf',\n                                    :content =\u003e File.read('path/to/myfile.pdf') }\n@mail.parts.first.mime_type #=\u003e 'application/x-pdf'\n```\n\nOf course... Mail will round trip an attachment as well\n\n```ruby\n@mail = Mail.new do\n  to      'nicolas@test.lindsaar.net.au'\n  from    'Mikel Lindsaar \u003cmikel@test.lindsaar.net.au\u003e'\n  subject 'First multipart email sent with Mail'\n\n  text_part do\n    body 'Here is the attachment you wanted'\n  end\n\n  html_part do\n    content_type 'text/html; charset=UTF-8'\n    body '\u003ch1\u003eFunky Title\u003c/h1\u003e\u003cp\u003eHere is the attachment you wanted\u003c/p\u003e'\n  end\n\n  add_file '/path/to/myfile.pdf'\nend\n\n@round_tripped_mail = Mail.new(@mail.encoded)\n\n@round_tripped_mail.attachments.length #=\u003e 1\n@round_tripped_mail.attachments.first.filename #=\u003e 'myfile.pdf'\n```\n\nUsing Mail with Testing or Spec'ing Libraries\n---------------------------------------------\n\nIf mail is part of your system, you'll need a way to test it without actually\nsending emails, the TestMailer can do this for you.\n\n```\nrequire 'mail'\n=\u003e true\nMail.defaults do\n  delivery_method :test\nend\n=\u003e #\u003cMail::Configuration:0x19345a8 @delivery_method=Mail::TestMailer\u003e\nMail::TestMailer.deliveries\n=\u003e []\nMail.deliver do\n  to 'mikel@me.com'\n  from 'you@you.com'\n  subject 'testing'\n  body 'hello'\nend\n=\u003e #\u003cMail::Message:0x19284ec ...\nMail::TestMailer.deliveries.length\n=\u003e 1\nMail::TestMailer.deliveries.first\n=\u003e #\u003cMail::Message:0x19284ec ...\nMail::TestMailer.deliveries.clear\n=\u003e []\n```\n\nExcerpts from TREC Spam Corpus 2005\n-----------------------------------\n\nThe spec fixture files in spec/fixtures/emails/from_trec_2005 are from the\n2005 TREC Public Spam Corpus. They remain copyrighted under the terms of\nthat project and license agreement. They are used in this project to verify\nand describe the development of this email parser implementation.\n\nhttp://plg.uwaterloo.ca/~gvcormac/treccorpus/\n\nThey are used as allowed by 'Permitted Uses, Clause 3':\n\n    \"Small excerpts of the information may be displayed to others\n     or published in a scientific or technical context, solely for\n     the purpose of describing the research and development and\n     related issues.\"\n\n     -- http://plg.uwaterloo.ca/~gvcormac/treccorpus/\n\nLicense\n-------\n\n(The MIT License)\n\nCopyright (c) 2009, 2010, 2011\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasecamp%2Fmail","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbasecamp%2Fmail","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasecamp%2Fmail/lists"}