App Inventor and the web component


How to send an email including attachment from within App Inventor (for php version until 5.3)

This solution is based on Scott's abandoned App Inventor Classic solution. I modified the php script completely and now use the PHP Mailer library to send the eMail. Thank you Scott for your work and thank you Santiago for being the sponsor of this tutorial!

Important Note: Sending HTML format and attachments only will work for php versions until 5.3!

With this example app, you can take a photo or pick an image and send it as attachment by email.

Preparation and Preconditions

  1. You will need a php server for the php script.
  2. You successfully got the postfile example running and you installed the mentioned PEAR package.
  3. Additionally download the PHP Mailer library and copy the unzipped library to your server.

Your directory structure will look like this in the end:

For questions about App Inventor,
please ask in the App Inventor community.Thank you.

Screenshots

Tested on 000webhost.com, which does not send the colors(?)

App Inventor Blocks

Put the URL to the php script on your php server in global variable strUrlPhpScript and use a random key key in global variable strACCESSKEY. In the php script this must match with the variable $ACCESSKEY.



PHP script

<?PHP
/*
 * Adjustments by Taifun
 * using Scott's abandoned Email with single attachment solution, which can be found at
 * https://groups.google.com/forum/#!msg/app-inventor-developers-library/-6XI0hWHqao/1zzc0jEm0iwJ
 *
 * Date: Feb 11, 2016
 * Contact: info@puravidaapps.com
 *
 * A Web component support service to allow emailing with an optional file attachment from App Inventor apps
 *
 * Usage  http://PATH TO THE SCRIPT/sendmail.php?p=(accesskey)&fi=(file name)&to=(to)&fr=(from)&su=(subject)&bo=(body)
 *
 * Version 2.0: complete cleanup of previous version and now using PHPMailer to send mails
 * Version 2.1: added HTML formatting
 *
 *
 * IMPORTANT NOTE: this solution works until PHP VERSION 5.3,
 *                 for newer php versions HTML formatting and sending attachments does not work anymore!
 */

/************************************CONFIG****************************************/

//SETTINGS//
//This code is something you set in the APP so random people cant use it.
$ACCESSKEY="ask768tz";

// this text will be displayed as sender
$SENDER="Pura Vida Mail Service";

/************************************CONFIG****************************************/

$to       = urldecode($_GET['to']);
$from     = urldecode($_GET['from']);
$subject  = urldecode($_GET['subject']);
$body     = "<html><body>" . urldecode($_GET['body']) . "</body></html>"; // HTML formatting added
$filename = urldecode($_GET['filename']);



if($_GET['p']==$ACCESSKEY){
  if ($filename != ''){
    // this is the workaround for file_get_contents(...)
    require_once (dirname(__FILE__).'/PHP_Compat-1.6.0a3/Compat/Function/file_get_contents.php');
    $data = php_compat_file_get_contents('php://input');          // note: some servers require 'php://stdin' instead!

    if (file_put_contents($filename, $data) === FALSE) die("Upload failed, send cancelled.");
    $file_size = filesize($filename);

    // limit attachment to 10MB
    if ($file_size > 10485760) die("> 10MB. Send cancelled.");

    // save file attachment info in log
    $emtemp = 'emailtemp.log';

    // this is the workaround for file_get_contents(...)
    $current = php_compat_file_get_contents($emtemp);
    $current .= gmdate("Y-m-d\TH:i:s") . ", " . $filename . ", " . round($file_size/1048576, 3) . "MB\n";
    file_put_contents($emtemp, $current);
  }

  // email solution using PHPMailer, http://github.com/PHPMailer/PHPMailer
  require_once (dirname(__FILE__).'/PHPMailer-master/class.phpmailer.php');
  $email = new PHPMailer();
  $email->From      = $from;
  $email->FromName  = $SENDER;
  $email->Subject   = $subject;
  $email->Body      = $body;
  $email->AddAddress( $to );
  $email->IsHTML(true);          // HTML formatting added

  if ($filename != ''){
    $email->AddAttachment( $filename , $filename );
  }

  if(!$email->send()) {
    header("HTTP/1.0 400 Bad Request");
    echo "Message could not be sent.";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
  }
  echo "Message has been sent";
} else {
  header("HTTP/1.0 400 Bad Request");
  echo "Access denied";     //reports if accesskey is wrong
}

?>

Notes

Questions and Answers

Q1:I'm trying using your "how to send an email" tutorial in your website but I really having a hard time understanding how to use it, can you help me? I don't see the instructions to making that php server.
A: How to get your php server for free: Let me recommend 000webhost.com as web hoster, see banner below. It is very easy to work with them: just ftp the script and the PEAR package to a directory and it works without a hassle, and the best is: their service is free! You have to use php://input instead of php://stdin with them.

1. To set up your server, click onto the banner to continue...

Web hosting

2. After setting up your server, use ftp to copy the scripts to your server, you can use a tool like WinSCP for that.

3. Start testing.

Download


Developing and maintaining snippets, tutorials and extensions for App Inventor takes a lot of time.
I hope it saved some of your time. If yes, then you might consider to donate a small amount!

Donation amount:

or donate some mBTC to Bitcoin Address:
1Jd8kXLHu2Vkuhi15TWHiQm4uE9AGPYxi8
Bitcoin

Thank you! Taifun
 

Download aia file for App Inventor
Download php script

Back to top of page ...

Creative Commons License
This work by Pura Vida Apps is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License
with attribution (name=Pura Vida Apps and link to the source site) required.


Home | Snippets | Tutorials | Extensions | Links | Search | Privacy Policy | Contact