angelos3lex / react-native-smtp-mailer

Send emails by connecting to smtp server+attachments, using android javamail and ios mailcore2
39 stars 32 forks source link

Struggeling with android attachments #29

Open maendamedia opened 4 years ago

maendamedia commented 4 years ago

Hello,

Like your module and iOS works great. For android it is not so easy :)

I let the user select some images using the react-native-image-crop module and store them in an array. This array will be filtered and attached tot the smtp email .

As mentioned, iOS works great, but on android i get the famous IOExeption.

What am i doing wrong here:

const attachmentPaths = [];
    const attachmentNames = [];
    const attachmentTypes = [];
    var re = /(?:\.([^.]+))?$/;

    for (var i = 0; i < image1.length; i++) {
      let filePath = image1[i].path;
      let fileName = filePath.substring(filePath.lastIndexOf('/') + 1);

      attachmentPaths[i] = filePath;
      if (Platform.OS === 'android') {
        attachmentNames[i] = fileName;
        attachmentTypes[i] = re.exec(filePath)[1];
        console.log(attachmentTypes);
      }
    }

    RNSmtpMailer.sendMail({
      mailhost: cs.mailServerHost,
      port: cs.mailServerPort,
      username: cs.mailServerUserName,
      ssl: true,
      password: cs.mailServerPassword,
      from: 'myactualemail',
      recipients: 'receipt emaill',
      bcc: ['bccemail address'],
      subject: 'E-mail vfrom app app',
      htmlBody: `my html text`,
      **attachmentPaths: attachmentPaths,**
      **attachmentNames: attachmentNames,** //only used in android, these are renames of original files. in ios filenames will be same as specified in path. In ios-only application, leave it empty: attachmentNames:[]
      attachmentTypes: attachmentTypes, //needed for android, in ios-only application, leave it empty: attachmentTypes:[]. Generally every img(either jpg, png, jpeg or whatever) file should have "img", and every other file should have its corresponding type.
    })
      .then(success => {alert(success)} ......rest of code
angelos3lex commented 4 years ago

@maendamedia Unfortunately this lib doesn't yet support loading attachments that are not in the filesystem of the device. The crop library you are using, has a writeTempFile (ios only) option that you are probably using, but works only on ios. So in android, there is no tmp file in the filesystem, and it can't be loaded. A workaround, will be to save the file yourself in a tmp folder, and then load it with this library.

LuizFBrisighello commented 4 years ago

@angelos3lex Just like @maendamedia, Im using react-native-image-crop to take a photo, after that I save the img using @react-native-community/cameraroll and then I call smtp but I get this error IOException while sending message

I guess it relates to #23

The path that is returned from saving the photo to the library is: content://media/external_primary/images/media/9624

My code:

            ImagePicker.openCamera({
              width: 300),
              height: 400)
            }).then(image => {
              console.log(image)
              CameraRoll.saveToCameraRoll(image.path).then(path => {
                console.log(path)
                    RNSmtpMailer.sendMail({
                        mailhost: 'email-smtp.us-east-1.amazonaws.com',
                        port: '587',
                        ssl: false,
                        username: 'host.userName',
                        password: 'host.password',
                        from: 'host.email@gmail.com',
                        recipients: 'my.email@gmail.com',
                        subject: service,
                        htmlBody: '<p>body</p>',
                        attachmentPaths: [path + '.jpg'],
                        attachmentNames: ['image.jpg'],
                        attachmentTypes: ['img']
            })
            .then(success => {
                if (step === curStep) {
                console.log(success)
                dispatch(nextStep())
                alert(success)
        }
      })
      .catch(err => console.log(err))
              })
            }).catch(error => { console.log(error) })

Am I missing something here?

angelos3lex commented 4 years ago

Why do you append .jpg on the path and you don't trust exactly what the camera-roll lib returns? Could you try that as well?

LuizFBrisighello commented 4 years ago

@angelos3lex The outcome is the same, I thought because camera-roll's path doesnt specify file type it would fail. I'll try react-native-fs and get back to you

yasirwahid commented 4 years ago

@angelos3lex @LuizFBrisighello im having the same issue with android. showing me IOexception while ios is working fine any leads? the path is correct . im picking an image from gallery through react-native-image-picker, not working with camera either.

LuizFBrisighello commented 4 years ago

@yasirwahid I ended up moving to other tasks without solving the attachments issue. react-native-fs doesnt do the right behaviour for me..

angelos3lex commented 3 years ago

Could you guys check if the solution proposed by samuelj1323 in #23 is the reason behind this prob?