How to add unsubscribe link in mail when using Bcc - php

I am using the following code in controller to send newsletters to subscribers
$body = $model->letter_content;
$to_email = 'admin#site.in';
for($i=0;$i<count($msg_to);$i++){
$maitto = $msg_to[$i];
if($maitto != '')
$headers .= 'Bcc:'.$maitto."\r\n";
}
mail($to_email,$subject,$body,$headers);
the variable '$msg_to' contains all subscriber list as array.
The variable '$body' has the saved static newsletter body..
I am sending the mail to admin and adding all subscribers as 'Bcc' as I dont want to use mail function inside the for loop to send individually to all subscribers.
Now I want to add a link in the mail to allow subscribers to unsubscribe..If i was sending mail individually inside the for loop i could have used something like this inside loop before mail() function
$body .= 'UNSUBSCRIBE'
But since here i am using 'Bcc' is there any other way to do it.
Thank you.

You basically have two options in this case:
You could have the unsubscribe link take them to a page where they enter their email address.
You can find a way to start looping through each user to send the email individually, like you said you don't want to do.
One email can only have one set of content. Therefore, no matter how many people you send it to they all will get the same email.
If you really feel strongly about using the BCC field for everyone, option one will work fine.

Related

How to send email to two different email address in codeigniter

I am trying to send an email to two different email address using codeigniter email library.
$mail_array = array($email_address, 'example#yahoo.com');
$this->email->to($mail_array);
I have created an array which contains two email address.
The problem here is that the mail is sent only to one email address which is $email_address and not the other.
Can you guys help me on this.
Looks like your code is just fine but as to method accept an array or comma separated string, try change to comma separated string:
$this->email->to("$email_address, example#yahoo.com");
Note: Make you using real email instead of example#yahoo.com and see the documentation in case you missing anything.
Try the following code:
$mail_array = array($email_address, 'example#yahoo.com');
foreach($mail_array as $email) {
$this->email->to($email);
}
You can add second email to CC like,
$this->email->cc('email#email.com');
In CodeIgniter, sending email is not only simple, but you can configure it on the fly or set your preferences in a config file.
$this->email->to()
Sets the email address(s) of the recipient(s). Can be a single e-mail,
a comma-delimited list or an array:
$this->email->to('someone#example.com');
$this->email->to('one#example.com, two#example.com, three#example.com');
$this->email->to(
array('one#example.com', 'two#example.com', 'three#example.com') );
For more reference see this link - http://www.codeigniter.com/user_guide/libraries/email.html#class-reference
Hope this will help you !!

Oscommerce Mail does not send for concatenated string

hi am new to oscommerce plat form,i have a task i.e once user check the mail means it will send to the user email address and admin email address else it will send mail to admin.the problem is here,once i did the condition means it wont send the mail for concatenate string.
My code is given below:
VARIABLE NAME:email_address=tep_db_prepare_input(HTTP_POST_VARS['email']);
tep_mail(STORE_OWNER,emailaddres."xxyt#somemail.com",EMAIL_SUBJECT,subject,message,name, email_address);
tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
here i want send the mail to user also.
HTTP_POST_VARS['email'] needs Register Globals to be on which in most servers these days is off( as its a security threat).
echo HTTP_POST_VARS['email'] and check are you getting some value or not else use $_POST['email']

Send multiple mail at a time but other reciepient should not display i.e. other recipient in bcc

I want to send mail to multiple user at a time but one user should not see other users addresses i.e. make them in bcc but mail should be sent only once.I have used PHPMailer for that.
$i = 1;
$emailCount = count($newEmail);
foreach($newEmail as $emailAddress)
{
if($emailCount != $i)
{
$phpmail->AddAddress($emailAddress);
}
$i++;
}
The code you are looking for is:
$phpmail->AddBCC($emailAddress);
Place this in your loop to add all of the addresses you would like to send to as BCCs. I believe that you do not need to specify an address using AddAddress, rather you can just add many BCCs and send the email that way. See this other thread for more info.
See this forum thread for information on including the correct files.
As MrGingerbear pointed out, AddBCC is all you need to do.
Each person will only receive one message and they will not be able to see other recipients.
Here is what I have:
$recipients = array(
'recipient1#domain.com' => 'Alex Baker',
'recipient2#domain.com' => 'Charles Dickens',
);
foreach($recipients as $email => $name)
{
$mail->AddBCC($email, $name);
}
EDIT: Clarifying that AddAddress() is not required, simply used most of the time because you usually include an address in the To: field. However, in your scenario, it would be disadvantageous to do so because then all users would see the address of each user.
EDIT 2: It is not possible, because of the way email is designed, to send only one message, but have the To: field look different to each recipient and also not see the email addresses of the other recipients.

