File: Send.pm

package info (click to toggle)
pronto 2.2.6-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,892 kB
  • ctags: 406
  • sloc: perl: 21,215; makefile: 145; sh: 34
file content (203 lines) | stat: -rw-r--r-- 5,460 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# $Id: Send.pm,v 1.2 2001/02/03 16:22:54 muhri Exp $
# -*- perl -*-
package Pronto::Send;
use SelfLoader;
use vars qw( $inter $called_from_send);
use strict;
use Pronto::Data::Message;
1;
__DATA__

sub send 
{
	if ($called_from_send == 1 and $inter ==1)
	{
		use Gtk;
		init Gtk;
	}	
  
	#touch lock file
	open(TMP,">$main::prefs{'MailDir'}/prontosend.running");
	print TMP "$$\n";
	close(TMP);

	use IO::Socket;
	use Sys::Hostname;
	use Text::ParseWords;


	my ($sql, $query, @row, $sql2, $query2, $smtp, @test, $tmp, @body, $sender, $msg, $mailline);
	my $crlf="\r\n";


	$sql = "select id, accountid, sentto, sentfrom, subject, replyto, cc, bcc from messages where boxid = ?";
	$query = $main::conn->prepare($sql);
	$query->execute(2);
	while (@row=$query->fetchrow_array()) {
		my ($msgid, $acctid, $to, $from, $subj, $reply, $cc, $bcc) = @row;
		$sql2 = "select smtp from accounts where id = ?";
		$query2=$main::conn->prepare($sql2);
		$query2->execute($acctid);
		($smtp) = $query2->fetchrow_array();
	 	@test = gethostbyname($smtp);# or next;
		if (!@test) { 
		#       if ($inter == 1) {
		#		&main::err_dialog("SMTP error: Could not resolve hostname $smtp","y");
		#	}
			print(_("SMTP error: Could not resolve hostname ")."$smtp\n");
			next;
		}       	
		if ($called_from_send == 0) {
			while (Gtk->events_pending) { Gtk->main_iteration }
		}
		#get the body
		@body = &Pronto::Data::Message::get_source($msgid);
		# open connection to the smtp server
		$sender=IO::Socket::INET->new( PeerAddr => $smtp,
 						PeerPort => 'smtp(25)',
						Proto => 'tcp',
		        			Timeout=>60);
		$msg=$sender->getline() if ($sender);
		if (!$msg) {  
			if ($inter == 1) {
				&main::err_dialog(_("FATAL: An error occured while connecting to ")."$smtp","y");
			}
			print(_("FATAL: An error occured while connecting to ")."$smtp\n");
			last;
		}	
		if ($called_from_send == 0) {
			while (Gtk->events_pending) { Gtk->main_iteration }
		}
		if($msg=~/^[45](.*)/){
			if ($inter == 1) { 
				&main::err_dialog("$smtp error: smtp error $1","y") 
			}
			print "$smtp error: smtp error $1\n";
			next;
		}
 
		# send helo
		$sender->send("helo ".hostname().$crlf);
		if ($called_from_send == 0) {
			while (Gtk->events_pending) { Gtk->main_iteration }
		}
		$msg=$sender->getline();
		if($msg=~/^[45](.*)/){
			if ($inter == 1) { 
				&main::err_dialog("$smtp error: smtp error $1","y") 
		       	}
			print "$smtp error: smtp error $1\n";
			next;
		}
 
		# send mail from
		$_=$from;
		if (/\<(.*)\>/){
			$from="<$1>";
		} else {
			$from="<".$from.">";
		}
		$from=~s/ //g;

		$sender->send("mail from: ".$from.$crlf);
		$msg=$sender->getline();
		if ($called_from_send == 0) {
			while (Gtk->events_pending) { Gtk->main_iteration }
		}
		if($msg=~/^[45](.*)/){
			if ($inter == 1) { 
				&main::err_dialog("$smtp error: smtp error $1","y") 
		       	}
	 		print "$smtp error: smtp error $1\n";
			next;
		}
	 # send rcpt to for each mail adress
		foreach (quotewords(", ", 1, $to), quotewords(", ", 1, $cc), quotewords(", ", 1, $bcc)){
		#pull <blah@foo.org> from address, or assume address is in blah@foo.org format and just <> it (ie: if you set address as "Kordik, Steven" <stevek@voila.net> this will pull the <stevek@voila.net> out.. if you set address as stevek@voila.net it will just use that, wrapping it in <>) Obviously, this breaks if the To: line contains invalid addresses... but hey.. we should check for that in pronto send function and popup an &&main::err_dialog() to warn... 
			if (/\<(.*)\>/){
				$_="<$1>";
			} else {
				$_="<$_>";
			}
			s/ //g;
			$sender->send("rcpt to: ".$_.$crlf);
 			$msg=$sender->getline();
	 		if ($called_from_send == 0) {
				while (Gtk->events_pending) { Gtk->main_iteration }
			}
			if($msg=~/^[45](.*)/){
				if ($inter == 1) { 
					&main::err_dialog("$smtp error: smtp error $1","y") 
			       	}
	 			print "$smtp error: smtp error $1\n";
				next;
 			}
		}

		# print beginning of data
		$sender->send("data".$crlf);
		$msg=$sender->getline();
		if ($called_from_send == 0) {
			while (Gtk->events_pending) { Gtk->main_iteration }
		}
		if($msg=~/^[45](.*)/){
			if ($inter == 1) { 
				&main::err_dialog("$smtp error: smtp error $1","y") 
		       	}
	 		print "$smtp error: smtp error $1\n";
			next;
		}
 
 
	 # send the body
		foreach $mailline (@body){
 			 $mailline=~s/\n/$crlf/g;
			 $sender->send($mailline);
			if ($called_from_send == 0) {
				while (Gtk->events_pending) { Gtk->main_iteration }
			}
		}
 
		# print data end (a dot)
		$sender->send($crlf.".".$crlf);
		$msg=$sender->getline();
		if ($called_from_send == 0) {
			while (Gtk->events_pending) { Gtk->main_iteration }
		}
		if($msg=~/^[45](.*)/){
			if ($inter == 1) { 
				&main::err_dialog("$smtp error: smtp error $1","y") 
			}
		 	print "$smtp error: smtp error $1\n";
			next;
		}

		$sender->send("quit".$crlf);
		if ($called_from_send == 0) {
			while (Gtk->events_pending) { Gtk->main_iteration }
		}
		$sender->close();
		$sql2 = "update messages set boxid=3 where id = ?";
		$query2=$main::conn->prepare($sql2);
		$query2->execute($msgid);
	}
	
	if(open(TMP,"<$main::prefs{'MailDir'}/pronto.running")) {
		my $proc = <TMP>;
        	close(TMP);
        	if(kill(0,$proc)) {
        		kill('USR2',$proc);
        	}
	}
	unlink("$main::prefs{'MailDir'}/prontosend.running");
	if ($query) {$query->finish;}
	if ($query2) {$query2->finish;}

	if ($called_from_send) {
		$main::conn->disconnect;
	}


}

1;