【问题标题】:Send an email in react native以本机反应发送电子邮件
【发布时间】:2021-11-23 23:33:16
【问题描述】:

我是 react native 的新手。我的应用程序中有一个文本输入区域。我想将用户写的文本发送到我的 gmail 。我该怎么做?

这是我的代码:

import React, { useState } from "react"
import { View, Text, TextInput } from "react-native"
import ModalButton from "./ModalButton"
import styles from "./theme"



const StoreReview = (props) => {

    const [comment, setComment] = useState('')

    return (
        <View style={styles.container}>
            <Text style={styles.headerText}>
                We are very sorry about this.
                Can you share your experiences?
            </Text>
            <View style={styles.commentArea}>
                <TextInput
                    onChangeText={setComment}
                    value={comment}
                    multiline={true}
                />
            </View>
            <View style={styles.button}>
                <ModalButton
                    buttonText={'Send'}
                    onPress={() => {
                        console.log(comment, 'send that commet to gmail')
                    }}
                />
            </View>
        </View>
    )
}
export default StoreReview

【问题讨论】:

    标签: javascript react-native email


    【解决方案1】:

    您可以使用Linking 模块和mailto 方案来打开邮件应用程序。

    导入Linking 模块:

    import { Linking } from 'react-native';
    

    然后,使用它的openURL() 函数打开邮件应用:

    await Linking.openURL('mailto:youremail@example.com');
    

    你也可以使用canOpenURL()函数来检查链接是否被支持:

    const url = 'mailto:youremail@example.com';
    
    const supported = await Linking.canOpenURL(url);
    
    if (supported) {
      await Linking.openURL(url);
    }
    

    因此,在您的示例中,它看起来像:

    import React, { useState, useCallback } from 'react';
    import { View, Text, TextInput, Linking } from 'react-native';
    import ModalButton from './ModalButton';
    
    const StoreReview = (props) => {
        const [comment, setComment] = useState('');
    
        const emailAddress = 'youremail@example.com';
        const url = `mailto:${emailAddress}?body=${comment}`;
    
        const handlePress = useCallback(async () => {
            const supported = await Linking.canOpenURL(url);
    
            if (supported) {
                await Linking.openURL(url);
            }
        }, [url]);
    
        return (
            <View style={styles.container}>
                <Text style={styles.headerText}>
                    We are very sorry about this.
                    Can you share your experiences?
                </Text>
                <View style={styles.commentArea}>
                    <TextInput
                        onChangeText={setComment}
                        value={comment}
                        multiline={true}
                    />
                </View>
                <View style={styles.button}>
                    <ModalButton
                        buttonText="Send"
                        onPress={handlePress}
                    />
                </View>
            </View>
        );
    }
    
    export default StoreReview;
    

    编辑:我刚刚意识到您想在电子邮件正文中包含comment

    要设置电子邮件主题和正文,只需将您的 URL 从 mailto:youremail@example.com 更改为 mailto:youremail@example.com?subject=Hello&amp;body=World

    因此,在您的情况下,URL 应该是:

    const [comment, setComment] = useState('');
    
    const emailAddress = 'youremail@example.com';
    const url = `mailto:${emailAddress}?body=${comment}`;
    

    【讨论】:

    猜你喜欢
    • 2018-10-28
    • 2013-10-01
    • 2010-12-19
    • 2011-09-16
    • 2016-07-22
    • 2013-04-16
    相关资源
    最近更新 更多