1

I'm trying to send emails to users from my site just like how craiglist does it.(E.g When user A responds to a post by user B, an email gets sent out to user A informing him that user A has responded to his post.User B can then press the reply button in his email client like gmial/hotmail and the reply email will get sent to user A, and any further correspondence will be strictly between the 2 of them, with my server only sending the initial email.)

Currently i have written a function that sends out an email to the user upon successful registration and i am thinking of modifying it for use in the situation above. However, i am not sure how i would go about doing it as i can't seem to change the FROM field in my function.

Note.I'm still new to programming in general and am currently using PHP, and Swiftmailer to handle my emails.

My function:

function Activation($email,$token){
$transport=Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
    ->setUsername('test@gmail.com')
    ->setPassword('testpass');

$mailer=Swift_Mailer::newInstance($transport);
$message=Swift_Message::newInstance()
    ->setSubject('Account activation')
    ->setFrom(array('mymail@gmail.com' => '*Website name*'))
    ->setTo(array($email => $email))
    ->setBody("

        Thank you for registering with us. Before we can activate your account one last step must be taken to complete your registration.
        <br />
        <br />
        Please note - you must complete this last step to become a registered member. You will only need to visit this URL once to activate your account.
        <br />
        <br />
        To complete your registration, please visit this URL:
        <a href=http://localhost/test/account/activate.php?code=$token >http://localhost/test/account/activate.php?code=".$token."</a>
        <br />
        <br />
        **** Does The Above URL Not Work? ****
        If the above URL does not work, please go to the following link into your browser:
        http://localhost/test/account/activate.php
        <br />
        <br />
        Please be sure not to add extra spaces. You will need to type in your email and activation code on the page that appears when you visit the URL.
        <br />
        <br />
        Your email is: ".$email."
        <br />
        Your Activation code is: ".$token."
        <br />
        <br />
        If you are still having problems signing up please contact a member of our support staff at *contactemail*
        <br />
        <br />
        All the best



        ","text/html");

    if (!$mailer->send($message))
    {
        throw new Exception('Message sending failed');
    }
}

Any help would be greatly appreciated.

Kenneth .J
  • 1,433
  • 8
  • 27
  • 49
  • Obviously you can change the `FROM` value by simply changing the value you pass in `setFrom()`, however there is more to it than that. If you try to send mail under the guise of some other email domain, you are likely going to have significant email devilerability problems, as your mail will be flagged as spam at a high rate if it even gets past outgoing SMTP. You are really delving into a very complex problem, one that you honestly should probably not be dabbling in as a beginning programmer. – Mike Brant Jan 17 '14 at 20:16
  • @MikeBrant Thanks for taking the time to reply. I've tried changing the setFrom() field, but the email in the setFrom() field does not show in the email sent. Instead the email is shown to be sent from the setUsername() field. I understand that it might be a tad complex, is there any alternative,easier solution and or reading material/tutorial that you can point me to? – Kenneth .J Jan 17 '14 at 20:21

2 Answers2

0

Craigslist creates a temporary forwarding address for you (@craigslist.com) when you post. So, when the first user sends the e-mail, it is rerouted to the other user's e-mail address.

Most SMTP providers do not allow you to impersonate other people's e-mail addresses. This makes sense, since you probably would not want someone untrustworthy to be able to send e-mails from your address.

0

What you describe in the first paragraph is not how craigslist functions. Craigslist will act as intermediary between ALL email communications so that neither party knows the others' true email address.

This is obviously very complex and requires having your own mail server, translation tables, and pop/smtp cron jobs.

However, if you want to accomplish what you first described, it is rather easy. Just use the Sender header and ReplyTo header as described in this answer: Should I use the Reply-To header when sending emails as a service to others?

Community
  • 1
  • 1
Digital Chris
  • 6,177
  • 1
  • 20
  • 29