ChosenJuan
ChosenJuan

Reputation: 961

How can I add a PHP script to Auto-respond with a "thank you" message

I'm php newbie that just figured out how to use php with phpmailer to send email addresses of my users to my email address to be added to a newsletter.

However, now I want to add a simple auto-respond script in php, so when users add their email to my guestlist it sends them an autoreply email to their email that says:

Thanks for signing up. [Picture of my logo] www.mysite.com

I've searched and searched, but I haven't been able to find a proper answer on how to create an autorespond script in php. Please let me know how I can accomplish this task. Thank you!

<?php

$email = $_REQUEST['email'] ;

require("C:/inetpub/mysite.com/PHPMailer/PHPMailerAutoload.php");

$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

$mail->Host = "smtp.comcast.net"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "[email protected]"; // SMTP username
$mail->Password = "*******"; // SMTP password
$mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25;                                    


$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("[email protected]", "Guestlist");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "A new member wishes to be added";

$message = $_REQUEST['message'] ;
$mail->Body = $email;
$mail->AltBody = $email;

if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

echo "Message has been sent";

$mail2 = new PHPMailer();

// set mailer to use SMTP
$mail2->IsSMTP();

$mail2->Host = "smtp.comcast.net"; // specify main and backup server
$mail2->SMTPAuth = true; // turn on SMTP authentication
$mail2->Username = "[email protected]"; // SMTP username
$mail2->Password = "*******"; // SMTP password
$mail2->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
$mail2->Port = 25;                                    


$mail2->From = $email;

// below we want to set the email address we will be sending our email to.
$mail2->AddAddress("$email");

// set word wrap to 50 characters
$mail2->WordWrap = 50;
// set email format to HTML
$mail2->IsHTML(true);

$mail2->Subject = "Thank you for joining";

$message = "Please stay tune for updates" ;

$message = $_REQUEST['message'] ;
$mail2->Body = $message;
$mail2->AltBody = $message;

if(!$mail2->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail2->ErrorInfo;
exit;
}

echo "Message has been sent";
?>

Update: 1 Ok, I figured out how to send auto-respond emails to users. However, now the users are receiving messages with their own email address and the name Root user

So what can I do to fix this problem so that users see my email address when they recevie auto-responses, and how can I make sure it says my name instead of root user?

Upvotes: 0

Views: 6403

Answers (2)

Synchro
Synchro

Reputation: 37750

Do not use the submitter's email address as the from address - it won't work as it looks like a forgery and will fail SPF checks. Put your own address as the From address, and add the submitter's address as a reply-to.

Using SMTPSecure = 'ssl' with Port = 25 is an extremely unusual combination, and very likely to be wrong. ssl/465 and tls/587 are more usual.

To send multiple messages, you do not need to create a second instance - just re-use the same one. You can reset any individual properties you want (such as Body) and clear addresses using $mail->clearAddresses(), then just call $mail->send() a second time.

It looks like you have based your code on an old example (try a new one) - make sure you are using latest PHPMailer - at least 5.2.10.

Upvotes: 2

user557846
user557846

Reputation:

add:

echo "Message has been sent";
$mail = new PHPMailer();
$mail->From = '[email protected]';

// below we want to set the email address we will be sending our email to.
$mail->AddAddress($email);

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "Thanks for joining ...";

$message = "Thanks for joining...";
$mail->Body = $email;
$mail->AltBody = $email;

$mail->Send();

Upvotes: 1

Related Questions