How to get the sender's email address from Zend_Mail_Message?

I'm using the Zend_Mail_Message class to display emails inside of my PHP app, and am able to output the subject, content, date and even the NAME of the sender (using $message->from) but I can't figure out how to get the email address of the person who sent the message. The documentation is no help and googling finds a million results for how to send messages with Zend, but nothing about getting the address that sent the message.
EDIT:
This is how I ended up doing it. After some more digging, I found the sender's email in a field called 'return-path'. Unfortunately, this field has a dash in the name (WTF??) so to access it, I had to do this:
$return_path = 'return-path';
$message->reply_to = $zendMessage->$return_path;
Using the return-path caused some problems with some emails, specifically messages from no-reply accounts (mail-noreply#gmail.com, member#linkedin.com etc). These addresses would end up looking something like this:
m-9xpfkzulthmad8z9lls0s6ehupvordjdcor30geppm12kbvyropj1zs5#bounce.linkedin.com
...which obviously doesn't work for display in a 'from' field on the front-end. Email clients like Apple Mail and Gmail show mail-noreply#gmail.com or member#linkedin.com, so that's what I was going for too.
Anyways, after some more research, I discovered that the 'from' field in a Zend Mail Message object always looks something like this:
"user account name" <user#email.com>
The part in < > is what I was after, but simply doing $from = $zend_message->from only gave me the user's account name (hence my original question). After some more playing around, this is how I finally got it working:
$from = $zendMessage->from;
$start = strpos($from, '<');
$email = substr($from, $start, -1);
$result = str_replace('<', '', $email);
Hopefully this will save someone some frustration. If anyone knows of a simpler way of doing this, please let me know.
This works well..
$senderMailAddress = null;
foreach ( $message->getHeader('from')->getAddressList() as $address ) {
if ( $senderMailAddress === null) {
$senderMailAddress = $address->getEmail();
}
}
The main problem here is that many email programs, relay agents and virus scanner along the way do funny stuff to an actually simple and well defined email standard.
Zend_Mail_Message extends to Zend_Mail_Part which has a method called getHeaders(). This will have all the data from an email stored in the head versus the body which is accessed with getContent() and the actual email message.
With this method you'll get an array of all the key/value pairs in the header and while developing you should be able to determine which header field you will actually want. Once you know that you can then get the actual field with getHeader('field_name') or with its actual name directly.
However, if you have to many different email senders you may want to stick with the complete header array though and evaluate multiple fields for the best result like if there's an "reply-to" address. Again there are many uncertainties because the standard isn't always obeyed.

phpMailer - How do you Remove Recipients

There are a lot of StackOverflow questions on this topic, but I couldn't find one that was able to help with the issue I'm having. The script that I'm writing sends out multiple emails to various recipients with different message contents.
I can get this working by re-initializing the phpMailer object multiple times, but what I'd like to be able to do is create the object a single time, and then re-assign the following fields:
$mail->AddAddress($email);
$mail->Subject = $subject;
$mail->IsHTML(false);
$mail->Body = $message;
That way I can just run those four lines of code and then send the mail out, again and again, as many times as necessary. The Subject, IsHTML, and Body fields are easily changed, so the problem I'm having is in the AddAddress function.
As you can probably guess, after I send out the first email, changing recipients for future emails will result in those stacking onto the current list of recipients.
To put it simply, how can I remove the email addresses associated with my $mail object so that I can assign them each time while removing the old addresses?
Is there another function besides AddAddress that I can use that will just assign the addresses?
You can use clearAllRecipients( )
$mailer->clearAllRecipients( ); // clear all
im using this always before sending email to recipients:
// clear addresses of all types
$mail->ClearAddresses(); // each AddAddress add to list
$mail->ClearCCs();
$mail->ClearBCCs();
then im doing just this: (not using CC or BCC, $toaddress is just an array of recipients)
foreach($toaddress as $key=>$val) { $mail->AddAddress( $val ); }
im using PHPMailer 5.2

Categories