A wrapper on top of MFMailComposeViewController from iOS and Mail Intent on android
npm install rn-better-mail``bash`
npm i --save rn-better-mail # npm syntax
`bash`
react-native link
* In android/setting.gradle
`gradle`
...
include ':RNMail', ':app'
project(':RNMail').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-mail/android')
* In android/app/build.gradle
`gradle`
...
dependencies {
...
compile project(':RNMail')
}
* if MainActivity extends Activity: register module in MainActivity.java
`java
import com.chirag.RNMail.*; // <--- import
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new RNMail()) // <------ add here
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN", null);
setContentView(mReactRootView);
}
......
}
`MainApplication.java
* else if MainActivity extends ReactActivity: register module in
`java
import com.chirag.RNMail.*; // <--- import
public class MainApplication extends Application implements ReactApplication {
....
@Override
protected List
return Arrays.
new MainReactPackage(),
new RNMail() // <------ add here
);
}
};
`
1. Run npm install react-native-mail --saveLibraries
2. Open your project in XCode, right click on and click Add
Files to "Your Project Name" (Screenshot) then navigate to node_modules/react-native-mail and select RNMail.xcodeproj (Screenshot).libRNMail.a
3. Add to Build Phases -> Link Binary With Librariesvar Mailer = require('NativeModules').RNMail;
(Screenshot).
4. Whenever you want to use it within React code now you can:
`typescript
import Mailer from 'rn-better-mail';
Mailer.mail(mailPreferences, callback);
interface mailPreferences {
subject: string,
recipients: string,
ccRecipients: string,
bccRecipients: string,
body: string | HTML,
isHtml: boolean // Be sure to set this flag to true, if the body is HTML
attachments: Array<{
path: string, // The absolute path of the file from which to read data
type: string, // Mime Type: jpg, png, doc, ppt, html, pdf
name: string
}>
}
`
On Android, the callback will only be called if an error occurs. The event` argument is unused!