Пример #1
0
// Send campaign
func (c campaign) send() {
	var r recipient
	var wg sync.WaitGroup

	query, err := models.Db.Prepare("SELECT `id`, `email`, `name` FROM recipient WHERE campaign_id=? AND removed=0 AND status IS NULL")
	checkErr(err)
	defer query.Close()

	q, err := query.Query(c.id)
	checkErr(err)
	defer q.Close()

	for q.Next() {
		err = q.Scan(&r.id, &r.to_email, &r.to_name)
		checkErr(err)
		if r.unsubscribe(c.id) == false || c.sendUnsubscribe {
			models.Db.Exec("UPDATE recipient SET status='Sending', date=NOW() WHERE id=?", r.id)
			pid, iface, host := models.ProfileNext(c.profileId)
			go func(d recipient, p int, i, h string) {
				wg.Add(1)
				rs := d.send(&c, i, h)
				wg.Done()
				models.ProfileFree(p)
				models.Db.Exec("UPDATE recipient SET status=?, date=NOW() WHERE id=?", rs, d.id)
			}(r, pid, iface, host)
		} else {
			models.Db.Exec("UPDATE recipient SET status='Unsubscribe', date=NOW() WHERE id=?", r.id)
			camplog.Printf("Recipient id %s email %s is unsubscribed", r.id, r.to_email)
		}
	}
	wg.Wait()
	camplog.Printf("Done campaign id %s.", c.id)
}
Пример #2
0
func (c campaign) resend() {
	var r recipient

	count := c.countSoftBounce()
	if count == 0 {
		return
	}

	if c.resendCount != 0 {
		camplog.Printf("Start %d resend by campaign id %s ", count, c.id)
	}

	for n := 0; n < c.resendCount; n++ {
		if c.countSoftBounce() == 0 {
			return
		}
		time.Sleep(time.Duration(c.resendDelay) * time.Second)
		query, err := models.Db.Prepare("SELECT DISTINCT r.`id`, r.`email`, r.`name` FROM `recipient` as r,`status` as s WHERE r.`campaign_id`=? AND r.`removed`=0 AND s.`bounce_id`=2 AND UPPER(`r`.`status`) LIKE CONCAT(\"%\",s.`pattern`,\"%\")")
		checkErr(err)
		defer query.Close()

		q, err := query.Query(c.id)
		checkErr(err)
		defer q.Close()

		for q.Next() {
			err = q.Scan(&r.id, &r.to_email, &r.to_name)
			checkErr(err)
			if r.unsubscribe(c.id) == false || c.sendUnsubscribe {
				models.Db.Exec("UPDATE recipient SET status='Sending', date=NOW() WHERE id=?", r.id)
				p, i, h := models.ProfileNext(c.profileId)
				rs := r.send(&c, i, h)
				models.ProfileFree(p)
				models.Db.Exec("UPDATE recipient SET status=?, date=NOW() WHERE id=?", rs, r.id)
			} else {
				models.Db.Exec("UPDATE recipient SET status='Unsubscribe', date=NOW() WHERE id=?", r.id)
				camplog.Printf("Recipient id %s email %s is unsubscribed", r.id, r.to_email)
			}
		}
	}
}