func (s *Session) sendTo(r io.Reader) error {
	// Set up blocks & attachments
	var blocks []string
	var attachments []string

	// Will read the whole DATA mail and add it to a block called "mime", this is
	// only done to have a fully compatible email bridge
	fullMime, _ := ioutil.ReadAll(r)

	decodedMessage, err := common.DecodeFromMime(string(fullMime))
	if err != nil {
		return err
	}

	// Fetch both sender and recipient info
	svc := container.Instance.GetResolveService()

	// Check from address
	var fromAddr *address.Address
	if s.IsGateway {
		fromAddr, err = address.NewAddress(s.Account)
	} else {
		fromAddr, err = address.NewAddress(s.From)
	}
	if err != nil {
		return err
	}

	senderInfo, err := svc.ResolveAddress(fromAddr.Hash())
	if err != nil {
		return err
	}

	// Check to address
	toAddr, err := address.NewAddress(s.To)
	if err != nil {
		return err
	}

	recipientInfo, err := svc.ResolveAddress(toAddr.Hash())
	if err != nil {
		return err
	}

	// Setup addressing
	addressing := message.NewAddressing(message.SignedByTypeOrigin)
	addressing.AddSender(fromAddr, nil, decodedMessage.From.Name, s.Info.GetActiveKey().PrivKey, senderInfo.RoutingInfo.Routing)
	addressing.AddRecipient(toAddr, nil, &recipientInfo.PublicKey)

	blocks = decodedMessage.Blocks
	if isGatewayDestination(s.To) {
		if decodedMessage.To == nil || len(decodedMessage.To) < 1 {
			return errIncorrectFormat
		}
		blocks = append(blocks, common.DestinationBlock+","+decodedMessage.To[0].Address)
	}

	for filename, base64Data := range decodedMessage.Attachments {
		b, _ := internal.Decode(base64Data)
		// We write the attachments temporary to disk so we can use it later on message.Compose,
		// however this needs to be improved so we don't need to write them to disk
		fName := filepath.Join(os.TempDir(), filename)
		err = ioutil.WriteFile(fName, b, 0644)
		if err != nil {
			return err
		}

		defer os.Remove(fName)

		attachments = append(attachments, fName)
	}

	// Compose mail
	envelope, err := message.Compose(addressing, decodedMessage.Subject, blocks, attachments)
	if err != nil {
		return err
	}

	// Send mail
	client, err := api.NewAuthenticated(*fromAddr, s.Info.GetActiveKey().PrivKey, senderInfo.RoutingInfo.Routing, nil)
	if err != nil {
		return err
	}

	err = messages.Send(*client, envelope)
	if err != nil {
		return err
	}

	return nil
}
// sendTo sends the mail to the bitmaelum network
sendTo sends the mail to the bitmaelum network
// sendTo sends the mail to the bitmaelum network
func (s *Session) sendTo(r io.Reader) error {
	var blocks []string
	var attachments []string
	fullMime, _ := ioutil.ReadAll(r)
	decodedMessage, err := common.DecodeFromMime(string(fullMime))
	if err != nil {
		return err
	}
	svc := container.Instance.GetResolveService()
	var fromAddr *address.Address
	if s.IsGateway {
		fromAddr, err = address.NewAddress(s.Account)
	} else {
		fromAddr, err = address.NewAddress(s.From)
	}
	if err != nil {
		return err
	}
	senderInfo, err := svc.ResolveAddress(fromAddr.Hash())
	if err != nil {
		return err
	}
	toAddr, err := address.NewAddress(s.To)
	if err != nil {
		return err
	}
	recipientInfo, err := svc.ResolveAddress(toAddr.Hash())
	if err != nil {
		return err
	}
	addressing := message.NewAddressing(message.SignedByTypeOrigin)
	addressing.AddSender(fromAddr, nil, decodedMessage.From.Name, s.Info.GetActiveKey().PrivKey, senderInfo.RoutingInfo.Routing)
	addressing.AddRecipient(toAddr, nil, &recipientInfo.PublicKey)
	blocks = decodedMessage.Blocks
	if isGatewayDestination(s.To) {
		if decodedMessage.To == nil || len(decodedMessage.To) < 1 {
			return errIncorrectFormat
		}
		blocks = append(blocks, common.DestinationBlock+","+decodedMessage.To[0].Address)
	}
	for filename, base64Data := range decodedMessage.Attachments {
		b, _ := internal.Decode(base64Data)
		fName := filepath.Join(os.TempDir(), filename)
		err = ioutil.WriteFile(fName, b, 0644)
		if err != nil {
			return err
		}
		defer os.Remove(fName)
		attachments = append(attachments, fName)
	}
	envelope, err := message.Compose(addressing, decodedMessage.Subject, blocks, attachments)
	if err != nil {
		return err
	}
	client, err := api.NewAuthenticated(*fromAddr, s.Info.GetActiveKey().PrivKey, senderInfo.RoutingInfo.Routing, nil)
	if err != nil {
		return err
	}
	err = messages.Send(*client, envelope)
	if err != nil {
		return err
	}
	return nil
}
func ( s * Session ) sendTo ( r io . Reader ) error { var blocks [ ] string var attachments [ ] string fullMime , _ := ioutil . ReadAll ( r ) decodedMessage , err := common . DecodeFromMime ( string ( fullMime ) ) if err != nil { return err } svc := container . Instance . GetResolveService ( ) var fromAddr * address . Address if s . IsGateway { fromAddr , err = address . NewAddress ( s . Account ) } else { fromAddr , err = address . NewAddress ( s . From ) } if err != nil { return err } senderInfo , err := svc . ResolveAddress ( fromAddr . Hash ( ) ) if err != nil { return err } toAddr , err := address . NewAddress ( s . To ) if err != nil { return err } recipientInfo , err := svc . ResolveAddress ( toAddr . Hash ( ) ) if err != nil { return err } addressing := message . NewAddressing ( message . SignedByTypeOrigin ) addressing . AddSender ( fromAddr , nil , decodedMessage . From . Name , s . Info . GetActiveKey ( ) . PrivKey , senderInfo . RoutingInfo . Routing ) addressing . AddRecipient ( toAddr , nil , & recipientInfo . PublicKey ) blocks = decodedMessage . Blocks if isGatewayDestination ( s . To ) { if decodedMessage . To == nil || len ( decodedMessage . To ) < 1 { return errIncorrectFormat } blocks = append ( blocks , common . DestinationBlock + " " + decodedMessage . To [ 0 ] . Address ) } for filename , base64Data := range decodedMessage . Attachments { b , _ := internal . Decode ( base64Data ) fName := filepath . Join ( os . TempDir ( ) , filename ) err = ioutil . WriteFile ( fName , b , 0644 ) if err != nil { return err } defer os . Remove ( fName ) attachments = append ( attachments , fName ) } envelope , err := message . Compose ( addressing , decodedMessage . Subject , blocks , attachments ) if err != nil { return err } client , err := api . NewAuthenticated ( * fromAddr , s . Info . GetActiveKey ( ) . PrivKey , senderInfo . RoutingInfo . Routing , nil ) if err != nil { return err } err = messages . Send ( * client , envelope ) if err != nil { return err } return nil }
sendTo sends the mail to the bitmaelum network
sendTo sends the mail to the bitmaelum network
// Set up blocks & attachments // Will read the whole DATA mail and add it to a block called "mime", this is // only done to have a fully compatible email bridge // Fetch both sender and recipient info // Check from address // Check to address // Setup addressing // We write the attachments temporary to disk so we can use it later on message.Compose, // however this needs to be improved so we don't need to write them to disk // Compose mail // Send mail