Send attachment in mail (direct using absolute file path)

  • In requirements, there can be a case where you need to send attachment in mail.
  • Where as this attachment can be static which means already upload in media.
  • or you can make array of filepaths by fetching them through meta, collection meta or any specified space.
  • This filepath plays vital role in mail send.
  • Consider following code.
  • In above snippet,
    • attachment array consists of filepath key
    • this filepath can be one or multiple depending upon you attachment send requirement.
    • remember to mention absolute path of file as filepath value
    • for ex:
      • absolute path of xyz.pdf on https://www.getawesomestudio.com/ will be
      • /var/www/getawesomestudio.com/htdocs/wp-content/uploads/{year}/{month}/xyz.pdf
      • wp-content/uploads/{year}/{month}/xyz.pdf – this part you can copy from file URL itself.
  • Whole Email snippet will be like
  • Study above code and try it.
  • Happy coding!
Updated on Jun 19, 2021