def email_to_admin(subject, body, username=None, user_email=None, cc_user=True, request_tracker=False): """ Send a basic email to the admins. Nothing more than subject and message are required. """ if request_tracker: sendto, sendto_email = request_tracker_address() else: sendto, sendto_email = admin_address() #E-mail yourself if no users are provided if not username and not user_email: username, user_email = sendto, sendto_email elif not user_email: # Username provided user_email = lookupEmail(username) elif not username: # user_email provided username = '******' if request_tracker or not cc_user: #Send w/o the CC return send_email(subject, body, from_email=email_address_str(username, user_email), to=[email_address_str(sendto, sendto_email)]) #Send w/ the CC return send_email(subject, body, from_email=email_address_str(username, user_email), to=[email_address_str(sendto, sendto_email)], cc=[email_address_str(username, user_email)])
def send_image_request_failed_email(machine_request, exception_str): """ Sends an email to the admins, who will verify the reason for the error, with an option to re-approve the request. """ user_email = lookupEmail(user.username) approve_link = '%s/api/v1/request_image/%s/approve' \ % (settings.SERVER_URL, machine_request.id) body = """ADMINS: A machine request has FAILED." Please look over the exception. If the exception is one-time failure you can re-approve the image here: %s ------------------------------------------------------------------ Machine Request: ID: %d Owner: %s Instance: %s IP Address: %s Exception: %s """ % (approve_link, machine_request.id, machine_request.new_machine_owner, machine_request.instance.provider_alias, machine_request.instance.ip_address, exception_str) subject = 'Your Atmosphere Image is Complete' return email_to_admin(subject, body, user.username, user_email, cc_user=False)
def send_deploy_failed_email(core_instance, exception_str): """ Sends an email to the admins, who will verify the reason for the error. """ user = core_instance.created_by user_email = lookupEmail(user.username) body = """ADMINS: An instance has FAILED to deploy succesfully. The exception and relevant details about the image can be found here: --- Failed Instance Details: Alias: %s Owner: %s (%s) IP Address: %s Image ID: %s --- Exception: %s """ % (core_instance.provider_alias, user, user_email, core_instance.ip_address, core_instance.provider_machine.identifier, exception_str) subject = 'An Atmosphere Instance has failed to launch.' return email_to_admin(subject, body, user.username, user_email, cc_user=False)
def user_address(request): """ Return the username and email given a django request object. """ logger.debug("request = %s" % str(request)) username = request.session.get('username', '') logger.debug("user = %s" % username) user_email = lookupEmail(username) if not user_email: user_email = "*****@*****.**" % username return (username, user_email)
def post(self, request): """ Creates a new feedback email and sends it to admins. """ required = ["message"] missing_keys = check_missing_keys(request.DATA, required) if missing_keys: return keys_not_found(missing_keys) result = self._email(request, request.user.username, lookupEmail(request.user.username), request.DATA["message"]) return Response(result, status=status.HTTP_201_CREATED)
def email_from_admin(username, subject, message, html=False): """ Use user, subject and message to build and send a standard Atmosphere admin email from admins to a user. Returns True on success and False on failure. """ from_name, from_email = admin_address() user_email = lookupEmail(username) return send_email(subject, message, from_email=email_address_str(from_name, from_email), to=[email_address_str(username, user_email)], cc=[email_address_str(from_name, from_email)], html=html)
def post(self, request): """ Creates a new feedback email and sends it to admins """ data = request.DATA required = ['message',] missing_keys = valid_post_data(data, required) if missing_keys: return keys_not_found(missing_keys) #Pass arguments user = request.user message = data['message'] user_email = lookupEmail(user.username) result = feedback_email(request, user.username, user_email, message) return Response(result, status=status.HTTP_201_CREATED)
def send_image_request_email(user, new_machine, name): """ Sends an email to the admins, who will verify the image boots successfully. Upon launching, the admins will forward this email to the user, which will provide useful information about the new image. """ user_email = lookupEmail(user.username) body = """Hello %s, Your image is ready. The image ID is "%s" and the image is named "%s". Thank you for using atmosphere! If you have any questions please contact: [email protected]""" %\ (user.username, new_machine.identifier, name) subject = 'Your Atmosphere Image is Complete' return email_from_admin(user, subject, body)
def post(self, request): """ Creates a new feedback email and sends it to admins """ data = request.DATA required = [ 'message', ] missing_keys = valid_post_data(data, required) if missing_keys: return keys_not_found(missing_keys) #Pass arguments user = request.user message = data['message'] user_email = lookupEmail(user.username) result = feedback_email(request, user.username, user_email, message) return Response(result, status=status.HTTP_201_CREATED)
def send_approved_resource_email(user, request, reason): """ Notify the user the that their request has been approved. """ template = "core/email/resource_request_approved.html" subject = "Your Resource Request has been approved" context = { "user": user.username, "request": request, "reason": reason } from_name, from_email = admin_address() user_email = lookupEmail(user.username) recipients = [email_address_str(user.username, user_email)] sender = email_address_str(from_name, from_email) return send_email_template(subject, template, recipients, sender, context=context, cc=[sender])
def send_image_request_email(user, new_machine, name): """ Sends an email to the admins, who will verify the image boots successfully. Upon launching, the admins will forward this email to the user, which will provide useful information about the new image. """ user_email = lookupEmail(user.username) body = """ADMINS: A new image has been completed. Please ensure the image launches correctly. After verifying the image, forward the contents of this e-mail to: %s ------------------------------------------------------------------ Hello %s, Your image is ready. The image ID is "%s" and the image is named "%s". Thank you for using atmosphere! If you have any questions please contact: [email protected]""" %\ (user_email, user.username, new_machine.identifier, name) subject = 'Your Atmosphere Image is Complete' return email_to_admin(subject, body, user.username, user_email, cc_user=False)